OpenCV: get Hough accumulator value?

前端 未结 2 2064
野性不改
野性不改 2020-12-30 08:33

Is it possible to get the accumulator value along with rho and theta from a Hough transform?

I ask because I\'d like to differentiate betwe

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 08:49

    Although this is an old question, I had the same problem, so I might as well put up my solution. The threshold in houghlines() returns 1 for any point that cleared the threshold for votes. The solution is to run houghlines() for every threshold value (until there are no more votes) and add up the votes in another array. In python (maybe with other languages too) when you have no more votes, it throws an error, so use try/except.

    Here is an example in python. The array I used was for rho values of -199 to 200 with a max vote of less than 100. You can play around with those constants to suit your needs. You may need to add a line to convert the source image to grayscale.

    import matplotlib.pyplot as plt
    
    import cv2
    
    import math
    
    
    
    ############ make houghspace array ############
    
    houghspace = []
    
    c = 0
    
    height = 400
    
    while c <= height:
    
        houghspace.append([])
    
        cc = 0
    
        while cc <= 180:
    
            houghspace[c].append(0)
    
            cc += 1
    
        c+=1
    
    
    
    ############ do transform ############
    
    
    degree_tick = 1 #by how many degrees to check 
    
    total_votes = 1 #votes counter
    
    highest_vote = 0 #highest vote in the array
    
    
    
    while total_votes < 100:
    
        img = cv2.imread('source.pgm')
    
        edges = cv2.Canny(img,50,150,apertureSize = 3)
    
        lines = cv2.HoughLines(edges,1,math.pi*degree_tick/180,total_votes)
    
    
    
        try:
    
            for rho,theta in lines[0]:
    
    
    
    
    
                a = math.cos(theta)
    
                b = math.sin(theta)
    
                x1 = int((a*rho) + 1000*(-b))
    
                y1 = int((b*rho) + 1000*(a))
    
                x2 = int((a*rho) - 1000*(-b))
    
                y2 = int((b*rho) - 1000*(a))
    
                cv2.line(img,(x1,y1),(x2,y2),(50,200,255),2)
    
            #################add votes into the array################
    
            deradian = 180/math.pi #used to convert to degrees
    
            for rho,theta in lines[0]:
    
                degree = int(round(theta*deradian))
    
                rho_pos = int(rho - 200) 
    
                houghspace[rho_pos][degree] += 1 
        #when lines[0] has no votes, it throws an error which is caught here
    
        except:     
    
            total_votes = 999 #exit loop
    
    
        highest_vote = total_votes
    
        total_votes += 1
        del lines
    
    
    
    ########### loop finished ###############################
    print highest_vote
    
    
    
    #############################################################
    
    ################### plot the houghspace ###################
    
    
    maxy = 200 #used to offset the y-axis
    
    miny = -200 #used to offset the y-axis
    
    #the main graph
    
    fig = plt.figure(figsize=(10, 5))
    
    ax = fig.add_subplot(111)
    
    ax.set_title('Houghspace')
    
    plt.imshow(houghspace, cmap='gist_stern')
    
    ax.set_aspect('equal')
    
    plt.yticks([0,-miny,maxy-miny], [miny,0,maxy])
    
    #the legend
    cax = fig.add_axes([0, 0.1, 0.78, 0.8])
    
    cax.get_xaxis().set_visible(False)
    
    cax.get_yaxis().set_visible(False)
    
    cax.patch.set_alpha(0)
    
    cax.set_frame_on(False)
    
    plt.colorbar(orientation='vertical')
    
    #plot
    
    plt.show()
    

提交回复
热议问题