Skip to content

Commit 70fe01a

Browse files
authored
Add files via upload
1 parent c2231b7 commit 70fe01a

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#encoding:utf-8
2+
#By:Eastmount CSDN 2021-08-20
3+
import cv2
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
#读取图片
8+
src = cv2.imread('na.png', cv2.IMREAD_UNCHANGED)
9+
img = cv2.cvtColor(src,cv2.COLOR_BGR2RGB)
10+
11+
# 转化为灰度图
12+
Grayimg = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
13+
14+
# 1、消除椒盐噪声:
15+
# 中值滤波器
16+
median = cv2.medianBlur(Grayimg, 5)
17+
# 消除噪声图
18+
cv2.imshow("median-image", median)
19+
20+
# 2、直方图均衡化:
21+
equalize = cv2.equalizeHist(median)
22+
cv2.imshow('hist', equalize)
23+
24+
# 3、二值化处理:
25+
# 阈值为140
26+
ret, binary = cv2.threshold(equalize, 127, 255,cv2.THRESH_BINARY)
27+
cv2.imshow("binary-image",binary)
28+
cv2.waitKey(0)
29+
30+
#设置卷积核
31+
kernel = np.ones((10,10), np.uint8)
32+
close = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
33+
34+
#图像开运算
35+
kernel = np.ones((10,10), np.uint8)
36+
open1 = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
37+
38+
#显示图像
39+
cv2.imshow("src", src)
40+
cv2.imshow("result", close)
41+
42+
#等待显示
43+
cv2.waitKey(0)
44+
cv2.destroyAllWindows()
45+
46+
#图像开运算
47+
kernel = np.ones((10,10), np.uint8)
48+
gradient = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel)
49+
50+
# Sobel算子 XY方向求梯度 cv2.CV_8U
51+
x = cv2.Sobel(close, cv2.CV_32F, 1, 0, ksize = 3) #X方向
52+
y = cv2.Sobel(close, cv2.CV_32F, 0, 1, ksize = 3) #Y方向
53+
#absX = cv2.convertScaleAbs(x) # 转回uint8
54+
#absY = cv2.convertScaleAbs(y)
55+
#Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
56+
gradient = cv2.subtract(x, y)
57+
sobel = cv2.convertScaleAbs(gradient)
58+
cv2.imshow('Sobel', sobel)
59+
cv2.waitKey(0)
60+
61+
#循环显示图形
62+
titles = [ 'source', 'gray', 'median', 'equalize', 'binary', 'close', 'open', 'gradient', 'sobel']
63+
images = [img, Grayimg, median, equalize, binary, close, open1, gradient, sobel]
64+
for i in range(9):
65+
plt.subplot(3, 3, i+1), plt.imshow(images[i], 'gray')
66+
plt.title(titles[i])
67+
plt.xticks([]),plt.yticks([])
68+
plt.show()

0 commit comments

Comments
 (0)