Matplotlib-图片灰度处理(3种方法)

图片的灰度化处理

灰度化处理就是将一幅色彩图像转化为灰度图像的过程。彩色图像分为R,G,B三个分量,分别显示出红绿蓝等各种颜色,灰度化就是使彩色的R,G,B分量相等的过程。灰度值大的像素点比较亮(像素值最大为255,为白色),反之比较暗(像素最下为0,为黑色)。

灰度化的算法主要有以下3种:

1、最大值法 

2、平均值法

3、加权平均法

 

In [1]:

import numpy as np

import pandas as pd

#显示图片

import matplotlib.pyplot as plt

#读写图片,使用scipy中misc.imread()/imsave()

import scipy.misc as misc

#魔法指令

%matplotlib inline

In [2]:

display(misc.face())

face = misc.face()

display(face.shape)

plt.imshow(face)

print('此时,图片是彩色的,对应的数据是三维!')

array([[[121, 112, 131],

        [138, 129, 148],

        [153, 144, 165],

        ...,

        [119, 126,  74],

        [131, 136,  82],

        [139, 144,  90]],

 

       [[ 89,  82, 100],

        [110, 103, 121],

        [130, 122, 143],

        ...,

        [118, 125,  71],

        [134, 141,  87],

        [146, 153,  99]],

 

       [[ 73,  66,  84],

        [ 94,  87, 105],

        [115, 108, 126],

        ...,

        [117, 126,  71],

        [133, 142,  87],

        [144, 153,  98]],

 

       ...,

 

       [[ 87, 106,  76],

        [ 94, 110,  81],

        [107, 124,  92],

        ...,

        [120, 158,  97],

        [119, 157,  96],

        [119, 158,  95]],

 

       [[ 85, 101,  72],

        [ 95, 111,  82],

        [112, 127,  96],

        ...,

        [121, 157,  96],

        [120, 156,  94],

        [120, 156,  94]],

 

       [[ 85, 101,  74],

        [ 97, 113,  84],

        [111, 126,  97],

        ...,

        [120, 156,  95],

        [119, 155,  93],

        [118, 154,  92]]], dtype=uint8)

(768, 1024, 3)

此时,图片是彩色的,对应的数据是三维!

In [4]:

print('热身:图片灰色处理,gray很重要!')

face_g = misc.face(gray=True)

display(face_g.shape)

#cmap = 'gray'实现灰度化处理

plt.imshow(face_g,cmap = 'gray')

print('经过gray=True之后,图片是灰色的,对应的数据是二维!')

热身:图片灰色处理,gray很重要!

(768, 1024)

经过gray=True之后,图片是灰色的,对应的数据是二维!

In [5]:

im_data = misc.face()

#(768, 1024, 3)转换成(768, 1024)

#图片灰度处理

print('图片灰度处理(总共有3种方法)')

print('原始图片:')

plt.imshow(im_data)

图片灰度处理(总共有3种方法)

原始图片:

Out[5]:

<matplotlib.image.AxesImage at 0x7f6ae95fb8d0>

In [6]:

#使用,最大值法

print('第1种:最大值法(max)')  #如果使用最小值(min),会很暗,数值偏向0,黑

im_data1 = im_data.max(axis=2)

display(im_data1.shape)

plt.imshow(im_data1,cmap = 'gray')

第1种:最大值法(max)

(768, 1024)

Out[6]:

<matplotlib.image.AxesImage at 0x7f6aeaec4e10>

In [7]:

#使用,平均值法

print('第2种:平均值法(mean)')  #稍微暗些

im_data2 = im_data.mean(axis=-1)

display(im_data2.shape)

plt.imshow(im_data2,cmap = 'gray')

第2种:平均值法(mean)

(768, 1024)

Out[7]:

<matplotlib.image.AxesImage at 0x7f6aeaf444e0>

In [8]:

#使用,加权平均法

print('第3种:加权平均法(红绿蓝的权重)')

a = np.array([0.299,0.587,0.114])

#红色red*0.299,绿色green*0.587,蓝色blue*0.114

im_data3 = np.dot(im_data,a)

display(im_data3.shape)

plt.imshow(im_data3,cmap = 'gray')

第3种:加权平均法(红绿蓝的权重)

(768, 1024)

Out[8]:

<matplotlib.image.AxesImage at 0x7f6ae95695c0>

 

仅供参考学习,严禁转载!

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值