python自定义编写调用函数实现hough变换和canny算子的实用操作

多种变换实现图像检测处理并输出写入文件。
001、自定义函数hough变换
Hough_transform.py文件
函数源码:

#filename _rename_as_Hough_transform
import numpy as np
import math

class Hough_transform:
    def __init__(self, img, angle, step=5, threshold=135):
        '''

        :param img: 输入的图像
        :param angle: 输入的梯度方向矩阵
        :param step: Hough 变换步长大小
        :param threshold: 筛选单元的阈值
        '''
        self.img = img
        self.angle = angle
        self.y, self.x = img.shape[0:2]
        self.radius = math.ceil( math.sqrt( self.y ** 2 + self.x ** 2 ) )
        self.step = step
        self.vote_matrix = np.zeros(
            [math.ceil( self.y / self.step ), math.ceil( self.x / self.step ), math.ceil( self.radius / self.step )] )
        self.threshold = threshold
        self.circles = []

    def Hough_transform_algorithm(self):
        '''
        按照 x,y,radius 建立三维空间,根据图片中边上的点沿梯度方向对空间中的所有单
        元进行投票。每个点投出来结果为一折线。
        :return:  投票矩阵
        '''
        print( 'Hough_transform_algorithm' )

        for i in range( 1, self.y - 1 ):
            for j in range( 1, self.x - 1 ):
                if self.img[i][j] > 0:
                    y = i
                    x = j
                    r = 0
                    while y < self.y and x < self.x and y >= 0 and x >= 0:
                        self.vote_matrix[math.floor( y / self.step )][math.floor( x / self.step )][
                            math.floor( r / self.step )] += 1
                        y = y + self.step * self.angle[i][j]
                        x = x + self.step
                        r = r + math.sqrt( (self.step * self.angle[i][j]) ** 2 + self.step ** 2 )
                    y = i - self.step * self.angle[i][j]
                    x = j - self.step
                    r = math.sqrt( (self.step * self.angle[i][j]) ** 2 + self.step ** 2 )
                    while y < self.y and x < self.x and y >= 0 and x >= 0:
                        self.vote_matrix[math.floor( y / self.step )][math.floor( x / self.step )][
                            math.floor( r / self.step )] += 1
                        y = y - self.step * self.angle[i][j]
                        x = x - self.step
                        r = r + math.sqrt( (self.step * self.angle[i][j]) ** 2 + self.step ** 2 )

        return self.vote_matrix

    def Select_Circle(self):
        '''
        按照阈值从投票矩阵中筛选出合适的圆,并作极大化抑制,这里的非极大化抑制我采
        用的是邻近点结果取平均值的方法,而非单纯的取极大值。
        :return: None
        '''
        print( 'Select_Circle' )

        houxuanyuan = []
        for i in range( 0, math.ceil( self.y / self.step ) ):
            for j in range( 0, math.ceil( self.x / self.step ) ):
                for r in range( 0, math.ceil( self.radius / self.step ) ):
                    if self.vote_matrix
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海宝7号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值