如何检测2D数组是否在另一个2D数组中?(How to detect if a 2D array is inside another 2D array?)

编程入门 行业动态 更新时间:2024-10-24 16:23:19
如何检测2D数组是否在另一个2D数组中?(How to detect if a 2D array is inside another 2D array?)

所以在堆栈溢出成员的帮助下,我有以下代码:

data = "needle's (which is a png image) base64 code goes here" decoded = data.decode('base64') f = cStringIO.StringIO(decoded) image = Image.open(f) needle = image.load() while True: screenshot = ImageGrab.grab() haystack = screenshot.load() if detectImage(haystack, needle): break else: time.sleep(5)

我写了下面的代码来检查针是否在大海捞针:

def detectImage(haystack, needle): counter = 0 for hayrow in haystack: for haypix in hayrow: for needlerow in needle: for needlepix in needlerow: if haypix == needlepix: counter += 1 if counter == 980: #the needle has 980 pixels return True else: return False

问题是我在第3行遇到了这个错误:'PixelAccess'对象不可迭代

有人告诉我,将针和干草堆复制成numpy / scipy阵列会更容易。 然后我可以使用一个函数来检查2D阵列针是否在2D数组haystack中。

我需要帮助:

1)将这些数组转换为numpy数组。

2)检查2D阵列针是否在2D阵列干草堆内的功能。 我的功能不起作用。

这些是图像: 针: 草垛:

So with the help of a stack-overflow member, I have the following code:

data = "needle's (which is a png image) base64 code goes here" decoded = data.decode('base64') f = cStringIO.StringIO(decoded) image = Image.open(f) needle = image.load() while True: screenshot = ImageGrab.grab() haystack = screenshot.load() if detectImage(haystack, needle): break else: time.sleep(5)

I've written the following code to check if the needle is in the haystack:

def detectImage(haystack, needle): counter = 0 for hayrow in haystack: for haypix in hayrow: for needlerow in needle: for needlepix in needlerow: if haypix == needlepix: counter += 1 if counter == 980: #the needle has 980 pixels return True else: return False

The issue is that I get this error for line 3: 'PixelAccess' object is not iterable

It was suggested to me that it would be easier to copy both needle and haystack into a numpy/scipy array. And then I can just use a function that checks to see if the 2D array needle is inside the 2D array haystack.

I need help with:

1) converting those arrays to numpy arrays.

2) a function that checks to see if the 2D array needle is inside the 2D array haystack. My function doesn't work.

These are the images: Needle: Haystack:

最满意答案

您可以在opencv中使用matchTemplate来检测位置:

import cv2 import numpy as np import pylab as pl needle = cv2.imread("needle.png") haystack = cv2.imread("haystack.jpg") diff = cv2.matchTemplate(haystack, needle, cv2.TM_CCORR_NORMED) x, y = np.unravel_index(np.argmax(diff), diff.shape) pl.figure(figsize=(12, 8)) im = pl.imshow(haystack[:,:, ::-1]) ax = pl.gca() ax.add_artist(pl.Rectangle((y, x), needle.shape[1], needle.shape[0], transform=ax.transData, alpha=0.6))

这是输出:

在此处输入图像描述

You can use matchTemplate in opencv to detect the position:

import cv2 import numpy as np import pylab as pl needle = cv2.imread("needle.png") haystack = cv2.imread("haystack.jpg") diff = cv2.matchTemplate(haystack, needle, cv2.TM_CCORR_NORMED) x, y = np.unravel_index(np.argmax(diff), diff.shape) pl.figure(figsize=(12, 8)) im = pl.imshow(haystack[:,:, ::-1]) ax = pl.gca() ax.add_artist(pl.Rectangle((y, x), needle.shape[1], needle.shape[0], transform=ax.transData, alpha=0.6))

here is the output:

enter image description here

更多推荐

本文发布于:2023-08-04 09:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1414833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   组中   array   detect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!