在OpenCV中使用Hough变换检测垂直线

5
我正在尝试使用OpenCV(Python)中的Hough变换来去除方框(垂直和水平线)。问题是没有任何垂直线被检测到。我已经尝试查找轮廓和层次结构,但是在这张图像中有太多轮廓,我不知道该如何使用它们。
经过查看相关的帖子,我已经尝试调整阈值和rho参数,但这并没有起到作用。我附上代码以获取更多细节。为什么Hough变换无法在图像中找到垂直线呢?欢迎提出任何解决此任务的建议。谢谢。
输入图像:enter image description here 经Hough变换处理后的图像:enter image description here 轮廓图像:enter image description here
import cv2
import numpy as np
import pdb


img = cv2.imread('/home/user/Downloads/cropped/robust_blaze_cpp-300-0000046A-02-HW.jpg')

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 140, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,0,255), 2)

edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 5
maxLineGap = 100
lines = cv2.HoughLinesP(edges,rho=1,theta=np.pi/180,threshold=100,minLineLength=minLineLength,maxLineGap=maxLineGap)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('probHough.jpg',img)

1
问题的一部分可能是初始阈值太低了。检查中间图像。我并没有真正回答你最初的问题,但我发布了一种可行的替代方法。 - Dan Mašek
1个回答

24
说实话,与其找线条,我宁愿寻找白色的盒子。
  1. Preparation

    import cv2
    import numpy as np
    
  2. Load the image

    img = cv2.imread("digitbox.jpg", 0)
    
  3. Binarize it, so that both the boxes and the digits are black, rest is white

    _, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
    cv2.imwrite('digitbox_step1.png', thresh)
    

    Step 1 -- thresholded input

  4. Find contours. In this example image, it's fine to just look for external contours.

    _, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
  5. Process the contours, filtering out any with too small an area. Find convex hull of each contour, create a mask of all areas outside the contour. Store the bounding boxes of each found contour, sorted by x coordinate.

    mask = np.ones_like(img) * 255
    
    boxes = []
    
    for contour in contours:
        if cv2.contourArea(contour) > 100:
            hull = cv2.convexHull(contour)
            cv2.drawContours(mask, [hull], -1, 0, -1)
            x,y,w,h = cv2.boundingRect(contour)
            boxes.append((x,y,w,h))
    
    boxes = sorted(boxes, key=lambda box: box[0])
    
    cv2.imwrite('digitbox_step2.png', mask)
    

    Step 2 -- the mask

  6. Dilate the mask (to shrink the black parts), to clip off any remains the the gray frames.

    mask = cv2.dilate(mask, np.ones((5,5),np.uint8))
    
    cv2.imwrite('digitbox_step3.png', mask)
    

    Step 3 -- dilated mask

  7. Fill all the masked pixels with white, to erase the frames.

    img[mask != 0] = 255
    
    cv2.imwrite('digitbox_step4.png', img)
    

    Step 4 - cleaned up input

  8. Process the digits as you desire -- i'll just draw the bounding boxes.

    result = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    
    for n,box in enumerate(boxes):
        x,y,w,h = box
        cv2.rectangle(result,(x,y),(x+w,y+h),(255,0,0),2)
        cv2.putText(result, str(n),(x+5,y+17), cv2.FONT_HERSHEY_SIMPLEX, 0.6,(255,0,0),2,cv2.LINE_AA)
    
    cv2.imwrite('digitbox_step5.png', result)
    

    Enumerated bounding boxes

整个脚本在一个文件中的代码:
import cv2
import numpy as np

img = cv2.imread("digitbox.jpg", 0)

_, thresh = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

mask = np.ones_like(img) * 255
boxes = []

for contour in contours:
    if cv2.contourArea(contour) > 100:
        hull = cv2.convexHull(contour)
        cv2.drawContours(mask, [hull], -1, 0, -1)
        x,y,w,h = cv2.boundingRect(contour)
        boxes.append((x,y,w,h))

boxes = sorted(boxes, key=lambda box: box[0])

mask = cv2.dilate(mask, np.ones((5,5),np.uint8))

img[mask != 0] = 255

result = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

for n,box in enumerate(boxes):
    x,y,w,h = box
    cv2.rectangle(result,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.putText(result, str(n),(x+5,y+17), cv2.FONT_HERSHEY_SIMPLEX, 0.6,(255,0,0),2,cv2.LINE_AA)

cv2.imwrite('digitbox_result.png', result)

1
这太完美了。这是一个很好的例子,可以理解如何在opencv中使用凸包和轮廓。谢谢!! - Dheeraj Peri
你好,你能告诉我如何找到水平线的数量吗?请告诉我。 - Amit Saini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接