最近在分析几个开源图像处理程序时,发现了几种不同的彩色图像转灰度方法,方法都很简单,也非常容易理解,但是效果还是有明显不同。在介绍计算方法前,先要说说亮度(lightness)与照度(luminosity)的区别。亮度(lightness),指的是人在看到光源时,眼睛感觉到的光亮度。照度(luminosity),指的是光源照射到物体上,单位被照射面积上的光通量。可以说,亮度是物体反射光到眼里的强弱,是人的一种主观感觉;而照度是光源照在物体上的强弱,是一种客观的物理量。同样的光源、同样的距离照在白纸和黑纸上,亮度不同,照度相同。下面是简单代码示例:
1. 基于均值的方法:
uchar *pSrcImg = src.data; for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++, pSrcImg += 3) { double average = (pSrcImg[0] + pSrcImg[1] + pSrcImg[2]) / 3; pSrcImg[0] = (uchar)MIN(255, average); pSrcImg[1] = (uchar)MIN(255, average); pSrcImg[2] = (uchar)MIN(255, average); } }
2. 基于亮度的方法:
uchar *pSrcImg = src.data; for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++, pSrcImg += 3) { uchar max_val = MAX(pSrcImg[0], pSrcImg[1]); max_val = MAX(max_val, pSrcImg[2]); uchar min_val = MIN(pSrcImg[0], pSrcImg[1]); min_val = MIN(min_val, pSrcImg[2]); double lightness = (max_val + min_val) / 2; pSrcImg[0] = (uchar)MIN(255, lightness); pSrcImg[1] = (uchar)MIN(255, lightness); pSrcImg[2] = (uchar)MIN(255, lightness); } }
3. 基于照度的方法:
// HDTV with BT.709 uchar *pSrcImg = src.data; for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++, pSrcImg += 3) { double luminosity = pSrcImg[0] * 0.0722 + pSrcImg[1] * 0.7152 + pSrcImg[2] * 0.2126; pSrcImg[0] = (uchar)MIN(255, luminosity); pSrcImg[1] = (uchar)MIN(255, luminosity); pSrcImg[2] = (uchar)MIN(255, luminosity); } }
// SDTV with BT.601 uchar *pSrcImg = src3.data; for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++, pSrcImg += 3) { double luminosity = pSrcImg[0] * 0.114 + pSrcImg[1] * 0.587 + pSrcImg[2] * 0.299; pSrcImg[0] = (uchar)MIN(255, luminosity); pSrcImg[1] = (uchar)MIN(255, luminosity); pSrcImg[2] = (uchar)MIN(255, luminosity); } }
下面是上述几种转换方法的效果图:
时间: 2024-10-13 00:43:05