一、概述:
人類能夠觀察到的光的波長范圍是有限的,並且人類視覺有一個特點,只能分辨出二十幾種灰度,也就是說即使采集到的灰度圖像分辨率超級高,有上百個灰度級,但是很遺憾,人們只能看出二十幾個,也就是說信息損失了五十倍。但人類視覺對彩色的分辨能力相當強,能夠分辨出幾千種色度,所以在實際應用中,可以將灰度圖轉變成彩虹圖或者偽彩圖等根據需求的彩色圖。
二、彩虹圖:
主要思路:把灰度圖對應的0~255的數值分別轉成彩虹色:紅、橙、黃、綠、青、藍,這里沒有使用紫色,是因為紫色的效果並不好。
//彩虹圖的顏色分配取一下值 // R G B gray //---------------------------------- // 紅 255, 0, 0 255 // 橙 255, 127, 0 204 // 黃 255, 255, 0 153 // 綠 0, 255, 0 102 // 青 0, 255, 255 51 // 藍 0, 0, 255 0
代碼:
Mat gray2rainbow(const Mat& scaledGray) { Mat outputRainbow(scaledGray.size(), CV_8UC3); unsigned char grayValue; for (int y = 0; y < scaledGray.rows; y++) for (int x = 0; x < scaledGray.cols; x++) { grayValue = scaledGray.at<uchar>(y, x); Vec3b& pixel = outputRainbow.at<Vec3b>(y, x); if (grayValue <= 51) { pixel[0] = 255; pixel[1] = grayValue * 5; pixel[2] = 0; } else if (grayValue <= 102) { grayValue -= 51; pixel[0] = 255 - grayValue * 5; pixel[1] = 255; pixel[2] = 0; } else if (grayValue <= 153) { grayValue -= 102; pixel[0] = 0; pixel[1] = 255; pixel[2] = grayValue * 5; } else if (grayValue <= 204) { grayValue -= 153; pixel[0] = 0; pixel[1] = 255 - static_cast<unsigned char>(grayValue * 128.0 / 51 + 0.5); pixel[2] = 255; } else if (grayValue <= 255) { grayValue -= 204; pixel[0] = 0; pixel[1] = 127 - static_cast<unsigned char>(grayValue * 127.0 / 51 + 0.5); pixel[2] = 255; } } return outputRainbow; }
三、偽彩圖
偽彩色圖片的處理,就是用RGB三色交叉,不同的彩色表示不同的灰度值,將一幅灰度圖轉變成為一幅彩色圖片。
Mat gray2pseudocolor(const Mat& scaledGray) { Mat outputPseudocolor(scaledGray.size(), CV_8UC3); unsigned char grayValue; for (int y = 0; y < scaledGray.rows; y++) for (int x = 0; x < scaledGray.cols; x++) { grayValue = scaledGray.at<uchar>(y, x); Vec3b& pixel = outputPseudocolor.at<Vec3b>(y, x); pixel[0] = abs(255 - grayValue); pixel[1] = abs(127 - grayValue); pixel[2] = abs(0 - grayValue); } return outputPseudocolor; }
四、銅色圖
將R去0,G、B兩色交叉。
Mat gray2CopperColor(const Mat& scaledGray) { Mat outputCopperColor(scaledGray.size(), CV_8UC3); unsigned char grayValue; for (int y = 0; y < scaledGray.rows; y++) for (int x = 0; x < scaledGray.cols; x++) { grayValue = scaledGray.at<uchar>(y, x); Vec3b& pixel = outputCopperColor.at<Vec3b>(y, x); pixel[0] = abs(0); pixel[1] = abs(grayValue); pixel[2] = abs(grayValue); } return outputCopperColor; }
五、灰度反轉
將圖像進行灰度反轉處理,即將灰度值為x的像素點轉變為255-x。
利用Opencv中bitwise_not()函數可實現,沒必要一個像素點一個像素點處理。
Mat gray2disColor(const Mat& scaledGray) { Mat disColor(scaledGray.size(), CV_8UC3); bitwise_not(disColor, scaledGray); return disColor; }
六、灰度圖
將一幅彩色圖片轉換為灰度圖
Mat scaleGray(const Mat& inputGray) { Mat outputGray(inputGray.size(), CV_8U); unsigned char grayValue, maxValue = 1; for (int y = 0; y < inputGray.rows; y++) for (int x = 0; x < inputGray.cols; x ++) { grayValue = inputGray.at<uchar>(y, x); maxValue = max(maxValue, grayValue); } float scale = 255.0 / maxValue; for (int y = 0; y < inputGray.rows; y++) for (int x = 0; x < inputGray.cols; x ++) { outputGray.at<uchar>(y, x) = static_cast<unsigned char>(inputGray.at<uchar>(y, x) * scale + 0.5); } return outputGray; }
七、完整代碼
略