首页 > 解决方案 > 在 Python 中绘制霍夫空间

问题描述

我使用以下代码在超声图像上生成霍夫线:

image = mpimg.imread("img.png")
data = np.array(image)
num_lines = 0

gray_image = cv2.cvtColor((image*255).astype(np.uint8), cv2.COLOR_RGB2GRAY)
blurred_image = cv2.GaussianBlur(gray_image, (9, 9), 0)
edges_image = cv2.Canny(blurred_image, 50, 120)

def draw_lines(img, houghLines, color=[0, 255, 0], thickness=2):
    global num_lines
    for line in houghLines:
        num_lines += 1
        for rho,theta in line:
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a*rho
            y0 = b*rho
            x1 = int(x0 + 1000*(-b))
            y1 = int(y0 + 1000*(a))
            x2 = int(x0 - 1000*(-b))
            y2 = int(y0 - 1000*(a))
 
            cv2.line(img,(x1,y1),(x2,y2),color,thickness)   
                
 
def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
    return cv2.addWeighted(initial_img, α, img, β, λ) #blending or transparency

rho_resolution = 1
theta_resolution = np.pi/180
threshold = 145

h_lines = cv2.HoughLines(edges_image, rho_resolution, theta_resolution, threshold)
 
    
#draw lines    
h_lines_image = np.zeros_like(image) #array of zeros
draw_lines(h_lines_image, h_lines)
image_hough = weighted_img(h_lines_image,image) #blending or transparency

但它不会产生输出来可视化霍夫空间中的点,例如本例中的底部图:在此处输入图像描述

我已经按照这篇文章中的图像示例进行了操作,但出现了以下错误:

'ValueError: The truth value of an array with more than one element is ambiguous.'

为了

for j, pixel in enumerate(row):   
            if pixel != val : continue

或者

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

为了

feature_space[i, d] += 1

任何帮助将不胜感激!

标签: pythonpython-3.xcomputer-visionedge-detectionhough-transform

解决方案


推荐阅读