对一种生成图像角点图像的算法进行了仿真,结果令人满意

2024-10-01 17:36:05 发布

您现在位置:Python中文网/ 问答频道 /正文

模拟了一种生成图像角点图像的算法,结果不正确 HALCON软件的sobelèu dir操作符将返回方向角图像。我用Python根据指令重写了这个算法,但是我不知道如何计算最终的灰度值

算法描述: 1在图像的水平和垂直方向上计算sobel核卷积,即得到图像的和导数,分别为Ex和Ey 2根据Ex和Ey得到振幅图像,即边缘检测图像 三。根据Ex和Ey的正负条件,将灰度值赋给指定的范围,生成具有振幅角的方向图像

第一步和第二步没有问题,但是我的算法在第三步是不正确的。以下是原文第三步的说明:

你知道吗---------------------------------------------------------------------- 边缘方向以边缘方向返回,并且以2度步长存储,即,在数学上正意义上并且相对于水平轴的x度边缘方向在边缘方向图像中存储为x/2。此外,还考虑了强度变化的方向。让Ex,Ey表示图像梯度。然后将以下边缘方向返回为r/2:


强度增加| Ex/Ey |


边缘方向r


自下而上| 0/+| 0 |


从右下到左上|-/+|]0,90[


从右到左|-/0 | 90


从右上到左下|-/-|]90180[


自上而下|-/0 | 180


从左上到右下|+/-|]180270[


从左到右|+/-| 270


从左下到右上|+/+| 180


我知道这个算法是用角度方向来代替原来的切线角度,从而使角度得到更有效的利用功能。但是我不知道如何确定0到90,90到180之间的灰度值。学生:根据大小?你知道吗

    def sobel_dir(Image, FilterType, Size):
        '''
        使用Sobel算子检测边缘(振幅和方向)
        :param Image: 输入图像
        :param FilterType: 过滤器类型
        :param Size: 掩码尺寸
        :return EdgeAmplitude: 边缘振幅(梯度大小)图像
        :return EdgeDirection: 图像边缘方向
        '''
        Image = Image.astype(np.float_)
        hor = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
        ver = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])
        Size = Size * 2 + 1

        if Size == 3:
            pass
        elif Size > 3 and 'binomial' in FilterType:
            Image = Binomial2D(Image, Size)
        elif Size > 3:
            Image = cv2.GaussianBlur(Image, (Size, Size), 0)

        s1 = cv2.filter2D(Image, -1, hor)
        s2 = cv2.filter2D(Image, -1, ver)
        EdgeDirection = np.zeros(Image.shape, np.float_)
        Height, Width = s1.shape

                if 'sum_sqrt' in FilterType:
            EdgeAmplitude = np.sqrt(np.power(s1, 2) + np.power(s2, 2)) / 4
            # EdgeDirection = np.arctan2(-s2, -s1)
        elif 'sum_abs' in FilterType:
            EdgeAmplitude = np.add(np.abs(s1), np.abs(s2)) / 4
            EdgeAmplitudeMax = np.max(EdgeAmplitude)
            EdgeAmplitudeMin = np.min(EdgeAmplitude)
            EdgeAmplitudeNor = (EdgeAmplitude - EdgeAmplitudeMin) / (EdgeAmplitudeMax - EdgeAmplitudeMin) * 90


            for y in range(Height):
                for x in range(Width):
                    if s2[y, x] == 0 and s1[y, x] == 0:
                        EdgeDirection[y, x] = 255
                        continue
                    if s2[y, x] == 0 and s1[y, x] > 0:
                        EdgeDirection[y, x] = 0
                        continue
                    elif s2[y, x] < 0 and s1[y, x] > 0:
                        # EdgeDirection[y, x] = random.uniform(1, 89)  右下到左上
                        EdgeDirection[y, x] = abs(EdgeAmplitudeNor[y, x])
                        continue
                    elif s2[y, x] < 0 and s1[y, x] == 0:
                        EdgeDirection[y, x] = 90
                        continue
                    elif s2[y, x] < 0 and s1[y, x] < 0:
                        # EdgeDirection[y, x] = random.uniform(91, 179) 右上到左下
                        EdgeDirection[y, x] = 90 + abs(EdgeAmplitudeNor[y, x])
                        continue
                    elif s2[y, x] == 0 and s1[y, x] < 0:
                        EdgeDirection[y, x] = 180
                        continue
                    elif s2[y, x] > 0 and s1[y, x] < 0:
                        # EdgeDirection[y, x] = random.uniform(1, 45) 左上到右下
                        EdgeDirection[y, x] = 180 + abs(EdgeAmplitudeNor[y, x])
                        continue
                    elif s2[y, x] > 0 and s1[y, x] == 0:
                        EdgeDirection[y, x] = 270
                        continue
                    elif s2[y, x] > 0 and s1[y, x] > 0:
                        # EdgeDirection[y, x] = random.uniform(1, 45) 左下到右上
                        EdgeDirection[y, x] = 270 + abs(EdgeAmplitudeNor[y, x])
                        continue

        return EdgeAmplitude.astype(np.uint8), EdgeDirection.astype(np.uint8)

我找到一张随机图片进行测试,计算出的振幅值与Halcon软件完全相同,但在角度图像中,我不知道如何将这些灰度值分配到0和90之间,90和180之间。。。你知道吗

帮助。。。你知道吗


Tags: and图像image算法sizenpabs方向

热门问题