使用OpenCV改进图像中的矩形轮廓检测 [英] Improve rectangle contour detection in image using OpenCV
问题描述
我正在尝试检测给定图像中的矩形框
I am trying to detect the rectangular boxes in the given image
原始图片: 但是图像不足以检测矩形,如何改善图像并检测图像中的所有矩形?
Original image: but the image is not good enough to detect rectangles, how can i improve it and detect all the rectangles in image?
我尝试使用canny边缘检测并应用膨胀,双边过滤器将图像转换为二进制图像,然后输出如下:
I tried to convert the image into binary image using canny edge detection and applied dilation ,bilateral filter then the output is this:
我尝试应用所有morphologyEx,然后sobel无法检测到图像中的所有矩形.如果我能够找到矩形的所有边界,那么我可以使用findcountours来检测所有矩形,但是如何改善图像以检测所有矩形.
I tried to apply all the morphologyEx, sobel then to i was not able to detect all rectangles in the image. If i am able to find all the boundary of rectangle then i can detect all rectangles using find countours but how can i improve image to detect all the rectangles.
下面给出了给定转换的代码
The code for the given conversion is given below
img = cv2.imread("givenimage.png",0)
img = cv2.resize(img,(1280,720))
edges = cv2.Canny(img,100,200)
kernal = np.ones((2,2),np.uint8)
dilation = cv2.dilate(edges, kernal , iterations=2)
bilateral = cv2.bilateralFilter(dilation,9,75,75)
contours, hireracy = cv2.findContours(bilateral,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for i,contour in enumerate(contours):
approx = cv2.approxPolyDP(contour, 0.01*cv2.arcLength(contour,True),True)
if len(approx) ==4:
X,Y,W,H = cv2.boundingRect(approx)
aspectratio = float(W)/H
if aspectratio >=1.2 :
box = cv2.rectangle(img, (X,Y), (X+W,Y+H), (0,0,255), 2)
cropped = img[Y: Y+H, X: X+W]
cv2.drawContours(img, [approx], 0, (0,255,0),5)
x = approx.ravel()[0]
y = approx.ravel()[1]
cv2.putText(img, "rectangle"+str(i), (x,y),cv2.FONT_HERSHEY_COMPLEX, 0.5, (0,255,0))
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
以下程序的输出仅检测8个矩形:
Output of the following program detects only 8 rectangles:
但是我需要检测图像中存在的所有矩形
but i need to detect all the rectangles present in the image
1)我可以为所有黑色像素增加图像的厚度吗?
1) Can I increase the thickness of the image for all the black pixels in this:
2)我可以扩大
推荐答案
这是一种简单的方法:
- 将图像转换为灰度和高斯模糊
- 执行Canny边缘检测
- 查找轮廓并绘制矩形
Canny边缘检测
结果
import cv2
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
canny = cv2.Canny(blurred, 120, 255, 1)
# Find contours
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Iterate thorugh contours and draw rectangles around contours
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.imwrite('canny.png', canny)
cv2.imwrite('image.png', image)
cv2.waitKey(0)
这篇关于使用OpenCV改进图像中的矩形轮廓检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!