Пример #1
0
def hough_circles(d2image, d2_iscanny=False, radius_samples=200, xy_samples=[200,200], N_return=5,
                return_all=False, **kwargs ):
    if not d2_iscanny:
        data = itktest.canny_edge_detector(d2image, returnNP=True,  **kwargs)
    else:
        data = d2image
    
    data=np.asanyarray(data,  dtype="bool8")
    coords = processing.get_coords(data)
    #We're going to assume that Radius is less than the hypotenuse of the image 
    #This is convenient, because it only allows for circles completely contained in the image
    r_max= np.sqrt(np.float32(np.square(data.shape[0]) + np.square(data.shape[1])))
    x0=np.linspace(0.0, d2image.shape[1], num=xy_samples[0], endpoint=False, retstep=False)
    y0=np.linspace(0.0, d2image.shape[0], num=xy_samples[1], endpoint=False, retstep=False)
    r2_space=np.square(np.linspace(0.0,r_max, num=radius_samples, endpoint=False))
    r_space=np.linspace(0.0,r_max, num=radius_samples, endpoint=True)
    
    #Create a list of index arrays
    x0_ind=np.arange(0, len(x0), dtype="uint16")
    y0_ind=np.arange(0, len(y0), dtype="uint16")
    r2_ind=np.arange(0, len(r2_space),dtype="uint16")

    accum=np.zeros((len(r2_space)+1,len(y0),len(x0)),dtype="uint32")
    #Create a 1-D view
    accum_x=np.ravel(accum)
    
    xy=processing.right_join((x0,y0),sublists=False)
    xy_ind=processing.right_join((x0_ind, y0_ind),sublists=False)
    
    #Choosing to split them for the equation
    x0_eq,y0_eq=xy[:,0], xy[:,1]
    x_ind,y_ind=xy_ind[:,0], xy_ind[:,1]
    #Now going to 
    
    
    for x, y in coords:
        #Perform the transform to this space
        r2=np.square(x-x0_eq)+np.square(y-y0_eq)
        r2_sort=np.searchsorted(r2_space,r2)
        #Get the equivalent coordinates for the output
        #Make the 2-D results into 1-D results for easy indexing of the accumulator
        index=np.ravel_multi_index((r2_sort, y0_ind, x0_ind),accum.shape, mode="raise")
        accum_x[index]+=1
    
    indices=np.argwhere(
        accum >= processing.findNthGreatestValue(accum, count=N_return)
        )
    r2_y_x=np.column_stack((np.sqrt(r2_space[indices[:,0]]),y0[indices[:,1],x0[indices[:,2]]]))
    
        
        
    if return_all:
        return r2_y_x,(accum, x0,y0,r2_space)
    else:
        return r2_y_x
Пример #2
0
def hough_lines(d2image, d2_iscanny=False, angle_samples=181, ro_samples=200, N_return=5,
                return_all=False, **kwargs):
    if not d2_iscanny:
        data = itktest.canny_edge_detector(d2image, returnNP=True,  **kwargs)
    else:
        data = np.asanyarray(d2image,dtype="bool8")
    coords=np.asanyarray(np.argwhere(data),dtype="int16")
    #Number of divisions in 180 degrees to use for angle sampling.
    #Largest value is the hypotenuse of the image dimensions
    romax = np.sqrt(float(data.shape[0] ** 2 + data.shape[1] ** 2))
    #The evenly spaced bins of RO and theta
    rospace=np.linspace(0.0, romax,    num=ro_samples,    endpoint=False, retstep=False)
    theta = np.linspace(0.0, np.pi/2., num=angle_samples, endpoint=True, retstep=False)
    theta_ind=np.arange(0,len(theta),dtype="uint16")
    
    #Generate the accumulator space. X axis
    accum = np.zeros(( len(theta),len(rospace)+1), dtype="uint32")
    accum_x=np.ravel(accum)
    #Something to store extra results
    
    for (x, y) in  coords:
        #Perform the transform to this space
        #For each coordinate, apply theta equation to calculate ro
        ri=x * np.cos(theta) + y * np.sin(theta)
        ri_sort=np.searchsorted(rospace, ri)
        
        if ri_sort.max()>(ro_samples-1):
            print("Wat")
        # print(x,y, "    ",ri_sort.max(), ri_sort.min())
        #Get the equivalent coordinates for the output
        #Make the 2-D results into 1-D results for easy indexing of the accumulator
        index=np.ravel_multi_index((theta_ind,ri_sort),accum.shape, mode="raise")
        accum_x[index]+=1
    #Index locations where the greatest values are. Returned as numpy arrays of point coordinates
    plt.figure(1,figsize=(15,15))
    plt.imshow(accum)
    peakVal=processing.findNthGreatestValue(accum, count=N_return)
    indices=np.argwhere(
        accum >= peakVal
        )
    
    ro_the=np.column_stack((rospace[indices[:,1]],theta[indices[:,0]]))

    if return_all:
        return ro_the, (accum,rospace,theta)
    else:
        return ro_the