OpenCV中的神器Image Watch

Image Watch是在VS2012上使用的一款OpenCV工具,能够实时显示图像和矩阵Mat的内容,跟Matlab很像,方便程序调试,相当好用。跟VS2012配合使用,简直就是一款神器!让我一下就爱上它了!

第一次看到Image Watch是今年3、4月份的时候,当时是在微博上看到新闻,点击链接的下载页面一直出问题,后面就忘了这事,昨天碰巧在OpenCV主页看到OpenCV2.4.5的Change Logs里面有链接,点进能下载果断试用下啊!

OpenCV中的神器Image Watch

闲话少说,先看看部分相关链接。

1、Image Watch 的下载链接

2、OpenCV关于Image Watch的介绍页面链接

3、OpenCV2.4.5在线文档关于Image Watch的介绍文档

4、更详细的信息参见Image Watch的官方网站

直接上图,有个直观印象。

OpenCV中的神器Image Watch

下面利用一个实际的例子,来说明下Image Watch。

利用二维SURF特征和单映射寻找已知物体。输入两幅图像,一幅是需要寻找的物体图像,另一幅是场景中包含此物体的图像。

SURF特征的特征描述方法封装在SurfFeatureDetector类中,利用成员函数detect函数检测出SURF特征的关键点,保存在vector容器中,再利用SurfDesciptorExtractor类进行特征向量的计算,将之前的vector变量变成矩阵形式保存在Mat中。

利用FLANN特征匹配算法进行匹配,此算法封装在FlannBaseMatcher类中,匹配后保留好的特征匹配点。利用findHomography获取匹配特征点之间的变换,最后利用perspectiveTransform定位到场景图中物体的4个点。

代码如下:

#include <stdio.h>
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\nonfree\features2d.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\calib3d\calib3d.hpp> using namespace cv; void usage()
{
std::cout << "Usage: ./FindObjectByFeature <img1> <img2> " << std::endl;
} int main(int argc, char *argv[])
{
if(argc != )
{
usage();
return -;
} Mat img_object = imread(argv[], CV_LOAD_IMAGE_COLOR);
Mat img_scene = imread(argv[], CV_LOAD_IMAGE_COLOR); if(!img_object.data || !img_scene.data)
{
std::cout << "Error reading images!" << std::endl;
return -;
} //step1:检测SURF特征点/////////////////////////////////////////////////////////////////
int minHeassian = ;
SurfFeatureDetector detector(minHeassian); std::vector<KeyPoint> keypoints_object, keypoints_scene; detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene); //step2:计算特征向量///////////////////////////////////////////////////////////////////
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<DMatch> matches;
matcher.match( descriptors_object, descriptors_scene, matches); double max_dist = ; double min_dist = ; //快速计算特征点之间的最大和最小距离///////////////////////////////////////////////////
for(int i = ; 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); //只画出好的匹配点(匹配特征点之间距离小于3*min_dist)//////////////////////////////////
std::vector<DMatch> good_matches; for(int i = ; i < descriptors_object.rows; i++)
{
if(matches[i].distance < *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(-), Scalar::all(-),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); //定位物体////////////////////////////////////////////////////////////////////////////
std::vector<Point2f> obj;
std::vector<Point2f> scene; for(int i = ; i < good_matches.size(); i++)
{
//从好的匹配中获取特征点/////////////////////////////////////
obj.push_back(keypoints_object[good_matches[i].queryIdx].pt);
scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt);
} //找出匹配特征点之间的变换///////////////////
Mat H = findHomography(obj, scene, CV_RANSAC); //得到image_1的角点(需要寻找的物体)//////////
std::vector<Point2f> obj_corners();
obj_corners[] = cvPoint(,);
obj_corners[] = cvPoint(img_object.cols, );
obj_corners[] = cvPoint(img_object.cols, img_object.rows);
obj_corners[] = cvPoint(, img_object.rows);
std::vector<Point2f> scene_corners(); //匹配四个角点/////////////////////////////////////
perspectiveTransform(obj_corners, scene_corners, H); //画出匹配的物体///////////////////////////////////////////////////////////////////////
line(img_matches, scene_corners[] + Point2f(img_object.cols, ), scene_corners[] + Point2f(img_object.cols, ), Scalar(,,), );
line(img_matches, scene_corners[] + Point2f(img_object.cols, ), scene_corners[] + Point2f(img_object.cols, ), Scalar(,,), );
line(img_matches, scene_corners[] + Point2f(img_object.cols, ), scene_corners[] + Point2f(img_object.cols, ), Scalar(,,), );
line(img_matches, scene_corners[] + Point2f(img_object.cols, ), scene_corners[] + Point2f(img_object.cols, ), Scalar(,,), ); imshow("Good Matches & Object detection", img_matches); waitKey();
return ;
}

匹配结果图如下(下图中左边子图为待寻找的物体图像,右边子图场景中寻找到的物体图像):

OpenCV中的神器Image Watch

在Debug模式下,如果我们在程序某处设置调试断点,当程序运行到断点处时,可以在Image Watch窗口(View->Other Windows->Image Watch)查看已经分配内存的Mat图像。

还在将鼠标放置在所选图像上进行放大,最大能放大到像素级别。如下图所示:

OpenCV中的神器Image Watch

在这个例子中,再稍微多说一点编译工程时我遇到的问题。

1、如果用的是OpenCV2.4以后版本,在程序include中包含:#include <opencv2/features2d/features2d.hpp>,可能会出现SurfFeatureDetector无法解析的情况。

这是因为OpenCV2.4后,SurfFeatureDetector类移到了opencv2/nonfree/features2d.hpp,而不是opencv2/features2d/features2d.hpp。因此需要把#include<opencv2/features2d/features2d.hpp>改为

#include <opencv2/nonfree/features2d.hpp>,另外还需要把opencv_nonfree245d.lib库文件链接进去。

2、另外,对于工程中有两个以上OpenCV版本的情况,加入#include <opencv2/nonfree/features2d.hpp>后编译又可能出现重定义的情况。开始我一直没搞清楚原因,后来在*查了下。原因如下

Your compiler and editor are confused by the two OpenCV versions installed on your system.

First, make sure that all the settings ( include paths in Visual Studio, lib path in Visual studio linker settings and bin path -probably an environment variable) point to the same version.

Next, make sure to include all the needed headers. In OpenCV 2.4 and above, SURF and SIFT have been moved to nonfree module, so you also have to install it. Do not forget that some functions may be moved to legacy.

And if you uninstall one version of OpenCV, the editor (which doesn't have all the parsing capabilities of the compiler) will not be confused anymore.

也就是说如果VS中安装了两个以上的OpenCV版本,VS可能会搞混,把include的地址解析到了两个不同OpenCV目录下的头文件,因此引起重定义。

于是在VS中把include目录下的OpenCV2.3.1的头文件地址删除,问题解决。

看来程序员真的应该多上下*,能学到很多知识。

上一篇:Refusing to install package with name “XXXX”


下一篇:Node Newbie Error – NPM Refusing to Install Package as a Dependency of Itself