Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

wrong result when Port code from OpenCV V2.3 into OpenCV 4.1.2

I have the following code written and tested by using openCV v2.3 it's working fine the object is detected

//read the input image      
Mat img_object = imread( strObjectFile, CV_LOAD_IMAGE_GRAYSCALE );
Mat img_scene = imread( strSceneFile, CV_LOAD_IMAGE_GRAYSCALE );
Mat img_scene_color = imread( strSceneFile, CV_LOAD_IMAGE_COLOR );
if( img_scene.empty() || img_object.empty())
{
    return ERROR_READ_FILE;     
}   
//Step 1 Find the object in the scene and find H matrix
//-- 1: Detect the keypoints using SURF Detector
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_object, keypoints_scene;
detector.detect( img_object, keypoints_object );
detector.detect( img_scene, keypoints_scene );

//-- 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_object, descriptors_scene;
extractor.compute( img_object, keypoints_object, descriptors_object );
extractor.compute( img_scene, keypoints_scene, descriptors_scene );

When i try to port this piece of code to OPenCV 4.1.2 the result is different than OpenCV 2.3, OpenCV 2.3 is more curate here it code for OpenCV4.1.2

//read the input image      
Mat img_object_color = _src1.getMat();
Mat img_scene_color = _src2.getMat();
Mat img_object, img_scene;  
//Mat img_scene_color = imread(strSceneFile, IMREAD_COLOR);
if (img_object_color.empty() || img_scene_color.empty())
{
    return ERROR_READ_FILE;
}
//Convert image to gray if it's color
//convert object image to gray
int iChannelsNum = img_object_color.channels();
if (iChannelsNum == 1)
{
    img_object = img_object_color.clone();
}
else if (iChannelsNum == 3)
{
    cvtColor(img_object_color, img_object, COLOR_BGR2GRAY);
}
else if (iChannelsNum == 4)
{
    cvtColor(img_object_color, img_object, COLOR_BGRA2GRAY);
}

//convert Scene image to gray
iChannelsNum = img_scene_color.channels();
if (iChannelsNum == 1)
{
    img_scene = img_scene_color.clone();
}
else if (iChannelsNum == 3)
{
    cvtColor(img_scene_color, img_scene, COLOR_BGR2GRAY);
}
else if (iChannelsNum == 4)
{
    cvtColor(img_scene_color, img_scene, COLOR_BGRA2GRAY);
}

//Step 1 Find the object in the scene and find H matrix
//-- 1: Detect the keypoints using SURF Detector
int minHessian = 400;
Ptr<SURF> detector = SURF::create(minHessian);
std::vector<KeyPoint> keypoints_object, keypoints_scene;
Mat descriptors_object, descriptors_scene;
detector->detectAndCompute(img_object, noArray(), keypoints_object, descriptors_object);
detector->detectAndCompute(img_scene, noArray(), keypoints_scene, descriptors_scene);

detectAndCompute not giving same values , does anyone know what is going wrong ? thanks