gpt4 book ai didi

python-3.x - 使用Python(3.7)在OpenCV(4.2.0)中检测矩形,

转载 作者:行者123 更新时间:2023-12-02 16:34:24 25 4
gpt4 key购买 nike

我正在一个个人项目中,在该项目中,我检测矩形(所有尺寸均相同),然后将这些矩形以相同顺序(上下)放置在列表中,然后使用某些函数处理每个矩形内的信息。下面是我的测试图像。

Test Image

我设法检测到感兴趣的矩形,但是我不断得到其他不需要的矩形。如您所见,我只希望将带有信息(6,9,3)的三个矩形放入一个列表中。

My output when I run my code

我的密码

import cv2

width=700
height=700
y1=0
y2=700
x1=500
x2=700
img=cv2.imread('test.jpg') #read image
img=cv2.resize(img,(width,height)) #resize image
roi = img[y1:y2, x1:x2] #region of interest i.e where the rectangles will be
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) #convert roi into gray
Blur=cv2.GaussianBlur(gray,(5,5),1) #apply blur to roi
Canny=cv2.Canny(Blur,10,50) #apply canny to roi

#Find my contours
contours =cv2.findContours(Canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0]

#Loop through my contours to find rectangles and put them in a list, so i can view them individually later.
cntrRect = []
for i in contours:
epsilon = 0.05*cv2.arcLength(i,True)
approx = cv2.approxPolyDP(i,epsilon,True)
if len(approx) == 4:
cv2.drawContours(roi,cntrRect,-1,(0,255,0),2)
cv2.imshow('Roi Rect ONLY',roi)
cntrRect.append(approx)



cv2.waitKey(0)
cv2.destroyAllWindows()

最佳答案

Contour中有一个名为cv2.contourArea的功能,可以像cv2.contourArea(contours)这样输入轮廓尺寸。您可以使用条件

if cv2.contourArea(contours)>#Rectangle area

通过使用此方法,您的问题将得到解决

关于python-3.x - 使用Python(3.7)在OpenCV(4.2.0)中检测矩形,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61166180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com