opencv - 使用 Surf 进行目标检测

标签 opencv image-processing computer-vision

我正在尝试从视频中检测车辆,我将在实时应用程序中进行检测,但目前为了便于理解,我在视频中进行检测,代码如下:

void surf_detection(Mat img_1,Mat img_2); /** @function main */

int main( int argc, char** argv )
{

 int i;
 int key;

 CvCapture* capture = cvCaptureFromAVI("try2.avi");// Read the video file

 if (!capture){

     std::cout <<" Error in capture video file";
     return -1;
 }

 Mat img_template = imread("images.jpg"); // read template image

int numFrames = (int) cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);



IplImage* img = 0; 

for(i=0;i<numFrames;i++){
  cvGrabFrame(capture);          // capture a frame
  img=cvRetrieveFrame(capture);  // retrieve the captured frame


  surf_detection (img_template,img);

  cvShowImage("mainWin", img); 
  key=cvWaitKey(20);           

}

 return 0;
 }

void surf_detection(Mat img_1,Mat img_2)
{ 

if( !img_1.data || !img_2.data )
{ 
    std::cout<< " --(!) Error reading images " << std::endl; 

}




//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1, keypoints_2;

std::vector< DMatch > good_matches;

do{ 

detector.detect( img_1, keypoints_1 );
detector.detect( img_2, keypoints_2 );

//-- Draw keypoints

Mat img_keypoints_1; Mat img_keypoints_2;
drawKeypoints( img_1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT );
drawKeypoints( img_2, keypoints_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT );

//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_1, descriptors_2;
extractor.compute( img_1, keypoints_1, descriptors_1 );
extractor.compute( img_2, keypoints_2, descriptors_2 );


//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches );
double max_dist = 0; 
double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )
{ 
    double dist = matches[i].distance;
if( dist < min_dist )
    min_dist = dist;
if( dist > max_dist ) 
    max_dist = dist;
}


//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist )


for( int i = 0; i < descriptors_1.rows; i++ )
{ 
    if( matches[i].distance < 2*min_dist )
        { 
                good_matches.push_back( matches[i]);
        }
}

}while(good_matches.size()<100);

//-- Draw only "good" matches
Mat img_matches;
drawMatches( img_1, keypoints_1, img_2, keypoints_2,good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

//-- Localize the object
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for( int i = 0; i < good_matches.size(); i++ )
{
//-- Get the keypoints from the good matches
obj.push_back( keypoints_1[ good_matches[i].queryIdx ].pt );
scene.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );
}


Mat H = findHomography( obj, scene, CV_RANSAC );


//-- Get the corners from the image_1 ( the object to be "detected" )
std::vector<Point2f> obj_corners(4);
obj_corners[0] = Point2f(0,0); 
obj_corners[1] = Point2f( img_1.cols, 0 );
obj_corners[2] = Point2f( img_1.cols, img_1.rows ); 
obj_corners[3] = Point2f( 0, img_1.rows );
std::vector<Point2f> scene_corners(4);

perspectiveTransform( obj_corners, scene_corners, H);

//-- Draw lines between the corners (the mapped object in the scene - image_2 )
line( img_matches, scene_corners[0] , scene_corners[1] , Scalar(0, 255, 0), 4 );
line( img_matches, scene_corners[1], scene_corners[2], Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[2] , scene_corners[3], Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[3] , scene_corners[0], Scalar( 0, 255, 0), 4 );
imshow( "Good Matches & Object detection", img_matches );

}

我得到以下输出

enter image description here

和 std::cout << scene_corners[i](结果)

std::cout << scene_corners[i] (Result)

H 值:

enter image description here

但我的问题是为什么它不在检测到的对象上绘制矩形:

Rectangle is visible on detected object

我在简单的视频和图像上做这个,但是当我在静态相机上做的时候,如果没有那个矩形可能会很困难

最佳答案

首先,在您显示的图像中,根本没有绘制矩形。你能在图像中间画一个矩形吗?

然后,看下面的代码:

int x1 , x2 , y1 , y2 ;
x1 = scene_corners[0].x + Point2f( img_1.cols, 0).x ; 
y1 = scene_corners[0].y + Point2f( img_1.cols, 0).y ; 
x2 = scene_corners[0].x + Point2f( img_1.cols, 0).x + in_box.width ; 
y2 = scene_corners[0].y + Point2f( img_1.cols, 0).y + in_box.height ;

我不明白为什么要将 in_box.widthin_box.height 添加到每个角(它们在哪里定义?)。您应该改用 scene_corners[2]。但是注释行应该在某处打印一个矩形。

既然您要求提供更多详细信息,那么让我们看看您的代码中发生了什么。

首先,如何到达perspectiveTransform()

  1. 您使用 detector.detect 检测特征点。它为您提供了两个图像中的兴趣点。
  2. 您使用 extractor.compute描述这些功能。它为您提供了一种比较兴趣点的方法。比较两个特征的描述符可以回答以下问题:这些点有多相似?*
  3. 您实际上将第一张图片上的每个特征与第二张图片中的所有特征进行比较(某种程度上),并为每个特征保留最佳匹配。此时,您知道看起来最相似的特征对。
  4. 您只保留good_matches。因为有可能发生这样的情况,对于一个特征,另一幅图像中最相似的特征实际上是完全不同的(它仍然是最相似,因为您没有更好的选择)。这是删除错误匹配项的第一个过滤器。
  5. 你找到一个对应于你找到的匹配项的单应变换。这意味着您尝试找出第一个图像中的点应该如何投影到第二个图像中。然后,您获得的单应矩阵允许您将第一张图像的任何点投影到第二张图像中的对应点上。

其次,你用这个做什么?

现在变得有趣了。 您有一个单应矩阵,允许您将第一张图像的任何点投影到第二张图像中的对应点上。因此,您可以决定在对象周围绘制一个矩形(即 obj_corners),并将其投影到第二张图像上(perspectiveTransform( obj_corners, scene_corners, H);) .结果在 scene_corners 中。

现在您想使用scene_corners 绘制一个矩形。但还有一点:drawMatches() 显然将您的两个图像放在 img_matches 中并排放置。但是投影(单应矩阵)是在图像上单独计算的!这意味着每个 scene_corner 都必须进行相应的翻译。由于场景图像绘制在对象图像的右侧,因此您必须将对象图像的宽度添加到每个 scene_corner 以便将它们平移到右侧。

这就是将 0 添加到 y1y2 的原因,因为您不必垂直翻译它们。但是对于x1x2,你必须添加img_1.cols

//-- Draw lines between the corners (the mapped object in the scene - image_2 )
line( img_matches, scene_corners[0] + Point2f( img_1.cols, 0), scene_corners[1] + Point2f( img_1.cols, 0), Scalar(0, 255, 0), 4 );
line( img_matches, scene_corners[1] + Point2f( img_1.cols, 0), scene_corners[2] + Point2f( img_1.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[2] + Point2f( img_1.cols, 0), scene_corners[3] + Point2f( img_1.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[3] + Point2f( img_1.cols, 0), scene_corners[0] + Point2f( img_1.cols, 0), Scalar( 0, 255, 0), 4 );

所以我建议您取消注释这些行并查看是否绘制了一个矩形。如果不是,请尝试对值(例如 Point2f(0, 0)Point2f(100, 100))进行硬编码,直到成功绘制矩形。也许您的问题来自同时使用 cvPointPoint2f。也尝试使用 Scalar(0, 255, 0, 255)...

希望对您有所帮助。

*必须理解,两个点可能看起来完全相同,但实际上并不对应于同一点。想想一个真正重复的图案,例如建筑物 window 的角落。所有的 window 看起来都一样,所以两个不同 window 的角可能看起来非常相似,即使这显然是错误的匹配。

关于opencv - 使用 Surf 进行目标检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17570421/

相关文章:

machine-learning - caffe|消息类型 "caffe.ParamSpec"没有名为 "propagate_down"的字段

c++ - 确定直方图的所有局部最大值的函数

c++ - 如何仅在 Canny 函数使用 OpenCV 检测该图像的边缘时模糊 Cv::Mat 图像

c++ - 收集使用鼠标绘制的线条中的像素值

Android Studio - 与 Android Studio 配合良好的图像处理库

matlab - 如何在 Matlab 中从 RGB 图像创建训练示例?

java - OpenCV VideoWriter fourCC Android

opencv - 在Opencv中映射2图像

android - BoofCV 多边形绘图等效于 Android

c++ - SIFT 匹配给出非常差的结果