Image Watch是在VS2012上使用的一款OpenCV工具,能夠?qū)崟r(shí)顯示圖像和矩陣Mat的內(nèi)容,跟Matlab很像,方便程序調(diào)試,相當(dāng)好用。
多版本OpenCV:
對(duì)于工程中有兩個(gè)以上OpenCV版本的情況,加入#include
也就是說如果VS中安裝了兩個(gè)以上的OpenCV版本,VS可能會(huì)搞混,把include的地址解析到了兩個(gè)不同OpenCV目錄下的頭文件,因此引起重定義。
于是在VS中把include目錄下的OpenCV2.3.1的頭文件地址刪除,問題解決。
Image Watch實(shí)例:
利用二維SURF特征和單映射尋找已知物體。輸入兩幅圖像,一幅是需要尋找的物體圖像,另一幅是場(chǎng)景中包含此物體的圖像。
SURF特征的特征描述方法封裝在SurfFeatureDetector類中,利用成員函數(shù)detect函數(shù)檢測(cè)出SURF特征的關(guān)鍵點(diǎn),保存在vector容器中,再利用SurfDesciptorExtractor類進(jìn)行特征向量的計(jì)算,將之前的vector變量變成矩陣形式保存在Mat中。
利用FLANN特征匹配算法進(jìn)行匹配,此算法封裝在FlannBaseMatcher類中,匹配后保留好的特征匹配點(diǎn)。利用findHomography獲取匹配特征點(diǎn)之間的變換,最后利用perspectiveTransform定位到場(chǎng)景圖中物體的4個(gè)點(diǎn)。
代碼如下:
#include#include #include #include #include #include #include using namespace cv; void usage() { std::cout << "Usage: ./FindObjectByFeature " << std::endl; } int main(int argc, char *argv[]) { if(argc != 3) { usage(); return -1; } Mat img_object = imread(argv[1], CV_LOAD_IMAGE_COLOR); Mat img_scene = imread(argv[2], CV_LOAD_IMAGE_COLOR); if(!img_object.data || !img_scene.data) { std::cout << "Error reading images!" << std::endl; return -1; } //step1:檢測(cè)SURF特征點(diǎn)///////////////////////////////////////////////////////////////// int minHeassian = 400; SurfFeatureDetector detector(minHeassian); std::vector keypoints_object, keypoints_scene; detector.detect(img_object, keypoints_object); detector.detect(img_scene, keypoints_scene); //step2:計(jì)算特征向量/////////////////////////////////////////////////////////////////// SurfDescriptorExtractor extractor; Mat descriptors_object, descriptors_scene; extractor.compute(img_object, keypoints_object, descriptors_object); extractor.compute(img_scene, keypoints_scene, descriptors_scene); //step3:利用FLANN匹配算法匹配特征描述向量////////////////////////////////////////////// FlannBasedMatcher matcher; std::vector matches; matcher.match( descriptors_object, descriptors_scene, matches); double max_dist = 0; double min_dist = 100; //快速計(jì)算特征點(diǎn)之間的最大和最小距離/////////////////////////////////////////////////// for(int i = 0; i < descriptors_object.rows; i++) { double dist = matches[i].distance; if(dist < min_dist) min_dist = dist; if(dist > max_dist) max_dist = dist; } printf("---Max dist: %f \n", max_dist); printf("---Min dist: %f \n", min_dist); //只畫出好的匹配點(diǎn)(匹配特征點(diǎn)之間距離小于3*min_dist)////////////////////////////////// std::vector good_matches; for(int i = 0; i < descriptors_object.rows; i++) { if(matches[i].distance < 3*min_dist) good_matches.push_back(matches[i]); } Mat img_matches; drawMatches(img_object, keypoints_object, img_scene, keypoints_scene, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector (), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); //定位物體//////////////////////////////////////////////////////////////////////////// std::vector obj; std::vector scene; for(int i = 0; i < good_matches.size(); i++) { //從好的匹配中獲取特征點(diǎn)///////////////////////////////////// obj.push_back(keypoints_object[good_matches[i].queryIdx].pt); scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt); } //找出匹配特征點(diǎn)之間的變換/////////////////// Mat H = findHomography(obj, scene, CV_RANSAC); //得到image_1的角點(diǎn)(需要尋找的物體)////////// std::vector obj_corners(4); obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint(img_object.cols, 0); obj_corners[2] = cvPoint(img_object.cols, img_object.rows); obj_corners[3] = cvPoint(0, img_object.rows); std::vector scene_corners(4); //匹配四個(gè)角點(diǎn)///////////////////////////////////// perspectiveTransform(obj_corners, scene_corners, H); //畫出匹配的物體/////////////////////////////////////////////////////////////////////// line(img_matches, scene_corners[0] + Point2f(img_object.cols, 0), scene_corners[1] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); line(img_matches, scene_corners[1] + Point2f(img_object.cols, 0), scene_corners[2] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); line(img_matches, scene_corners[2] + Point2f(img_object.cols, 0), scene_corners[3] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); line(img_matches, scene_corners[3] + Point2f(img_object.cols, 0), scene_corners[0] + Point2f(img_object.cols, 0), Scalar(0,255,0), 4); imshow("Good Matches & Object detection", img_matches); waitKey(0); return 0; }
匹配結(jié)果圖如下(下圖中左邊子圖為待尋找的物體圖像,右邊子圖場(chǎng)景中尋找到的物體圖像):
在Debug模式下,如果我們?cè)诔绦蚰程幵O(shè)置調(diào)試斷點(diǎn),當(dāng)程序運(yùn)行到斷點(diǎn)處時(shí),可以在Image Watch窗口(View->Other Windows->Image Watch)查看已經(jīng)分配內(nèi)存的Mat圖像。