如何使用OpenCV的规范化相关性?

How to use OpenCV's normalized correlation?

本文关键字:相关性 规范化 何使用 OpenCV      更新时间:2023-10-16

如何使用OpenCV的规范化相关性?任何人都可以提供代码示例吗?

我的问题:我有一个螺丝头图像,需要找到螺丝的中心。所以我正在考虑使用 OpenCV 关联,这是一个好主意吗?

您可以在以下链接下找到示例图像:

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

请为我提供一个在 OpenCV 中进行关联的代码示例。如何使用?相关函数的输出是什么?相关函数会提供螺钉位置吗?

我认为您正在寻找cv::matchTemplate函数:

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