IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> opencv surf代码备份 -> 正文阅读

[人工智能]opencv surf代码备份

别人的

import cv2 as cv
import numpy as np
import pdb
buttons=cv.imread('/home/pi/Desktop/myfile/python/surf/buttons.jpg')
interface=cv.imread('/home/pi/Desktop/myfile/python/surf/device1.png')
button1=buttons[26:86,66:134,:]
surf=cv.xfeatures2d.SURF_create(100)
kp1,des1=surf.detectAndCompute(button1,None)
kp2,des2=surf.detectAndCompute(interface,None)
bf=cv.BFMatcher()
matches=bf.knnMatch(des1,des2,k=2)
#good=[[m] for m,n in matches if m.distance<0.75*n.distance]
good = []
for m,n in matches:
    if m.distance < 0.7*n.distance:
        good.append(m)
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC,5.0)
matchesMask = mask.ravel().tolist()
h,w,_ = button1.shape
pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
dst = cv.perspectiveTransform(pts,M)
img2 = cv.polylines(interface,[np.int32(dst)],True,255,3, cv.LINE_AA)
#img3=cv.drawMatchesKnn(button1,kp1,interface,kp2,good,None,flags=2)
cv.imshow('sp',img2)
cv.waitKey(0)
cv.destroyAllWindows()
exit()
#pdb.set_trace()

自己的

import numpy as np
import cv2 as cv

def GuassianKernel(sigma, dim):
    kernel=np.arange(dim)-dim//2
    kernel,_=np.meshgrid(kernel,kernel)
    kernel=(0.5/np.pi/sigma/sigma)*np.exp(-kernel**2)
    return result

def get_keypoints(img):
    sigma=1.52
    n=3
    Stack=n+3
    Octive=int(np.log2(min(img.shape[:2])))-3
    k=2**(1/n)
    sigma = [[(k ** s) * sigma0 * (1 << oct) for s in range(Stack)] for oct in range(Octave)]
    samplePyramid = [img[::(1 << oct), ::(1 << oct)] for oct in range(Octave)]
    GuassianPyramid = []
    for i in range(Octave):
        GuassianPyramid.append([])
        for j in range(Stack):
            dim = 2 * int(3 * sigma[i][j] + 0.5) + 1
            kernel = GuassianKernel(sigma[i][j], dim)
            conv = convolve(kernel, samplePyramid[i], [dim // 2, dim // 2, dim // 2, dim // 2], [1, 1])

def surfmatch(img1, img2, Hessian):
    if len(img1.shape):
        pass

surf测试test.cpp

#include <iostream>
#include "opencv2/opencv.hpp"
#include "surfmatch.hpp"
using namespace std;
using namespace cv;

int main()
{
    cout << "Locate 3D test." << CV_VERSION << endl;
	Mat imgshow;
	Mat box = imread("/home/pi/Desktop/myfile/hand.png");
	Mat scene = imread("/home/pi/Desktop/myfile/camera.png");
	Detector detector(box);
    scene = scene(Rect(640, 0, 640, 480));
    detector.find_known_object(scene, imgshow);
	imshow("test1", imgshow);
    waitKey(0);
    return 0;
}

摄像头检测camera.cpp

#include <iostream>
#include "opencv2/opencv.hpp"
#include "surfmatch.hpp"
using namespace std;
using namespace cv;

int main()
{
    cout << "Locate 3D test." << CV_VERSION << endl;
	Mat frame, Lcap, Rcap, imgshowL, imgshowR;
	Mat device = imread("/home/pi/Desktop/myfile/device2.png");
	Mat box = device(Rect(275, 108, 340, 222));
	Detector detector(box);
	VideoCapture cap(0);
	cap.set(CAP_PROP_FRAME_WIDTH, 1280);
	cap.set(CAP_PROP_FRAME_HEIGHT, 480);
	char ch;
	if (!cap.isOpened()) return 1;
	while (1){
		if (!cap.read(frame)) break;
        Lcap = frame(Rect(0,0,640,480));
        Rcap = frame(Rect(640, 0, 640, 480));
		if (detector.find_known_object(Lcap, imgshowL)) continue;
 		if (detector.find_known_object(Rcap, imgshowR)) continue;
		imshow("test1", imgshowL);
        imshow("test2", imgshowR);
		ch = (char)waitKey(10);
		if (ch == 'q') break;
	}
	cap.release();
    return 0;
}

检测器类detector.hpp

#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/xfeatures2d.hpp"
class Detector{
public:
	Detector(const cv::Mat& _box);
	int find_known_object(const cv::Mat& src, cv::Mat& dst);
private:
	cv::Mat box;
	cv::Ptr<cv::xfeatures2d::SURF> detector;
	std::vector<cv::KeyPoint> objKeypoints;
	cv::Mat objDescriptors;
};

Detector::Detector(const cv::Mat& _box)
{
	box = _box.clone();
	detector = cv::xfeatures2d::SURF::create();
	detector->setHessianThreshold(400);
	detector->detectAndCompute(box, cv::Mat(), objKeypoints, objDescriptors);
}

/*
Searching a known object in a scene.
@param src: scene.
@param dst: output.*/
int Detector::find_known_object(const cv::Mat& src, cv::Mat& dst)
{
	using namespace cv;
	using namespace cv::xfeatures2d;
	using namespace std;
	std::vector<KeyPoint> keypoints;
	Mat descriptors;
	FlannBasedMatcher matcher;
	vector<DMatch> matches, goodMatches;
	dst = src.clone();
	detector->detectAndCompute(src, Mat(), keypoints, descriptors);
	try { matcher.match(objDescriptors, descriptors, matches); }
	catch (const exception &) { return 1; }
	double dist,  mindist = 100;
	for (int i = 0; i < objDescriptors.rows; i++) {
		dist = matches[i].distance;
		if (dist < mindist)
			mindist = dist;
	}
	for (int i = 0; i < objDescriptors.rows; i++)
		if (matches[i].distance <= max(3 * mindist, 0.08))
			goodMatches.push_back(matches[i]);
	// drawMatches(box, keypoints1, src, keypoints2,
	// 	goodMatches, dst, Scalar::all(-1), Scalar::all(-1),
	// 	vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
	vector<Point2f>vecobj, vecsce;
	for (int i = 0; i < goodMatches.size(); i++) {
		vecobj.push_back(objKeypoints[goodMatches[i].queryIdx].pt);
		vecsce.push_back(keypoints[goodMatches[i].trainIdx].pt);
	}
	cv::Mat H;
	try { H = findHomography(vecobj, vecsce, RHO); }
	catch (const exception &) { return 2; }
	vector<Point2f>objCorners(4);
	objCorners[0] = Point(0, 0);
	objCorners[1] = Point(box.cols, 0);
	objCorners[2] = Point(box.cols, box.rows);
	objCorners[3] = Point(0, box.rows);
	vector<Point2f>sceneCorners(4);
	try { perspectiveTransform(objCorners, sceneCorners, H); }
	catch (const exception &) { return 3; }
	line(dst, sceneCorners[0], sceneCorners[1], Scalar(0, 255, 0), 1);
	line(dst, sceneCorners[1], sceneCorners[2], Scalar(0, 255, 0), 1);
	line(dst, sceneCorners[2], sceneCorners[3], Scalar(0, 255, 0), 1);
	line(dst, sceneCorners[3], sceneCorners[0], Scalar(0, 255, 0), 1);
	// line(dst, sceneCorners[0] + Point2f(box.cols, 0), sceneCorners[1] + Point2f(box.cols, 0), Scalar(0, 255, 0), 1);
	// line(dst, sceneCorners[1] + Point2f(box.cols, 0), sceneCorners[2] + Point2f(box.cols, 0), Scalar(0, 255, 0), 1);
	// line(dst, sceneCorners[2] + Point2f(box.cols, 0), sceneCorners[3] + Point2f(box.cols, 0), Scalar(0, 255, 0), 1);
	// line(dst, sceneCorners[3] + Point2f(box.cols, 0), sceneCorners[0] + Point2f(box.cols, 0), Scalar(0, 255, 0), 1);
	// Point2f vecbox;
	// Point2f vecscene;
	// Point2f kptmp;
	// int cnt = goodMatches.size();
	// for (int i = 0; i < cnt; i++)
	// {
	// 	vecbox += keypoints1[goodMatches[i].queryIdx].pt;
	// 	vecscene += keypoints2[goodMatches[i].trainIdx].pt;
	// }
	// vecbox /= (double)cnt;
	// vecscene /= (double)cnt;
	// circle(dst, (Point)vecscene, 10, Scalar(0, 0, 255), 3);
	return 0;
}
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-10-23 12:29:25  更:2021-10-23 12:33:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 8:45:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码