簡體   English   中英

Python,Cv2,numpy可檢測另一張圖片中的圖片

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

我正在使用這些代碼來檢測小圖片是否是大圖片的一部分。 (或者認為,如果可以在大圖片中找到小圖片)

這是大圖片

這是大圖片

這是小圖片

在此處輸入圖片說明

它可以正常工作,並給我小圖片開始的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

但是,當我要在大圖片中指定檢測區域時-也就是說,如果可以在大圖片的特定部分找到小圖片-它將失敗。

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

錯誤提示:

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

什么地方出了錯? 謝謝。

似乎[y,y1:x,x1]是正確的格式。 因此,應歸功於@ZdaR。

僅發布一個使用矩形SMALL的編輯示例。

這是大圖片

大

這是小圖片

小

目標區域是

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

它們是BIG中的左上角和右下角。

編寫如下代碼:

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