一:归一化
归一化是减小图片的像素值,方便后期数据处理
原理:(1)找到原本样本数据X的最小值Min及最大值Max (2)y=(x-Min)/(Max-Min)
1:读取图片
import cv2
import matplotlib.pyplot as plt
import numpy as np
image = plt.imread('77.jpg')
plt.imshow(image)
2:归一化原理实现
Max=np.max(image)
Min=np.min(image)
result = np.zeros(image.shape, dtype=np.float32) #创建一个零矩阵
result=(image-Min)/(Max-Min) #计算得到的新的数值储存在result中
plt.imshow(result)
3:结果
归一化之后,发先图片i的像素值介于[0,1]之间。
二:提取图片通道
1:分离三个通道为r,g,b
image=cv2.imread("77.jpg")#读取要处理的图片
r,g,b= cv2.split(image) #分离出图片的B,R,G颜色通道
2:为了达到提取某一通道的效果,创建两个零矩阵,与之合成。
zeros1 = np.zeros(R.shape,dtype="uint8")#创建与image相同大小的零矩阵
zeros2 = np.zeros(R.shape,dtype="uint8")#创建与image相同大小的零矩阵
3:通道分离后的图片
re_image1=cv2.merge([r,zeros1,zeros2])
fg,ax=plt.subplots()
plt.subplot(1,3,1), plt.title('r')
plt.imshow(re_image1),plt.axis('off')
re_image2=cv2.merge([zeros1,g,zeros2])
fg,ax=plt.subplots()
plt.subplot(1,3,2), plt.title('g')
plt.imshow(re_image2),plt.axis('off')
re_image3=cv2.merge([zeros1,zeros2,b])
fg,ax=plt.subplots()
plt.subplot(1,3,3), plt.title('b')
plt.imshow(re_image3),plt.axis('off')