简体   繁体   English

在图像内部搜索图像并使用 python 获取坐标

[英]Search image inside of image and take the coodinates using python

I need search one image inside another and get the coodinates.我需要在一张图片中搜索另一张图片并获取坐标。 I tried bellow.我试着吼叫。 But it won't stop if the image couldn't find.It shows wrong coodinates.但如果找不到图像,它不会停止。它显示错误的坐标。 I know, it's showing the maximum value.我知道,它正在显示最大值。 So i tried even adding "if thread... But it's not good solution. Is there any other method more accuracy than this? or pixel matching methods?所以我什至尝试添加“如果线程......但这不是好的解决方案。有没有其他方法比这更准确?或像素匹配方法?

code:代码:

import cv2

method = cv2.TM_SQDIFF_NORMED

# Read the images from the file
small_image = cv2.imread('c3.jpg')
large_image = cv2.imread('screenPls.png')

result = cv2.matchTemplate(small_image, large_image, method)

# We want the minimum squared difference
mn, lk, mnLoc, _ = cv2.minMaxLoc(result)
# Draw the rectangle:
   # Extract the coordinates of our best match
MPx, MPy = mnLoc

# Step 2: Get the size of the template.This is the same size as the match.
trows, tcols = small_image.shape[: 2]

# Step 3: Draw the rectangle on large_image
cv2.rectangle(large_image, (MPx, MPy), (MPx + tcols, MPy + trows), (0, 0, 255), 2)

# Display the original image with the rectangle around the match.
cv2.imshow('output', large_image)
# The image is only displayed
cv2.waitKey(0)

You just fave to use a threshold for squareError with an "if".您只是喜欢使用带有“if”的 squareError 阈值。 Even when you use an small image which is exactly within the big image, the error won't be 0. So do it like this.即使当你使用一个正好在大图像内的小图像时,错误也不会为 0。所以这样做吧。

Lats take the threshold as 0.01 lats取阈值为0.01

if mn < 0.01:
   MPx, MPy = mnLoc

   # Step 2: Get the size of the template.This is the same size as the match.
   trows, tcols = small_image.shape[: 2]

   # Step 3: Draw the rectangle on large_image
   cv2.rectangle(large_image, (MPx, MPy), (MPx + tcols, MPy + trows), (0, 0, 255), 2)

   # Display the original image with the rectangle around the match.
   cv2.imshow('output', large_image)
   # The image is only displayed
   cv2.waitKey(0)

That will work那可行

However the best way to search inside images is "Machine Learning"然而,搜索内部图像的最佳方式是“机器学习”

This is computationally expensive, but it will identify objects most accurately.这在计算上是昂贵的,但它会最准确地识别对象。 But there are limitations.但也有局限性。

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

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