AIR

1.代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cv2 #引入Opencv库
import numpy as np #用np代替引入的numpy
import mathplotlib.pyplot as plt
img=cv2.imread('1.tif') #读取所要测试的图片
H=img.shape[0] #获取图片的高度
W=img.shape[1] #获取图片的宽度
img15=np.zeros((H,W,3),np.uint8) #生成零矩阵;15*15领域平滑后的图像
img15T=np.zeros((H,W,3),np.unit8) #15*15领域平滑再通过阈值选取后的图像,阈值为最高亮度的25%
img15T2=np.zeros((H,W,3),np.unit8) #15*15领域平滑再通过阈值选取后的图像,阈值为255的25%
maxPix=0 #最高亮度值
#运用循环
for i in range(H):
for j in range(W):
sum=0
count=0
for m in range(-7,8):
for n in range(-7,8):
if 0<=i+m<H and 0<=j+n<W:
count+=1
sum+=img[i+m,j+n,0]
img15[i,j,0]=sum // conut
img15[i,j,1]=img15[i,j,0]
img15[i,j,2]=img15[i,j,0]
if img15[i,j,0]>maxPix:
maxPix=img15[i,j,0]
#处理阈值
for i in range(H):
for j in range(W):
if img15[i,j,0]>maxPix*0.25:
img15T[i,j]=[255,255,255]
if img15[i,j,0]>63:
img15T2[i,j]=[255,255,255]
#原图
plt.subplot(2,2,1) #指将图片画到一个2行2列图中的从左到右从上到下的第一个位置
plt.axis('off') #关闭坐标轴
plt.title('Original image') #定义图片题目
plt.imshow(img) #展示图片
#15*15邻域
plt.subplot(2,2,2)
plt.axis('off')
plt.title('15*15 smoothing')
plt.imshow(img15)
#阈值处理后
plt.subplot(2,2,3)
plt.axis('off')
plt.title('After maxpixel*25% Threshold')
plt.imshow(img15T)
#阈值处理之后
plt.subplot(2,2,4)
plt.axis('off')
plt.title('After 255*25% Threshold')
plt.imshow(img15T2)

plt.show()

2.运行结果
所用原图
在这里插入图片描述
运行结果

在这里插入图片描述
只是对代码的简单理解,还要努力学习。


 Comments


Blog content follows the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License

Use Material X as theme , total visits times .
载入天数...载入时分秒...