In the following template matching code I don't understand what the following line is doing:
cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
The full code is:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main()
{
cv::Mat ref = cv::imread("ref.jpg");
cv::Mat tpl = cv::imread("temp.jpg");
if (ref.empty() || tpl.empty())
return -1;
cv::Mat gref, gtpl;
cv::cvtColor(ref, gref, CV_BGR2GRAY);
cv::cvtColor(tpl, gtpl, CV_BGR2GRAY);
cv::Mat res(ref.rows - tpl.rows + 1, ref.cols - tpl.cols + 1, CV_32FC1);
cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
cv::threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
while (true)
{
double minval, maxval, threshold = 0.8;
cv::Point minloc, maxloc;
cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
if (maxval >= threshold)
{
cv::rectangle(
ref,
maxloc,
cv::Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows),
CV_RGB(0, 255, 0), 2
);
cv::floodFill(res, maxloc, cv::Scalar(0),
0, cv::Scalar(.1), cv::Scalar(1.));
}
else
break;
}
cv::imshow("reference", ref);
cv::waitKey();
return 0;
}
It's creating the matrix that will contain the result of the matching.
According to the documentation for matchTemplate
With reference to your code:
image -> ref
template -> tpl
W -> ref.cols
H -> ref.rows
w -> tpl.cols
h -> tpl.rows
single-channel 32-bit floating-point -> CV_32FC1
However, since matchTemplate
will create the result image, you don't need to create it. You can simply:
...
cv::Mat res;
cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.