Pixel India
Pixel India

Reputation: 41

How to use OpenCV's normalized correlation?

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?

Upvotes: 4

Views: 9204

Answers (1)

BloodAxe
BloodAxe

Reputation: 833

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);

Upvotes: 13

Related Questions