问题描述
我已经在我的iphone项目中实现了这个OpenCV版本:
i've implemented this OpenCV build into my iphone project:
它构建成功,我知道如何用我的相机捕获图像,灰度和显示在一个imageview。
It builds successfully and i know how to capture an image with my camera, grayscale it and show it on an imageview.
但我会喜欢做的是模板匹配与我捕获的图像(使用相机)对模板图像。
But what i'd like to do is templatematching with the image that i captured (with the camera) against an template image.
我有两个图像grayscaled,但我只是不知道'matchTemplate'工作。这是我到目前为止:
I have both images grayscaled, but i just don't get how the 'matchTemplate' works. This is what i have so far:
cv::Mat grayFrame, grayImg, output;
// This is the template image which i store in a cv::Mat and
// after that i grayscale the image
UIImage *testImage = [UIImage imageNamed:@"qr.png"];
cv::Mat tempMat = [testImage CVMat];
cv::cvtColor(tempMat, grayImg, cv::COLOR_RGB2GRAY);
// Convert captured frame to grayscale
cv::cvtColor(_lastFrame, grayFrame, cv::COLOR_RGB2GRAY);
// Having trouble here...
cv::matchTemplate(grayFrame, grayImg, output, CV_TM_CCORR_NORMED);
// Display result
// This already works for both the captured image and the template image
camView.image = [UIImage imageWithCVMat:grayFrame];
此代码的问题是这行:
cv :: matchTemplate(grayFrame,grayImg,output,1);
cv::matchTemplate(grayFrame, grayImg, output, 1);
我不确定我是否正确。因为我不会在输出像MinMax得到结果,以获得找到的匹配的位置...我甚至传递正确的变量到matchTemplate函数... ??
I'm not sure if i'm doing it right. Because i don't get a result back in 'output' like "MinMax" to get the positions of the found match... Am i even passing the right variables to the matchTemplate function...??
无论如何,有人可以告诉我我做错了什么以及如何使这项工作?
Anyway, can someone please tell me what i'm doing wrong and how to make this work?
- edit -
添加了我的变量output的屏幕截图
感谢任何帮助!
推荐答案
matchTemplate()函数返回一个矩阵,其值表示原始图像的每个像素上匹配的概率。它包含浮点数据并具有相同的大小(宽度/高度与输入)。要提取最可能匹配的位置,必须对结果运行minMaxLoc()函数。
The matchTemplate() function returns a matrix whose values represent the probability of the match on every pixel of the original image. It contains floating-point data and has the same size (width/height as the input). To extract the position of the most likely match, you must run the minMaxLoc() function over the result.
这篇关于iphone opencv - 模板匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!