C# · 12月 20, 2021

C++ Opencv——图像特征

Opencv2

Opencv3

// 特征点

void FeatureAndCompare(cv::Mat srcImage1)

{

CV_Assert(srcImage1.data != NULL);

Mat c_src1 = srcImage1.clone();

// 转换为灰度

cv::Mat grayMat1;

cv::cvtColor(srcImage1,grayMat1,CV_RGB2GRAY);

//// 加强

equalizeHist(grayMat1,grayMat1);

//// 锐化

sharpenImage1(grayMat1,grayMat1);

//

cv::Ptr ptrBrisk = cv::BRISK::create();

vector kp1;

Mat des1;//descriptor

ptrBrisk->detectAndCompute(grayMat1,Mat(),kp1,des1);

Mat res1;

int drawmode = DrawMatchesFlags::DRAW_RICH_KEYPOINTS;

drawKeypoints(c_src1,res1,Scalar::all(-1),drawmode);//画出特征点

//

//std::cout << "size of description of Img1: " << kp1.size() << endl;

//

namedWindow(“drawKeypoints”,0);

imshow(“drawKeypoints”,c_src1);

cvWaitKey(0);

}

//brisk

void brisk_feature(Mat src1,Mat src2)

{

cv::cvtColor(src1,src1,CV_RGB2GRAY);

cv::cvtColor(src2,src2,CV_RGB2GRAY);

// 加强

equalizeHist(src1,src1);

equalizeHist(src2,src2);

// 锐化

//sharpenImage1(src1,src1);

//sharpenImage1(src2,src2);

Ptr brisk = BRISK::create();

vectorkeypoints1,keypoints2;

Mat descriptors1,descriptors2;

brisk->detectAndCompute(src1,keypoints1,descriptors1);

brisk->detectAndCompute(src2,keypoints2,descriptors2);

Mat dst1,dst2;

drawKeypoints(src1,dst1);

drawKeypoints(src2,dst2);

namedWindow(“output1”,0);

imshow(“output1”,dst1);

waitKey();

namedWindow(“output2”,0);

imshow(“output2”,dst2);

waitKey();

BFMatcher matcher;

vectormatches;

matcher.match(descriptors1,descriptors2,matches);

Mat match_img;

drawMatches(src1,matches,match_img);

namedWindow(“match_img”,0);

imshow(“match_img”,match_img);

double minDist = 1000;

for (int i = 0; i < descriptors1.rows; i++)

{

double dist = matches[i].distance;

if (dist < minDist)

{

minDist = dist;

}

}

printf(“min distance is:%fn”,minDist);

vectorgoodMatches;

for (int i = 0; i < descriptors1.rows; i++)

{

double dist = matches[i].distance;

if (dist < max(1.8*minDist,0.02))

{

goodMatches.push_back(matches[i]);

}

}

Mat good_match_img;

drawMatches(src1,goodMatches,good_match_img,vector(),2);

namedWindow(“goodMatch”,0);

imshow(“goodMatch”,good_match_img);

waitKey(0);

}