Python实现哈夫变换

该博客介绍了如何使用OpenCV库进行图像预处理,包括高斯模糊和Canny边缘检测,然后详细阐述了霍夫变换的实现过程,用于从边缘检测结果中找出直线。代码示例展示了读取、处理RAW图像文件并将其写回的过程。
摘要由CSDN通过智能技术生成
import numpy as np
import cv2
def Read_raw(filename,w,h,m):
    #初始化image
    image=[]
    for i in range(m*h):
        image.append([])
        for j in range(w):
            image[i].append(255)
    #读取raw
    with open(filename,'rb') as f:
        data=f.read()
    #写入raw
    for i in range(m*h):
        for j in range(w):
            image[i][j]=data[i*w+j]
    print('读取文件{0}成功'.format(filename))
    return image

def Write_raw(filename,image):
    #将数组image转为bytes
    bt=bytearray()
    for i in range(len(image)):
        for j in range(len(image[i])):
            bt.append(image[i][j])
    #写入raw
    with open(filename,'wb') as f:
        f.write(bt)
    f.close()
    print('写入文件{0}成功'.format(filename))

def Hough(x,y):
    all_b=[]
    for t in range(180):
        temp_theta=np.pi/(t+1)
        all_b.append(0)
        for i in range(1):
            temp_b=y[i]-np.tan(temp_theta)*x[i]
            '''
            print('b=',temp_b)
            print('k=',round(np.tan(temp_theta),2))
            print('x=',x[i],'y=',y[i])
            '''
            
def threshold(filename):
    im=cv2.imread(filename,0)
    im=cv2.GaussianBlur(im,(5,5),0)
    canny=cv2.Canny(im,50,150)
    temp_x=[]
    temp_y=[]
    for i in range(len(canny)):
        for j in range(len(canny[i])):
            if canny[i][j]==255:
                temp_x.append(i)
                temp_y.append(j)
    return temp_x,temp_y

x,y=threshold('lena1.jpg')
Hough(x,y)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值