我想制作像boolean findobject(Mat ref_img,Mat object_img)
这样的功能
如果ref_img包含object_img,则返回true,否则返回false。
我正在使用SurfFeatureDetector找出ref_img中的对象,但我没有得到我应该使用哪个参数来得出结论。
提前致谢。
我使用以下代码
虽然它有一个字符串返回值,我想将其更改为bool。
String detect(Mat ref_img, Mat& output)
{
//cvtColor(img, gray, CV_RGBA2GRAY); // Assuming RGBA input
//previous code
/*
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std :: vector<KeyPoint> keypoints_1, keypoints_2;
detector.detect( ref_img, keypoints_1 );
//-- Draw keypoints
Mat img_keypoints_1;
drawKeypoints( ref_img, keypoints_1, img_keypoints_1, Scalar :: all( - 1), DrawMatchesFlags :: DEFAULT );
output = img_keypoints_1;
*/
String result="";
try{
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_object, keypoints_scene;
detector.detect( output, keypoints_object );
detector.detect( ref_img, keypoints_scene );
//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_object, descriptors_scene;
extractor.compute( output, keypoints_object, descriptors_object );
extractor.compute( ref_img, keypoints_scene, descriptors_scene );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
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;
}
ostringstream os;
//os << max_dist;
//String s=os.str();
//result.append("max dist:");
//result.append(s);
//LOGI(s);//os.str());
//os<<min_dist;
//result.append(" min dist:");
//result.append(os.str());
//LOGI(os.str());
//printf("-- Max dist : %f \n", max_dist );
//printf("-- Min dist : %f \n", min_dist );
//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_object.rows; i++ )
{ if( matches[i].distance < 2*min_dist )
{ good_matches.push_back( matches[i]); }
}
Mat img_matches;
drawMatches( output, keypoints_object, ref_img, keypoints_scene,
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;
os << good_matches.size();
result.append("no of good match:");
result.append(os.str());
for( int i = 0; i < good_matches.size(); i++ )
{
//-- Get the keypoints from the good matches
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 );
//-- Get the corners from the image_1 ( the object to be "detected" )
std::vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( output.cols, 0 );
obj_corners[2] = cvPoint( output.cols, output.rows ); obj_corners[3] = cvPoint( 0, output.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] + Point2f(output.cols, 0), scene_corners[1] + Point2f( output.cols, 0), Scalar(0, 255, 0), 4 );
line( img_matches, scene_corners[1] + Point2f( output.cols, 0), scene_corners[2] + Point2f( output.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[2] + Point2f( output.cols, 0), scene_corners[3] + Point2f( output.cols, 0), Scalar( 0, 255, 0), 4 );
line( img_matches, scene_corners[3] + Point2f( output.cols, 0), scene_corners[0] + Point2f( output.cols, 0), Scalar( 0, 255, 0), 4 );
//-- Show detected matches
//imshow( "Good Matches & Object detection", img_matches );
output = img_matches;
}
catch(Exception& e)
{
result.append("exception occured");
}
return result;``
}
答案 0 :(得分:0)
我认为你的函数的作用如下:它匹配参考图像/对象ref_img
和另一个图像(output
)之间的特征点,然后计算这两个图像之间的二维同态。然后它将参考图像的矩形边界映射到另一个图像并绘制它们。
您想要的是决定查询对象是否包含在图像中。我不确定,但我猜你可以根据发现的马克数量或距离来决定。