Error message here!

Hide Error message here!

Error message here!

Hide Error message here!

Error message here!

Close

# 彩色图到灰度图究竟是怎么变换的

shayue111 2019-02-18 00:09:00 阅读数:230 评论数:0 点赞数:0 收藏数:0

``````from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline``````
``````rgb_img = Image.open('Husky.jpg')
grey_img = rgb_img.convert('L')``````
``rgb_img``

``grey_img``

``````rgb_img.thumbnail((800, 800))
grey_img.thumbnail((800, 800))``````
``rgb_img``

``grey_img``

``````# 获取彩色图的数据
rgb_arr = np.array(rgb_img)``````
``````# 窥探下这个数组，第一印象它是一个3维的
rgb_arr``````
``````array([[[ 93, 153, 203],
[ 91, 151, 201],
[ 90, 149, 200],
...,
[ 3, 120, 189],
[ 2, 119, 188],
[ 4, 121, 190]],
[[ 90, 151, 200],
[ 89, 150, 199],
[ 90, 150, 200],
...,
[ 2, 119, 188],
[ 2, 119, 188],
[ 2, 119, 188]],
[[ 86, 149, 196],
[ 86, 149, 196],
[ 89, 151, 198],
...,
[ 1, 119, 188],
[ 1, 119, 188],
[ 1, 118, 187]],
...,
[[ 24, 22, 24],
[ 25, 22, 24],
[ 29, 26, 26],
...,
[ 10, 9, 14],
[ 9, 8, 13],
[ 6, 5, 10]],
[[ 23, 24, 28],
[ 27, 28, 32],
[ 44, 45, 48],
...,
[ 10, 9, 14],
[ 7, 6, 11],
[ 6, 5, 10]],
[[ 38, 41, 47],
[ 50, 53, 59],
[ 64, 66, 71],
...,
[ 8, 7, 12],
[ 6, 5, 10],
[ 7, 6, 11]]], dtype=uint8)``````
``````# 行 列 通道
rgb_arr.shape``````
``(633, 800, 3)``
``````# 宽（列数） 高（行数）
rgb_img.size``````
``(800, 633)``
``````# 可以用matlibplot.pyplot将数组显示为图片
plt.imshow(rgb_arr)``````
``<matplotlib.image.AxesImage at 0x110cb71d0>``

``x = rgb_arr[0][0]``
``````# x代表图片中的1个像素点，它的颜色由[93, 153, 203]表示，因此这3个值为3个通道的值
x``````
``array([ 93, 153, 203], dtype=uint8)``
``````# 那么不难想象，可以通过两个for循环对这些值取平均
grey_arr = np.zeros(rgb_arr.shape[0:2])
# grey_arr.shape (633, 800)
for row in range(633):
for col in range(800):
grey_arr[row][col] = (int(rgb_arr[row][col][0]) + int(rgb_arr[row][col][1]) + int(rgb_arr[row][col][2])) / 3``````
``grey_arr = np.array(grey_arr, dtype=np.uint8)``
``grey_arr``
``````array([[149, 147, 146, ..., 104, 103, 105],
[147, 146, 146, ..., 103, 103, 103],
[143, 143, 146, ..., 102, 102, 102],
...,
[ 23, 23, 27, ..., 11, 10, 7],
[ 25, 29, 45, ..., 11, 8, 7],
[ 42, 54, 67, ..., 9, 7, 8]], dtype=uint8)``````
``````out_img = Image.fromarray(grey_arr)
out_img``````

``std_grey_arr = np.asarray(grey_img)``
``std_grey_arr``
``````array([[140, 138, 136, ..., 92, 91, 93],
[138, 137, 137, ..., 91, 91, 91],
[135, 135, 137, ..., 91, 91, 90],
...,
[ 22, 22, 26, ..., 9, 8, 5],
[ 23, 27, 45, ..., 9, 6, 5],
[ 40, 52, 66, ..., 7, 5, 6]], dtype=uint8)``````

``````## 通过给定不同权重的方法再试一次
for row in range(633):
for col in range(800):
grey_arr[row][col] = 0.11 * rgb_arr[row][col][0] + 0.59 * rgb_arr[row][col][1] + 0.3 * rgb_arr[row][col][2]``````
``grey_arr = np.array(grey_arr, dtype=np.uint8)``
``grey_arr``
``````array([[161, 159, 157, ..., 127, 126, 128],
[158, 157, 158, ..., 126, 126, 126],
[156, 156, 158, ..., 126, 126, 125],
...,
[ 22, 22, 26, ..., 10, 9, 6],
[ 25, 29, 45, ..., 10, 7, 6],
[ 42, 54, 67, ..., 8, 6, 7]], dtype=uint8)``````
``out_img = Image.fromarray(grey_arr)``
``out_img``

https://www.cnblogs.com/shayue/p/10393542.html

30万现金开奖等你来领