我有一个随机改变颜色的图像,所以有时 OpenCV 会识别对象,有时它不是:
我的代码:
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
grey_temp = cv2.cvtColor(temp, cv2.COLOR_BGR2GRAY)
w, h = grey_temp.shape[::-1]
res = cv2.matchTemplate(grey_img, grey_temp, cv2.TM_CCOEFF_NORMED)
print(res)
threshold = 0.8;
loc = np.where(res >= threshold)
print(loc)
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (255,0,0), 3)
cv2.imshow('win', img)
cv2.waitKey()
与 emguCv 类似的代码:
Mat background = CvInvoke.Imread(file,
Emgu.CV.CvEnum.ImreadModes.AnyColor);//Convert Image to ReducedGrayscale2
Mat find = CvInvoke.Imread(compered, Emgu.CV.CvEnum.ImreadModes.AnyColor);
Mat match = CvInvoke.Imread(compered, Emgu.CV.CvEnum.ImreadModes.AnyColor);
CvInvoke.MatchTemplate(background, find, match, TemplateMatchingType.CcoeffNormed);
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
match.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
if (maxValues[0] > 0.7)
{
Console.WriteLine("found");
Rectangle match1 = new Rectangle(maxLocations[0],);
match.Draw(match1, new Bgr(Color.Red), 3);
}
CvInvoke.Imshow("Image1", background);
CvInvoke.WaitKey(0);
(顺便说一句,更改阈值无济于事)
识别示例:
我想知道你是否知道如何在不依赖颜色、灰度或他的位置的情况下跟踪照片中的相同图案对象(该对象具有一些相似的共享属性,因此在人眼中它被称为容易跟踪)。
我尝试使用 emguCv for c sharp 进行相同的进度,以检查结果是否有任何不同,但没有意外,没有区别。如果您能建议我如何管理以跟踪主图像中的模板,我将不胜感激。提前致谢!