image - Python, Cv2, numpy to detect a picture in another picture -
i using these codes detect if small picture part of big picture. (or take as, if small picture can found in big picture)
this big picture
this small picture
it works fine , give me x_coordinate of small picture starts.
import cv2 import numpy 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 want specify detection area in big picture – is, if small picture can found in part of big picture – 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
what went wrong? thank you.
it seems [y,y1 : x,x1] right format. credit shall go @zdar.
just post edited example, using rectangle shape small.
this big picture
this small picture
the target area is,
[0,0] [300,220]. # reading [x, y] , [x1, y1].
they top left point , bottom right point in big.
made code following:
big_pic = cv2.imread("c:\\big.jpg") target_area = big_pic[0:220, 0:300] # [y,y1 : x,x1] # returns result. target_area = big_pic[0:300, 0:220] # [x,x1 : y,y1] # doesn’t return 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
Comments
Post a Comment