Opencv The result data of function matchshape is inf

I tested opencv matchshape function.
but The result data of function matchshape is inf.

I tested on the Binary image(after Threshold) and Gray Image

In binary image, inf data does not come out, but double type data normally. On the other hand, inf data comes out in Gray image. But I would like to use this normally in Gray image. Is there any way to solve this problem?

please show image & code, thank you

ok,

  1. Images and process status

    [source image, RGB image]


[After pre-processing_Left image of source]

[After pre-processing_Right image of source]


[After Canny Edge Detection_Left image of source]

[After Canny Edge Detection_Right image of source]


[Result of contourshape. As you can see Left is ok, but Right is strange]

Left side similar values
similar value: [0]index, value : [3.257]
similar value: [1]index, value : [2.248]
similar value: [2]index, value : [2.748]
similar value: [3]index, value : [4.316]
similar value: [4]index, value : [2.605]
similar value: [5]index, value : [2.639]

Right side similar values
similar value_right: [0]index, value : [2.121]
similar value_right: [1]index, value : [2.433]
similar value_right: [2]index, value : [2.675]
similar value_right: [3]index, value : [2.451]
similar value_right: [4]index, value : [inf]
similar value_right: [5]index, value : [inf]
similar value_right: [6]index, value : [inf]
similar value_right: [7]index, value : [2.299]
similar value_right: [8]index, value : [2.367]
similar value_right: [9]index, value : [2.878]
similar value_right: [10]index, value : [2.501]
similar value_right: [11]index, value : [4.29]
similar value_right: [12]index, value : [2.835]
similar value_right: [13]index, value : [2.917]
similar value_right: [14]index, value : [3.71]
similar value_right: [15]index, value : [4.134]
similar value_right: [16]index, value : [4.496]

source code
//Data Initialize
if(!g_InImg1_ROI.empty()){ g_InImg1_ROI.zeros(g_InImg1_ROI.rows, g_InImg1_ROI.cols, CV_8UC3);}
if(!g_InImg2_ROI.empty()){ g_InImg2_ROI.zeros(g_InImg2_ROI.rows, g_InImg2_ROI.cols, CV_8UC3);}

if(!L_hist_mat.empty()){ L_hist_mat.zeros(L_hist_mat.rows, L_hist_mat.cols, CV_8UC3);}
if(!R_hist_mat.empty()){ R_hist_mat.zeros(R_hist_mat.rows, R_hist_mat.cols, CV_8UC3);}


//ROI Setting
cv::Mat ROI_L_g_InImg1 = g_InImg1(cv::Rect(0,0,1080,660)); //Left
cv::Mat ROI_R_g_InImg1 = g_InImg2(cv::Rect(200,0,1080,660)); //Right


g_InImg1_ROI = ROI_L_g_InImg1.clone();//Left
g_InImg2_ROI = ROI_R_g_InImg1.clone();//Right

//output for result
//Left
Mat outimg = ROI_L_g_InImg1.clone();
g_Left_result=outimg.clone();

//Right
Mat outimg2 = ROI_R_g_InImg1.clone();
g_Right_result=outimg2.clone();


//Remove noise
//Left
Mat median_result;
cv::medianBlur(ROI_L_g_InImg1,median_result,17);
cv::GaussianBlur(median_result,median_result,Size(5,5),0,0);


//Right
Mat median_result2;
cv::medianBlur(ROI_R_g_InImg1,median_result2,17);
cv::GaussianBlur(median_result2,median_result2,Size(5,5),0,0);


//shape match mat
Mat shape_mat_left, shape_mat_right;
shape_mat_left = median_result.clone();
shape_mat_right = median_result2.clone();
cv::cvtColor(shape_mat_left,shape_mat_left,CV_BGR2GRAY);
cv::cvtColor(shape_mat_right,shape_mat_right,CV_BGR2GRAY);

cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE();
clahe->setClipLimit(2);
clahe->setTilesGridSize(Size(8,8));
clahe->apply(shape_mat_left, shape_mat_left);
clahe->apply(shape_mat_right, shape_mat_right);
//cv::threshold(shape_mat_left,shape_mat_left,0,255,THRESH_OTSU);
//cv::threshold(shape_mat_right,shape_mat_right,0,255,THRESH_OTSU);
cv::imshow("shape_mat_left",shape_mat_left);
cv::imshow("shape_mat_right",shape_mat_right);

Mat Edge_Left, Edge_Right;
Mat LX, LY, RX, RY;
Canny_Ben(shape_mat_left,Edge_Left,70,130,3,0,LX,LY); //Get Edge
Canny_Ben(shape_mat_right,Edge_Right,70,130,3,0,RX,RY); //Get Edge
cv::imshow("Left",Edge_Left);
cv::imshow("Right",Edge_Right);


cv::findContours(Edge_Left, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
cv::findContours(Edge_Right, contours2, RETR_EXTERNAL, CHAIN_APPROX_NONE);


cont_simlar.push_back(make_pair(std::round(cv::matchShapes(g_contour_left_save,contours[i],CONTOURS_MATCH_I3,0) * 1000)/1000,i));

cont_simlar2.push_back(make_pair(std::round(cv::matchShapes(g_contour_right_save,contours2[i],CONTOURS_MATCH_I3,0)*1000)/1000,i));

if you want source image files, I can upload at here

I aleady putted, image and source code. please answer for my question. thank you.

btw, input to moments() / matchContours should be single contours (or images thereof) , your images have multiple.
none of your sample images above look appropriate for shape matching

also try to avoid Canny(), as it’s producing outer and inner edges

Thank you for your reply.
I don’t understand your reply. because If you reply right, Left image also should be same problem. also, as you can see, Left Image have multiple contours.
I just wonder why the return value of this function is inf data.

your code does not show the matching, but imo, you should match (single) contours, not (whole) images here

Left side show the image matching success…
I first saved the contour, and after comparing the saved contour with several contours, the contour with the smallest return value (double) data is displayed in yellow.

Left side similar values
similar value: [0]index, value : [3.257]
similar value: [1]index, value : [2.248]
similar value: [2]index, value : [2.748]
similar value: [3]index, value : [4.316]
similar value: [4]index, value : [2.605]
similar value: [5]index, value : [2.639]

In the Left side yellow line is [1]index, value : [2.248]

I just want know about reason of inf data type… and how to treat inf data type in Opencv

inf is usually the result of dividing by zero
if you’re curious, follow it back, eg. print out moments() and HuMoments()

1 Like

thank you for your reply

can I ask to you something, In my case just used matchshapes function, I did not use moments() and HuMoments(). If I use matchshapes, how to deal this problem?

they are used internally.
think of matchshapes() as the euclidean distance between 2 HuMoments (which you get from moments())

1 Like

ok, got it. your replay and explation

I tested it by printing moment value. you are right. inf value is moment have ‘0’ value. thank you for your advice