Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

python - Houghline method of openCV is giving only 1 line as a output

I am using houghLine method for line detection. But it gives only 1 line as an output which is I think is the line with the largest votes. I tried a solution from In opencv using houghlines prints only one line but this is taking a lot of time and is not working.

My code is:

folderInPath = "DevanagariHandwrittenCharacterDataset/Test"
folderOutPath = "DevanagariHandwrittenCharacterDataset/Test_Lines"


for folderPath in os.listdir(folderInPath):
    inPath = "DevanagariHandwrittenCharacterDataset/Test/" + folderPath
    #os.mkdir(os.path.join(folderOutPath, folderPath+'_lines'))
    outPath = os.path.join(folderOutPath, folderPath+'_lines')
    dirs = "DevanagariHandwrittenCharacterDataset/Test/" + folderPath
    for imagePath in os.listdir(dirs):
        # imagePath contains name of the image for eg. 46214.png 
        inputPath = os.path.join(inPath, imagePath)
        # inputPath contains the full directory name for eg. character_1_ka/46214.png|

        # Reading the required image in which operations are to be done.  
        # Make sure that the image is in the same directory in which this python program is 
        img = cv2.imread(inputPath)
    
        # Convert the img to grayscale 
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        
        # Apply edge detection method on the image 
        edges = cv2.Canny(gray,50,150,apertureSize = 3)
    
        # This returns an array of r and theta values 
        lines = cv2.HoughLines(edges,1,np.pi/180, 5) 
        
        for line in lines:
            # The below for loop runs till r and theta values are in the range of the 2d array 
            for r,theta in line:
                # Stores the value of cos(theta) in a 
                a = np.cos(theta) 

                # Stores the value of sin(theta) in b 
                b = np.sin(theta)

                # x0 stores the value rcos(theta) 
                x0 = a*r 

                # y0 stores the value rsin(theta) 
                y0 = b*r 

                # x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
                x1 = int(x0 + 1000*(-b))

                # y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
                y1 = int(y0 + 1000*(a))

                # x2 stores the rounded off value of (rcos(theta)+1000sin(theta)) 
                x2 = int(x0 - 1000*(-b))

                # y2 stores the rounded off value of (rsin(theta)-1000cos(theta)) 
                y2 = int(y0 - 1000*(a)) 

                # cv2.line draws a line in img from the point(x1,y1) to (x2,y2). (0,0,255) denotes the colour of the line to be  
                #drawn. In this case, it is red. 
                cv2.line(img,(x1,y1), (x2,y2), (0,0,255),2) 

                # fullOutPath contains the path of the output 
                fullOutPath = os.path.join(outPath, 'lines_'+imagePath)

                # All the changes made in the input image are finally written on a new image houghlines.jpg 
                cv2.imwrite(fullOutPath, img) 
    print("Done " + folderPath)

For information, my image input is a character from the Hindi language of 32 x 32 pixels. Someone with any suggestions or solution for this.

I am attaching one of the image in my dataset. There are several image like this.Image

question from:https://stackoverflow.com/questions/65856931/houghline-method-of-opencv-is-giving-only-1-line-as-a-output

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code is generally correct, except that you didn't choose the correct parameters for the Line detection lines = cv2.HoughLines(edges,1,np.pi/180, 5).

replacing this instruction by: lines = cv2.HoughLines(edges,1,np.pi/90, 18)

will give you this result:

Result

Note: if you want to detect more or less lines, you have to change the parameters accordingly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...