【问题标题】:Implement angle constraint in the Sobel operator在 Sobel 算子中实现角度约束
【发布时间】:2016-11-11 21:00:23
【问题描述】:

我对这个问题的边缘检测有几个疑问。

1) 我在下面编写的代码试图仅显示那些服从一定大小和方向约束的边缘。 当我使用 numpy 方法时,显示图像的 opencv 函数仅显示黑色。 在show_angle 函数中,当我使用for 循环实现它并使用cv2.imshow 显示图像时。

然后,我使用 numpy 方法检查了输出,并使用返回 Truenp.array_equal 检查了我的 for 循环。 这背后的原因可能是什么?

2) 我无法处理角度限制,我将发布一些不同角度限制的图像。

import cv2

import numpy as np

import matplotlib.pyplot as plt

def show_image(name, img, waitkey=0):
    cv2.namedWindow(name, 0)
    cv2.imshow(name, img)
    cv2.waitKey(waitkey)
    cv2.destroyWindow(name)

img = cv2.imread('hex2.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

shape = img.shape

out_x = cv2.Sobel(img, cv2.CV_16S, 1, 0)    # x gradient
out_y = cv2.Sobel(img, cv2.CV_16S, 0, 1)    # y gradient


out_x = cv2.convertScaleAbs(out_x)
out_y = cv2.convertScaleAbs(out_y)

out_weight = cv2.addWeighted(out_x, 0.5, out_y, 0.5,0)  # x and y weighted


def show_angle(out_weight, mag_final, dir_final, min_mag, theta_min, theta_max):
    """
        Return points based on magnitude and angle constraints
    """


    out_img = np.multiply(
        (
            (mag_final > min_mag) &
            (dir_final > theta_min) &
            (dir_final < theta_max)
        ).astype(int),

        out_weight
    )

    return out_img

def mag_dir():
    """
    Calculate gradient magnitude and direction matrix
    """

    mag = np.sqrt(
                np.add
                     (
                    np.square(out_x) , np.square(out_y)
                      )
                 )

    dir = np.arctan2(out_y, out_x)

    dir = np.multiply(dir, 180)

    print np.min(dir)   # 0
    print np.max(dir)   # 282

    plt.hist(dir,8, (0,360))
    plt.show()

    return mag, dir

mag, dir = mag_dir()



out_img = show_angle(out_weight, mag, dir, 0, 90,120)

plt.imshow(out_img, cmap='gray')
plt.show()

输入图片:

图像直方图:

一些约束的输出:

0 到 90 度

90 到 180 度

谢谢。

【问题讨论】:

  • 没有检查代码,但我的直觉:你不会忽略 0(或接近 0)值的像素。在您的图像中,几乎所有背景的幅度和角度都为 0,因此这可能是填充直方图 0-45 度箱的像素。如果这不是您的问题,请尝试np.multiply(dir, 180/pi) 以度数为单位获取 pi = 3.14159265359...
  • @Micka 感谢您的帮助,我犯了几个错误,我更正了它们。第二个问题已解决,但我仍然无法使用 cv2.imshow 显示图像,这可能是什么原因。

标签: python opencv image-processing computer-vision


【解决方案1】:

好吧,我发现了错误。

我的代码存在三个问题:

1) 在show_angle 函数中,numpy 运算符应该具有大于等于和小于等于比较。

2) 在用于将弧度转换为度数的公式中,我没有除以 pi

3) 我应该将 numpy 矩阵转换为 uint8 类型。

更正后的代码:

import cv2

import numpy as np

import matplotlib.pyplot as plt

import math

def show_image(name, img, waitkey=0):
    cv2.namedWindow(name, 0)
    cv2.imshow(name, img)
    cv2.waitKey(waitkey)
    cv2.destroyWindow(name)

img = cv2.imread('hex2.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

shape = img.shape

out_x = cv2.Sobel(img, cv2.CV_16S, 1, 0)    # x gradient
out_y = cv2.Sobel(img, cv2.CV_16S, 0, 1)    # y gradient


out_x = cv2.convertScaleAbs(out_x)
out_y = cv2.convertScaleAbs(out_y)

out_weight = cv2.addWeighted(out_x, 0.5, out_y, 0.5,0)  # x and y weighted


def show_angle(out_weight, mag_final, dir_final, min_mag, theta_min, theta_max):
    """
        Return points based on magnitude and angle constraints
    """


    out_img = np.multiply(
        (
            # (mag_final > min_mag) &
            (dir_final >= theta_min) &
            (dir_final <= theta_max)
        ).astype(int),

        out_weight
    ).astype('uint8')

    return out_img

def mag_dir():
    """
    Calculate gradient magnitude and direction matrix
    """

    mag = np.sqrt(
                np.add
                     (
                    np.square(out_x) , np.square(out_y)
                      )
                 )

    dir = np.arctan2(out_y, out_x)

    dir = np.multiply(dir, 180/math.pi)

    print np.min(dir)   # 0
    print np.max(dir)   # 89

    # plt.hist(mag,8)
    # plt.show()

    return mag, dir

mag, dir = mag_dir()


out_final = show_angle(out_weight, mag, dir, 1, 60, 90)
show_image("angle", out_final, 0)

【讨论】:

    猜你喜欢
    • 2018-01-31
    • 2014-04-18
    • 2018-01-16
    • 2018-09-16
    • 2021-12-21
    • 2014-02-23
    • 2021-06-02
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多