图像处理六:预处理方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yql_617540298/article/details/84886726

一、标准化处理与归一化

        对图像做数据预处理,最常见的对图像预处理方法有两种:

(1)白化处理(图像标准化处理);

        图像标准化是将数据通过去均值实现中心化的处理,根据凸优化理论与数据概率分布相关知识,数据中心化符合数据分布规律,更容易取得训练之后的泛化效果,数据标准化是数据预处理的常见方法之一。

(2)归一化处理。

       归一化不会改变图像本身的信息存储,取值范围从0~255已经转化为0~1之间。

源码:

import numpy as np
import cv2

pic_path = 'F:/a.jpg'

def normalization(input):
    pic = cv2.imread(input)
    pic = pic.astype(np.float32)
    # zero-center
    pic -= [np.mean(pic[..., 0]), np.mean(pic[..., 1]), np.mean(pic[..., 2])]
    # normalize
    pic /= [np.std(pic[..., 0]), np.std(pic[..., 1]), np.std(pic[..., 2])]

    print(pic[..., 0])
    pic *= (pic>0)
    pic *= 100
    pic = pic * (pic<=255) + 255 * (pic>255)
    pic = pic.astype(np.uint8)
    print(pic[..., 0])
    #cv2.imshow('', pic)
    cv2.imwrite('F:/b.jpg',pic)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == '__main__':
    # 均值0中心化(zero-center), 规范化(normalize)
    normalization(pic_path)

二、直方图与均衡化

       直方图在图像处理中非常重要,利用图像直方图对对比度进行调整的方法。

       基本思想:把原始图的直方图变换为均匀分布的形式,这样就增加了像素灰度值的动态范围,从而达到增强图像整体对比度的效果。

1. skimage

skimage.exposure.histogram(imagenbins=256)

skimage程序自带了一些示例图片:

2. 绘制直方图

import numpy as np
from skimage import exposure,data

#image = skimage.io.imread('F:/a.jpg')
image =data.camera()*1.0
hist1=np.histogram(image, bins=2)   #用numpy包计算直方图
hist2=exposure.histogram(image, nbins=2)  #用skimage计算直方图
print(hist1)
print(hist2)

from skimage import data
import matplotlib.pyplot as plt
img=data.camera()
plt.figure("hist")
arr=img.flatten()
n, bins, patches = plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red')  
plt.show()

from skimage import data
import matplotlib.pyplot as plt
img=data.chelsea()
ar=img[:,:,0].flatten()
plt.hist(ar, bins=256, normed=1,facecolor='r',edgecolor='r',hold=1)
ag=img[:,:,1].flatten()
plt.hist(ag, bins=256, normed=1, facecolor='g',edgecolor='g',hold=1)
ab=img[:,:,2].flatten()
plt.hist(ab, bins=256, normed=1, facecolor='b',edgecolor='b')
plt.show()

3. 直方图均衡化

from skimage import data,exposure
import matplotlib.pyplot as plt
import skimage.io
#img=data.chelsea()
img = skimage.io.imread('F:/a.jpg')
plt.figure("hist",figsize=(8,8))

arr=img.flatten()
plt.subplot(221)
plt.imshow(img,plt.cm.gray)  #原始图像
plt.subplot(222)
plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red') #原始图像直方图

img1=exposure.equalize_hist(img)
arr1=img1.flatten()
plt.subplot(223)
plt.imshow(img1,plt.cm.gray)  #均衡化图像
plt.subplot(224)
plt.hist(arr1, bins=256, normed=1,edgecolor='None',facecolor='red') #均衡化直方图

plt.show()

猜你喜欢

转载自blog.csdn.net/yql_617540298/article/details/84886726
今日推荐