最近在分析彩色图片灰度化的过程中使用到了一个函数skimage.color中的rgb2gray(),但是与自己所实现的灰度化公式在计算结果上出入较大,因此特意写这篇文章记录一下对比过程.
首先,看一下RGB转Gray的计算公式 : Gray = R*0.299 + G*0.587 + B*0.114
用Python代码手动实现:
# 手动实现
import numpy as np
import matplotlib.pyplot as plt
import cv2
# 读取图片
img = cv2.imread("lenna.png")
# cv2.imshow("lenna_png", img)
# cv2.waitKey(0)
img_height, img_weight, channel = img.shape
dest = np.ndarray((img_height,img_weight))
for img_y in range(img_height):
for img_x in range(img_weight):
R = img[img_y, img_x, 2]
G = img[img_y, img_x, 1]
B = img[img_y, img_x, 0]
gray_value = float(R*0.299 + G*0.587 + B*0.114) / 255
# print(gray_value)
dest[img_y, img_x] = gray_value
# 将数据输出到txt文件中方便后面对比数据差异
fp = open("data_1.txt", mode = 'w')
for img_y in range(img_height):
for img_x in range(img_weight):
fp.write(str(dest[img_y, img_x]))
fp.write("\t")
fp.write("\n")
print(dest)
plt.imshow(dest, cmap = 'gray')
# cv2.waitKey()
plt.show()
用 from skimage.color import rgb2gray来实现:
from skimage.color import rgb2gray
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import cv2
# 原图
plt.subplot(221)
img = plt.imread("lenna.png")
# img = cv2.imread("lenna.png", False)
plt.imshow(img)
print("---image lenna----")
print(img)
# 灰度化
img_gray = rgb2gray(img)
# img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# img_gray = img
plt.subplot(222)
# plt.imshow(img_gray, cmap='gray')
plt.imshow(img_gray,cmap = 'gray')
print("---image gray----")
print(img_gray)
img_height, img_weight, channel = img.shape
fp = open("data_rgb2gray.txt", mode = 'w')
for img_y in range(img_height):
for img_x in range(img_weight):
fp.write(str(img_gray[img_y, img_x]))
fp.write("\t")
fp.write("\n")
exit()
我们将灰度图的数据当成str类型的数据输出到txt文件中对比可以看到数据的差异非常大
虽然计算的结果不同但是二者都能输出灰度图, 且最终的呈现效果并无差异, 那么造成数据不一致的可能是计算的公式不一致吗 ?
查询资料发现 Gray = R*0.299 + G*0.587 + B*0.114 计算公式是由标准的RGB三元色通过Gamma矫正得到的.而skimage中的Gray值是通过RGB对当前CRT荧光屏校准得到的:Gray = 0.2125 * R + 0.7154 * G + 0.0721 * B , 如此, 我们需要换一种算法,再验证一次,看看效果.除了计算的精度差异之外,两组数据基本一致.
# Gray = float(0.2125 * R + 0.7154 * G + 0.0721 * B) / 255
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import cv2
# 读取图片
img = cv2.imread("lenna.png")
# cv2.imshow("lenna_png", img)
# cv2.waitKey(0)
img_height, img_weight, channel = img.shape
dest = np.ndarray((img_height,img_weight))
for img_y in range(img_height):
for img_x in range(img_weight):
R = img[img_y, img_x, 2]
G = img[img_y, img_x, 1]
B = img[img_y, img_x, 0]
gray_value = float(0.2125 * R + 0.7154 * G + 0.0721 * B) / 255
# print(gray_value)
dest[img_y, img_x] = round(gray_value, 8)
# 将数据输出到txt文件中方便后面对比数据差异
fp = open("data_2.txt", mode = 'w')
for img_y in range(img_height):
for img_x in range(img_weight):
fp.write(str(dest[img_y, img_x]))
fp.write("\t")
fp.write("\n")
print(dest)
plt.imshow(dest, cmap = 'gray')
# cv2.waitKey()
plt.show()
灰度图也可以正常显示,总结来说,无论是哪一种公式实现的灰度图,其本质上都是将三通道的彩色数据均值化到一通道数据的映射,只要标准统一.其结果基本一致.