【问题标题】:Python, Cv2, numpy to detect a picture in another picturePython、Cv2、numpy检测另一张图片中的一张图片
【发布时间】:2017-09-14 12:30:54
【问题描述】:

我正在使用这些代码来检测小图片是否是大图片的一部分。 (或者认为,如果小图能在大图中找到)

这是大图

这是小图

它工作正常,并给我小图片开始位置的 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 错误:断言失败 (corrsize.height

出了什么问题?谢谢。

【问题讨论】:

  • big_pic[0:0, 238:220] 好像错了。
  • 请包含完整的回溯。这些错误很神秘,因为它们是从较低级别的模块中引发的......
  • @MarkK big_pic[x1:x2, y1:y2],而不是big_pic[x1:y1, x2:y2]
  • 不,我猜它一定是big_pic[y1:y2, x1:x2] @Rightleg
  • 但是我们在这里专门讨论numpy 语法。如果这是观点问题,那么 OP 没有遇到错误。

标签: python image opencv numpy


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多