首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sobel滤波器在Sobel中的实现

Sobel滤波器在Sobel中的实现
EN

Stack Overflow用户
提问于 2018-10-23 11:05:48
回答 1查看 878关注 0票数 1

我尝试用Sobel_X函数实现convolve2d过滤器。

我将此函数的结果进行了比较:

代码语言:javascript
复制
from scipy.signal import convolve2d 
from scipy import misc
from skimage.exposure import rescale_intensity
import cv2
import numpy as np
#https://www.pyimagesearch.com/2016/07/25/convolutions-with-opencv-and-python/ 


def convolve(image, kernel):
    # grab the spatial dimensions of the image, along with
    # the spatial dimensions of the kernel
    (iH, iW) = image.shape[:2]
    (kH, kW) = kernel.shape[:2]
#         print("Kh,Kw", kernel.shape[:2])

    # allocate memory for the output image, taking care to
    # "pad" the borders of the input image so the spatial
    # size (i.e., width and height) are not reduced
    pad = (kW - 1) // 2
#         print("pad", pad)
    image = cv2.copyMakeBorder(image, pad, pad, pad, pad,
        cv2.BORDER_REPLICATE)
#         self.imshow(image, "padded image")
    output = np.zeros((iH, iW), dtype="float32")
    # loop over the input image, "sliding" the kernel across
    # each (x, y)-coordinate from left-to-right and top to
    # bottom
    for y in np.arange(pad, iH + pad):
        for x in np.arange(pad, iW + pad):
            # extract the ROI of the image by extracting the
            # *center* region of the current (x, y)-coordinates
            # dimensions
            roi = image[y - pad:y + pad + 1, x - pad:x + pad + 1]

            # perform the actual convolution by taking the
            # element-wise multiplicate between the ROI and
            # the kernel, then summing the matrix
            k = (roi * kernel).sum()

            # store the convolved value in the output (x,y)-
            # coordinate of the output image
            output[y - pad, x - pad] = k
#             self.imshow(output, "padded image")
    # rescale the output image to be in the range [0, 255]
    output = rescale_intensity(output, in_range=(0, 255))
    output = (output * 255).astype("uint8")

    # return the output image
    return output

下面是要比较的Sobel_X内核和代码。

代码语言:javascript
复制
sobelX = np.array((
        [-1, 0, 1],
        [-2, 0, 2],
        [-1, 0, 1]), dtype="int")]

testim=misc.face(gray=True)
convolved_func=convolve(testim, sobelX)
convolved_np=convolve2d(testim, sobelX, boundary='symm', mode='same')

cv2.imshow("Face", np.hstack((convolved_func,np.array(convolved_np, dtype="uint8"))))
cv2.waitKey(0)
cv2.destroyAllWindows()

正如您可以看到的,这里的结果是完全不同的,我无法了解如何实现这些过滤器来获得相同的结果。赖特,我应该以某种方式改变过滤器函数,还是在numpy中有一些特殊的东西来实现它呢?我试图像在示例中那样,为scipy创建函数,但是结果是相同的或值的(我有黑色的图像)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-23 11:17:20

你会得到稍微不同的结果。进行阈值处理以删除所有小于0的数字。

代码语言:javascript
复制
convolved_np[convolved_np<0]=0 

这会给你一些相似的东西,但还是不一样。一些人工制品出现了。我认为这些函数不同,这就是为什么我得到了一些不同的结果。也许有一些错误,所以如果你能在这个答案中加上一些,我会很感激的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52947492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档