简体   繁体   English

Python,Cv2,numpy可检测另一张图片中的图片

[英]Python, Cv2, numpy to detect a picture in another picture

I am using these codes to detect if a small picture is a part of a big picture. 我正在使用这些代码来检测小图片是否是大图片的一部分。 (or take it as, if the small picture can be found in the big picture) (或者认为,如果可以在大图片中找到小图片)

this is the BIG picture 这是大图片

这是大图片

this is the SMALL picture 这是小图片

在此处输入图片说明

It works fine and give me the x_coordinate of where the small picture starts. 它可以正常工作,并给我小图片开始的x_coordinate。

import cv2
import numpy as np

big_pic = cv2.imread("c:\\big.jpg")
small_pic = cv2.imread('c:\\small.jpg')
res = cv2.matchTemplate(big_pic,small_pic,cv2.TM_CCOEFF_NORMED)

threshold = 0.99
loc = np.where (res >= threshold)
x_coordinate = list(loc[0])

print x_coordinate

However, when I want to specify the detection area in the big picture – that is, if the small picture can be found in certain part of the big picture – it fails. 但是,当我要在大图片中指定检测区域时-也就是说,如果可以在大图片的特定部分找到小图片-它将失败。

big_pic = cv2.imread("c:\\big.jpg")
target_area = big_pic[0:0, 238:220]

small_pic = cv2.imread('c:\\small.jpg')
res = cv2.matchTemplate(target_area,small_pic,cv2.TM_CCOEFF_NORMED)

threshold = 0.99
loc = np.where (res >= threshold)
x_coordinate = list(loc[0])

print x_coordinate

The error says: 错误提示:

OpenCV Error: Assertion failed (corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1) in cv::crossCorr, file ......\\opencv-3.1.0\\modules\\imgproc\\src\\templmatch.cpp, line 658 Traceback (most recent call last): File "C:\\Python27\\finding picture.py", line 8, in res = cv2.matchTemplate(target_area,small_pic,cv2.TM_CCOEFF_NORMED) cv2.error: ......\\opencv-3.1.0\\modules\\imgproc\\src\\templmatch.cpp:658: error: (-215) corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1 in function cv::crossCorr OpenCV错误:在cv :: crossCorr,文件...... \\中断言失败(corrsize.height <= img.rows + templ.rows-1 && corrsize.width <= img.cols + templ.cols-1) opencv-3.1.0 \\ modules \\ imgproc \\ src \\ templmatch.cpp,第658行回溯(最近一次调用):文件“ C:\\ Python27 \\ find picture.py”,第8行,res = cv2.matchTemplate(target_area ,small_pic,cv2.TM_CCOEFF_NORMED)cv2.error:...... \\ opencv-3.1.0 \\ modules \\ imgproc \\ src \\ templmatch.cpp:658:错误:(-215)corrsize.height <= img.rows + templ.rows-1 && corrsize.width <= img.cols + templ.cols-函数cv :: crossCorr中的1

What went wrong? 什么地方出了错? Thank you. 谢谢。

It seems [y,y1 : x,x1] is the right format. 似乎[y,y1:x,x1]是正确的格式。 So the credit shall go to @ZdaR. 因此,应归功于@ZdaR。

Just to post an edited example, using a rectangle shape SMALL. 仅发布一个使用矩形SMALL的编辑示例。

this is the BIG picture 这是大图片

大

this is the SMALL picture 这是小图片

小

The target area is, 目标区域是

[0,0] [300,220]. # Reading as [x, y] and [x1, y1]. 

They are the top left point and bottom right point in the BIG. 它们是BIG中的左上角和右下角。

Made the code as following: 编写如下代码:

big_pic = cv2.imread("c:\\big.jpg")

target_area = big_pic[0:220, 0:300]   # [y,y1 : x,x1]   # it returns a result.
target_area = big_pic[0:300, 0:220]   # [x,x1 : y,y1]   # it doesn’t return a result.

small_pic = cv2.imread('c:\\small.jpg')
res = cv2.matchTemplate(target_area,small_pic,cv2.TM_CCOEFF_NORMED)

threshold = 0.99
loc = np.where (res >= threshold)
x_coordinate = list(loc[0])

print x_coordinate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM