Skip to content

Commit 9628e67

Browse files
authored
Add files via upload
1 parent c9f605d commit 9628e67

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

blog42-ruihua/blog42-03-sobel.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#encoding:utf-8
2+
#By:Eastmount CSDN 2021-07-19
3+
import cv2
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
#读取图像
8+
img = cv2.imread('lena.png')
9+
lenna_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
10+
11+
#灰度化处理图像
12+
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13+
14+
#Sobel算子
15+
x = cv2.Sobel(grayImage, cv2.CV_16S, 1, 0) #对x求一阶导
16+
y = cv2.Sobel(grayImage, cv2.CV_16S, 0, 1) #对y求一阶导
17+
absX = cv2.convertScaleAbs(x)
18+
absY = cv2.convertScaleAbs(y)
19+
Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
20+
21+
#用来正常显示中文标签
22+
plt.rcParams['font.sans-serif']=['SimHei']
23+
24+
#显示图形
25+
titles = [u'原始图像', u'Sobel算子']
26+
images = [lenna_img, Sobel]
27+
for i in range(2):
28+
plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray')
29+
plt.title(titles[i])
30+
plt.xticks([]),plt.yticks([])
31+
plt.show()

0 commit comments

Comments
 (0)