Scipy中的Sobel滤波器实现 [英] Sobel filter implementation in scipy

查看:126
本文介绍了Scipy中的Sobel滤波器实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用convolve2d函数在scipy中实现Sobel_X过滤器.

I tried to implement the Sobel_X filter in scipy with convolve2d function.

我将此函数的结果与之比较:

I compared with the results from this function:

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内核和要比较的代码.

Here are the Sobel_X Kernel and code to compare.

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中执行一些特殊操作,好吗? 我试图在示例,但结果相同或值得(我有黑色图像).

As you can see here the results are entirely different I can't get how to implement these filters to get the same results. Should I somehow change the filter function or maybe there some special things in numpy to implement it, wright? I tried to make the function for scipy as in this and that examples, but the results the same or worth (I've got black image).

推荐答案

您将获得略有不同的结果. 进行阈值处理以删除所有小于0的数字.

You will get results slightly different. Do thresholding to remove all numbers which are less than 0.

convolved_np[convolved_np<0]=0 

那会给你类似的东西,但还是不一样. 出现了. 我认为这些功能有所不同,这就是为什么我得到了一些不同的结果的原因.也许有一些错误,所以如果您可以在此答案中添加一些内容,我将不胜感激.

That will give you something similar, still not the same. Some artifacts appeared. I think these functions differ, that's why I have got a bit different results. Maybe there are some mistakes, so if you can add some to this answer, I will appreciate it.

这篇关于Scipy中的Sobel滤波器实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆