微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Python 中绘制霍夫空间

如何解决在 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

但它不会产生输出来可视化霍夫空间中的点,例如此示例中的底部绘图:

enter image description here

我已经按照 this post 中的图片示例进行操作,但出现以下错误

'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

任何帮助将不胜感激!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。