Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OpenCV's normalized correlation?

Tags:

c++

opencv

How do I use OpenCV's normalized correlation? Could anyone provide a code sample?

My problem: I have a screw head image and need to find the center of the screw. So I am thinking of using OpenCV correlation, is that a good idea?

You can find an example image under the link below:

http://imageshack.us/photo/my-images/685/screw1.png/

Please provide me with a code sample for correlation in OpenCV. How is it used? What is the output of the correlation function? Will the correlation function provide the screw location?

like image 903
Pixel India Avatar asked Dec 13 '22 00:12

Pixel India


1 Answers

I think you are looking for cv::matchTemplate function:

cv::Mat image;  // Your input image
cv::Mat templ;  // Your template image of the screw 
cv::Mat result; // Result correlation will be placed here

// Do template matching across whole image
cv::matchTemplate(image, templ, result, CV_TM_CCORR_NORMED);

// Find a best match:
double minVal, maxVal;
cv::Point minLoc, maxLoc;
cv::minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc);

// Regards to documentation the best match is in maxima location
// (http://opencv.willowgarage.com/documentation/cpp/object_detection.html)

// Move center of detected screw to the correct position:  
cv::Point screwCenter = maxLoc + cv::Point(templ.cols/2, templ.rows/2);
like image 191
BloodAxe Avatar answered Dec 27 '22 12:12

BloodAxe