Horizontal Line detection with OpenCV

前端 未结 5 628
一整个雨季
一整个雨季 2020-11-27 12:33

I am trying to find horizontal and vertical lines from an image which came from a \"document\". The documents are scanned pages from contracts and so the lines look like wha

5条回答
  •  情书的邮戳
    2020-11-27 12:55

    Have you seen a code sample from HoughLinesP function documentation?

    I think you can use it as starting point for your algorithm. To pick horizontal an vertical lines you just need to filter out other lines by line angle.

    UPDATE:

    As I see you need to find not the lines but horizontal an vertical edges on the page. For this task you need to combine several processing steps to get good results.

    For your image I'm able to get good results by combining Canny edge detection with HoughLinesP. Here is my code (I've used python, but I think you see the idea):

    img = cv2.imread("C:/temp/1.png")
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 80, 120)
    lines = cv2.HoughLinesP(edges, 1, math.pi/2, 2, None, 30, 1);
    for line in lines[0]:
        pt1 = (line[0],line[1])
        pt2 = (line[2],line[3])
        cv2.line(img, pt1, pt2, (0,0,255), 3)
    cv2.imwrite("C:/temp/2.png", img)
    

    Result looks like:

提交回复
热议问题