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

asked 2019-10-08 10:25:41 -0600

essamzaky gravatar image

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

edit retag flag offensive close merge delete

Comments

opencv 2.4 calls surf here https://github.com/opencv/opencv/blob...

and opencv 4.1.2 (master) it's here

try to compile with exactly same parameter values

LBerger gravatar imageLBerger ( 2019-10-08 10:46:18 -0600 )edit

Thanx @LBerger , the default values are different , i hade updated the following line to take same values as Opencv v2.3

Ptr<SURF> detector = SURF::create(minHessian,4,2,true,false);

but the result still different , OpenCV2,3 still better

essamzaky gravatar imageessamzaky ( 2019-10-08 11:19:34 -0600 )edit

Do you use a png format to read image (lossless)?

LBerger gravatar imageLBerger ( 2019-10-08 11:29:32 -0600 )edit

Yes the scene image is png and the object image (Patch to detect is jpg) i use the same images in OpenCV 2.3 , but the final result is better in Opencv2.3

essamzaky gravatar imageessamzaky ( 2019-10-08 11:46:17 -0600 )edit

What do you mean "Patch to detect is jpg "?

LBerger gravatar imageLBerger ( 2019-10-08 11:49:48 -0600 )edit

i'm try to build object detection application and Patch is the object image you need to find in the scene , it takes that name in the documentation

essamzaky gravatar imageessamzaky ( 2019-10-08 11:54:23 -0600 )edit