Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中使用open cv查找图片中的已知对象_Android_C++_Opencv - Fatal编程技术网

如何在android中使用open cv查找图片中的已知对象

如何在android中使用open cv查找图片中的已知对象,android,c++,opencv,Android,C++,Opencv,我想做一个类似布尔函数findobjectMat ref\u img,Mat object\u img的函数 如果ref\u img包含对象,则返回true,否则返回false。 我正在使用SurfFeatureDetector查找参考图像中的对象,但我不知道应该使用哪个参数得出结论。 提前谢谢。 我正在使用以下代码 虽然它有一个字符串返回值,但我想将其更改为bool String detect(Mat ref_img, Mat& output) { //cvtColor(img, g

我想做一个类似布尔函数findobjectMat ref\u img,Mat object\u img的函数 如果ref\u img包含对象,则返回true,否则返回false。 我正在使用SurfFeatureDetector查找参考图像中的对象,但我不知道应该使用哪个参数得出结论。 提前谢谢。 我正在使用以下代码 虽然它有一个字符串返回值,但我想将其更改为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;``
}

我认为您的函数的作用如下:它匹配参考图像/对象ref_img和另一个图像输出之间的特征点,然后计算这两个图像之间的二维单应性。然后将参考图像的矩形边界映射到另一个图像并绘制它们

您需要确定查询的对象是否包含在图像中。我不确定,但我想你可以试着根据发现的马赫数或它们的距离来决定