python - 识别模板图像中的矩形

标签 python opencv image-processing rectangles

所以我试图识别一个已经由边界框定义的区域。示例:Bowser

这些图像中这些矩形内的某些区域是白色,一些区域是黑色,并且它们中的大多数大小完全不同。这些图像之间的唯一共同特征是红色矩形:enter image description here

本质上,我想做的是创建一个随机生成的模因机器人,该机器人将随机源图像放置在这些矩形定义的区域中。我已经拥有大量带有预定义区域的图像,并带有这些红色矩形。我想以某种方式使过程自动化,目前必须为每个模板定义每个调整大小的形状和偏移量。因此,我需要做的是识别矩形内的区域,并使其返回定义的调整大小形状和放置源图像所需的偏移量。

我应该怎么做?我应该在OpenCV中使用某些内容还是要训练CNN?我只是真的在寻找一个正确的方向,因为我对解决这个问题的最佳方法非常困惑。

最佳答案

我认为OpenCV可以做到。以下是所需步骤的简短示例。阅读代码中的注释以获取更多详细信息。

import cv2
import numpy as np

img = cv2.imread("1.jpg")

#STEP1: get only red color (or the bounding box color) in the image
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of red color in HSV
lower_red = np.array([0,50,50])
upper_red = np.array([0,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_red, upper_red)
red_only = cv2.bitwise_and(img,img, mask= mask)

#STEP2: find contour
gray_img = cv2.cvtColor(red_only,cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray_img,1,255,cv2.THRESH_BINARY)

_,contours,_ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

#max contour in the image is the box you want
areas = [cv2.contourArea(c) for c in contours]
sorted_areas = np.sort(areas)
cnt=contours[areas.index(sorted_areas[-1])]
r = cv2.boundingRect(cnt)
cv2.rectangle(img,(r[0],r[1]),(r[0]+r[2],r[1]+r[3]),(0,255,0),3)

cv2.imshow("img",img)
cv2.imshow("red_only",red_only)
cv2.imshow("thresh",thresh)

cv2.waitKey()
cv2.destroyAllWindows()

enter image description here

enter image description here

enter image description here

关于python - 识别模板图像中的矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54066840/

相关文章:

成员函数的python静态重载

c - 使用 OpenCV 数据结构时堆损坏

c++ - 如何在 visual studio 2010 C++ 中使用所需的 .dll 和 lib (opencv) 文件制作 .exe 文件包

python - 通过 scipy.misc.imsave 将图像保存为 tif 时如何指定 dpi?

c - 如何在不丢失主要轮廓的情况下将字母的灰度图像转换为二进制图像?

python - 在python中的azure databricks笔记本中发送电子邮件(outlook作为服务器)?

python - 是否可以使用Python开发网络浏览器?

python - ValueError : could not broadcast input array from shape (300, 300,3) 变成形状 (300,300)

python - Python 新手(编程)和数据存储

image-processing - Haar 训练时正样本和负样本使用多少图像?