Python How to detect vertical and horizontal lines in an image with HoughLines with OpenCV?

前端 未结 3 1361
无人共我
无人共我 2020-12-08 03:09

I m trying to obtain a threshold of the calibration chessboard. I cant detect directly the chessboard corners as there is some dust as i observe a micro chessboard. I try se

相关标签:
3条回答
  • 2020-12-08 03:29

    You are using too small value for rho.

    Try the below code:-

    import numpy as np
    import cv2
    
    gray = cv2.imread('lines.jpg')
    edges = cv2.Canny(gray,50,150,apertureSize = 3)
    cv2.imwrite('edges-50-150.jpg',edges)
    minLineLength=100
    lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)
    
    a,b,c = lines.shape
    for i in range(a):
        cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
        cv2.imwrite('houghlines5.jpg',gray)
    

    Note, the change in rho value, pi value and maxLineGap to reduce outliers.

    Input Image

    Edges Image

    Output Image

    Miscellaneous - Tips for Beginners

    1. A lot of Computer Vision algorithms assume certain assumptions, well, in how the input should be. When building Proof-of-Concept, always try to view intermediate inputs you generate before applying such algorithms.

    2. For quick hack, if an algorithm accepts some parameters, use a for loop on possible values of these parameters and see how the results varies. Link to an answer on how to quickly generate these possible values.

    3. To really understand the algorithm, read on wiki or even better sources where if necessary. And then again/still do the above hack(point 2). It will further clear your understanding.

    0 讨论(0)
  • 2020-12-08 03:31

    I would rather write this as a comment but unfortunately I can't. You should change the minLineLength and minLineGap. Or what if its just sqaures that you have to find, I would get all the lines and check the angles between them to get lines only along squares. I have worked with HoughLineP before and it is pretty much based on the above two arguments. Additionally, try using Bilateral filtering. I really helps when the sharpening using median filter doesn't help.

    Bilateral Filter

    0 讨论(0)
  • 2020-12-08 03:35

    in images processing they are some roles you have to go through such as filters before you go for edges detection, in your condition the dust is just a noise that you have to remove by filter, use gausse or blure after that use thresholding and then use canny for edges and in opencv they are cornere detection you can use, or you can just go for key point after threshholding if i'm not wrong.. try to do those steps and see the resulte

    0 讨论(0)
提交回复
热议问题