# 使用Numpy和Opencv完成图像的基本数据分析（Part III）

• 导入图像并观察其属性
• 拆分图层
• 灰度处理
• 对像素值使用逻辑运算符
• 使用逻辑运算符进行掩码

## 强度变换|Intensity Transformation

```%matplot libinline
import imageio
import matplotlib.pyplot as plt
import warnings
import matplotlib.cbook
plt.imshow(pic);
plt.axis('off');```

## 图像底片|Image Negative

S = T(r)

S =(L-1)-r

```negative =255- pic # neg = (L-1) - imgplt.figure(figsize= (6,6))
plt.imshow(negative);
plt.axis('off');```

## 对数变换|Log transformation

s = c *log(r + 1)

```%matplot libinline
import imageio
import numpy as np
gray=lambdargb:np.dot(rgb[...,:3],[0.299,0.587,0.114])
gray=gray(pic)'''
log transform
-> s = c*log(1+r)So, we calculate constant c to estimate s
-> c = (L-1)/log(1+|I_max|)'''
max_=np.max(gray)
def log_transform():
return(255/np.log(1+max_))*np.log(1+gray)
plt.figure(figsize=(5,5))
plt.imshow(log_transform(),cmap=plt.get_cmap(name='gray'))
plt.axis('off');```

## 伽马校正| Gamma Correction

Vo = Vi ^(1 / G)

```import imageio
import matplotlib.pyplot as plt
# Gamma encoding
gamma=2.2
# Gamma < 1 ~ Dark ; Gamma > 1 ~ Bright
gamma_correction=((pic/255)**(1/gamma))
plt.figure(figsize=(5,5))
plt.imshow(gamma_correction)
plt.axis('off');```

## 伽马校正的原因|Reason for Gamma Correction

• 对比度拉伸（Contrast Stretching）
• 强度切片（Intensity-Level Slicing）
• 位平面切片（Bit-Plane Slicing）

## 卷积|Convolution

• 卷积：(N-F)/ S + 1

• 32-31 + 1 = 30

```%%time
import numpy as np
import imageio
import matplotlib.pyplot as plt
from scipy.signal import convolve2d
def Convolution(image, kernel):
conv_bucket= []for d in range(image.ndim):
conv_channel= convolve2d(image[:,:,d], kernel,
mode="same", boundary="symm")
conv_bucket.append(conv_channel)
return np.stack(conv_bucket, axis=2).astype("uint8")kernel_sizes= [9,15,30,60]
for k, ax in zip(kernel_sizes, axs):
kernel =np.ones((k,k))
kernel /=np.sum(kernel)
ax.imshow(Convolution(pic, kernel));
ax.set_title("Convolved By Kernel: {}".format(k));
ax.set_axis_off();
Wall time: 43.5 s```

87 篇文章9 人订阅

0 条评论