python - 在没有 opencv 的情况下使用 python 实现 Sobel 运算符

标签 python image vision sobel

给定一个灰度 8 位图像(像素强度值为 0 - 255 的二维数组),我想在图像上实现 Sobel 运算符(掩码)。下面的 Sobel 函数基本上围绕给定像素循环,对像素应用以下权重: enter image description here

enter image description here

然后应用给定的公式:

enter image description here

我正在尝试通过此链接实现公式: http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import Image


def Sobel(arr,rstart, cstart,masksize, divisor):
  sum = 0;
  x = 0
  y = 0

  for i in range(rstart, rstart+masksize, 1):
    x = 0
    for j in range(cstart, cstart+masksize, 1):
        if x == 0 and y == 0:
            p1 = arr[i][j]
        if x == 0 and y == 1:
            p2 = arr[i][j]
        if x == 0 and y == 2:
            p3 = arr[i][j]
        if x == 1 and y == 0:
            p4 = arr[i][j]
        if x == 1 and y == 1:
            p5 = arr[i][j]           
        if x == 1 and y == 2:
            p6 = arr[i][j]
        if x == 2 and y == 0:
            p7 = arr[i][j]
        if x == 2 and y == 1:
            p8 = arr[i][j]
        if x == 2 and y == 2:
            p9 = arr[i][j]
        x +=1
    y +=1
  return np.abs((p1 + 2*p2 + p3) - (p7 + 2*p8+p9)) + np.abs((p3 + 2*p6 + p9) - (p1 + 2*p4 +p7)) 


def padwithzeros(vector, pad_width, iaxis, kwargs):
   vector[:pad_width[0]] = 0
   vector[-pad_width[1]:] = 0
   return vector

im = Image.open('charlie.jpg')
im.show()
img = np.asarray(im)
img.flags.writeable = True
p = 1
k = 2
m = img.shape[0]
n = img.shape[1]
masksize = 3
img = np.lib.pad(img, p, padwithzeros) #this function padds image with zeros to cater for pixels on the border.
x = 0
y = 0
for row in img:
  y = 0
  for col in row:
    if not (x < p or y < p or y > (n-k) or x > (m-k)):
        img[x][y] = Sobel(img, x-p,y-p,masksize,masksize*masksize)
    y = y + 1
  x = x + 1

img2 = Image.fromarray(img)
img2.show()

给定这张灰度 8 位图像

enter image description here

我在应用函数时得到了这个:

enter image description here

但应该得到这个:

enter image description here

我已经用 python 实现了其他高斯滤波器,我不确定我哪里出错了?

最佳答案

紧贴您的代码正在执行的操作,一种优雅的解决方案是使用 scipy.ndimage.filters.generic_filter()使用上面提供的公式。

import numpy as np
from scipy.ndimage.filters import generic_filter
from scipy.ndimage import imread

# Load sample data
with np.DataSource().open("http://i.stack.imgur.com/8zINU.gif", "rb") as f:
    img = imread(f, mode="I")

# Apply the Sobel operator
def sobel_filter(P):
    return (np.abs((P[0] + 2 * P[1] + P[2]) - (P[6] + 2 * P[7] + P[8])) +
            np.abs((P[2] + 2 * P[6] + P[7]) - (P[0] + 2 * P[3] + P[6])))
G = generic_filter(img, sobel_filter, (3, 3))

在样本图像上运行此程序大约需要 400 毫秒。作为比较,convolve2d 的性能约为 6.5 毫秒。

关于python - 在没有 opencv 的情况下使用 python 实现 Sobel 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39035510/

相关文章:

python - 如何从字母选择中返回字母表中最早的字母?

python - 没有错误 : But its not indexing faces and not generating the face id's

python - Pandas DataFrame 搜索是线性时间还是常数时间?

html - 使文本完全出现在 div 中

c++ - OpenCV 3.0 无法加载神经网络

python - 如何创建一个等于 15 分钟前的 DateTime?

Javascript 无法在 Joomla 文章中工作

python - 让文本显示在子图图像前面

c++ - OpenCV:将 cvGoodFeaturesToTrack 与 C++ mat 变量一起使用

c# - WCF 服务和 COBOL VM