python 自己编写实现Sobel算子

本文详细介绍了使用Sobel算子进行边缘检测的过程,并解决了在matplotlib和OpenCV中显示图像颜色不一致的问题。通过调整代码,实现了正确显示彩色及灰度图像,同时对比了不同边缘检测结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(‘D:\my_matlab\lenna.jpg’)
d = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
sp = d.shape
print(sp)
height = sp[0]
weight = sp[1]
sx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
sy = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
dSobel = np.zeros((height,weight))
dSobelx = np.zeros((height,weight))
dSobely = np.zeros((height,weight))
Gx = np.zeros(d.shape)
Gy = np.zeros(d.shape)
for i in range(height-2):
for j in range(weight-2):
Gx[i + 1, j + 1] = abs(np.sum(d[i:i + 3, j:j + 3] * sx))
Gy[i + 1, j + 1] = abs(np.sum(d[i:i + 3, j:j + 3] * sy))
dSobel[i+1, j+1] = (Gx[i+1, j+1]*Gx[i+1,j+1] + Gy[i+1, j+1]*Gy[i+1,j+1])**0.5
dSobelx[i+1, j+1] = np.sqrt(Gx[i+1, j+1])
dSobely[i + 1, j + 1] = np.sqrt(Gy[i + 1, j + 1])
‘’’
cv2.imshow(‘a’, img)
cv2.imshow(‘b’, d)
cv2.imshow(‘c’, np.uint8(dSobel))
cv2.imshow(‘d’, np.uint8(dSobelx))
cv2.imshow(‘e’, np.uint8(dSobely))
cv2.waitKey(0)
cv2.destroyAllWindows()
‘’’
a = np.uint8(dSobel)
b = np.uint8(dSobelx)
c = np.uint8(dSobel)
img = img[:, :, ::-1]

plt.subplot(321), plt.imshow(img), plt.title(‘f’)
plt.subplot(322), plt.imshow(d), plt.title(‘g’)
plt.subplot(323), plt.imshow(a), plt.title(‘w’)
plt.subplot(324), plt.imshow(b), plt.title(‘q’)
plt.subplot(325), plt.imshow©, plt.title(‘e’)
plt.show()

问题
当用matplotlib 将多幅图片显示出来颜色除了问题?
那是因为用opencv读取的图片是BGR, 而matplotlib是RGB,所以用img = img[:, :, ::-1]将BGR转化为RGB。

出错图片
在这里插入图片描述
转化后的图片
在这里插入图片描述还有个问题 就是灰度图像为甚么也变了? 明天再说!

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread(‘D:\my_matlab\lenna.jpg’)
d = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
sp = d.shape
print(sp)
height = sp[0]
weight = sp[1]
sx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
sy = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
dSobel = np.zeros((height,weight))
dSobelx = np.zeros((height,weight))
dSobely = np.zeros((height,weight))
Gx = np.zeros(d.shape)
Gy = np.zeros(d.shape)
for i in range(height-2):
for j in range(weight-2):
Gx[i + 1, j + 1] = abs(np.sum(d[i:i + 3, j:j + 3] * sx))
Gy[i + 1, j + 1] = abs(np.sum(d[i:i + 3, j:j + 3] * sy))
dSobel[i+1, j+1] = (Gx[i+1, j+1]*Gx[i+1,j+1] + Gy[i+1, j+1]*Gy[i+1,j+1])**0.5
dSobelx[i+1, j+1] = np.sqrt(Gx[i+1, j+1])
dSobely[i + 1, j + 1] = np.sqrt(Gy[i + 1, j + 1])
‘’’
cv2.imshow(‘a’, img)
cv2.imshow(‘b’, d)
cv2.imshow(‘c’, np.uint8(dSobel))
cv2.imshow(‘d’, np.uint8(dSobelx))
cv2.imshow(‘e’, np.uint8(dSobely))
cv2.waitKey(0)
cv2.destroyAllWindows()
‘’’
a = np.uint8(dSobel)
b = np.uint8(dSobelx)
c = np.uint8(dSobel)
img = img[:, :, ::-1]

plt.subplot(321), plt.imshow(img), plt.title(‘f’)
plt.subplot(322), plt.imshow(d, cmap=plt.cm.gray), plt.title(‘g’)
plt.subplot(323), plt.imshow(a, cmap=plt.cm.gray), plt.title(‘w’)
plt.subplot(324), plt.imshow(b, cmap=plt.cm.gray), plt.title(‘q’)
plt.subplot(325), plt.imshow(c, cmap=plt.cm.gray), plt.title(‘e’)
plt.show()

改了之后的代码 ,灰度图像处理是plt.imshow(d, cmap=plt.cm.gray) 改成这样
在这里插入图片描述效果图,但是不知道什么原理!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值