用了ORB那么久,今天第一次将256bit的描述子打印出来,直观看到了match的汉明距离。
上代码:
#include <iostream>
#include <bitset>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat image = imread("t.png");
Mat image1 = imread("t1.png");
Mat gray,gray1;
cvtColor(image, gray, CV_BGR2GRAY);
cvtColor(image1, gray1, CV_BGR2GRAY);
ORB orb(1000);
vector<KeyPoint> keypoints,keypoints1;
orb.detect(gray, keypoints);
orb.detect(gray1, keypoints1);
Mat descriptors, descriptors1;
orb.compute(gray, keypoints, descriptors);
orb.compute(gray1, keypoints1, descriptors1);
BFMatcher bf(NORM_HAMMING);
vector<DMatch> matches,good;
bf.match(descriptors, descriptors1, matches);
for (size_t i = 0; i < matches.size(); ++i)
{
if (matches[i].distance < 20)
good.push_back(matches[i]);
}
cout << good.size() << endl <<endl;
Mat imagem;
drawMatches(image, keypoints, image1, keypoints1, good,imagem);
imshow("1", imagem);
for (size_t i = 0; i < good.size(); ++i)
{
cout << "第" << i+1 << "对匹配上的描述子,汉明距离为:" << (int)good[i].distance << endl;
for (int j = 0; j < 32; j++)
{
cout << bitset<8>( (int)descriptors.at<uchar>(good[i].queryIdx, j) ) << " ";
cout << bitset<8>( (int)descriptors1.at<uchar>(good[i].trainIdx, j) ) <<endl;
}
cout << endl;
}
waitKey(0);
return 0;
}
查看了Mat descriptors的信息,行数为keypoints的值,列数为32,说明type为CV_8U(256/32=8)通道的话就只能是1通道。(PS: 强制转换到CV_32U等均报错)
不同于brief描述子的uchar类型,sift和surf均采用float型。所以ORB(brief)描述子8维,32bytes(256bits),CV_8UC1。SIFT描述子128维,512bytes,CV_32UC4(512*8/32 = 32*4,第一个32表示描述子Mat是32列,第二个32表示float类型)。SURF描述子64维,256bytes,CV_32UC2。(OpenCV里compute的描述子的列数都是32)