MFC - 8bit convert 24bit 이미지 표시
8bit - gray scale 이미지를 24bit color scale 이미지로 표현하는 color map 데이터가 매우 많이 제공한다. OpenCV 에서 기본으로 제공하는 데이터도 있지만 상용 라이브러리들이 제공하는 여러가지 데이터들이 많이 있다.


// 컬러맵 룩업 테이블 초기화 (실제 값으로 채워야 함)
std::vector<cv::Vec3b> matlabColormap(256);
void initializeAfmhotColormap() {
for (int i = 0; i < 256; ++i) {
if (i < 85) {
matlabColormap [i] = cv::Vec3b(0, 0, static_cast<uchar>(i * 3));
} else if (i < 170) {
matlabColormap [i] = cv::Vec3b(0, static_cast<uchar>((i - 85) * 3), 255 - static_cast<uchar>((i - 85) * 3));
} else {
matlabColormap [i] = cv::Vec3b(static_cast<uchar>((i - 170) * 3), 255 - static_cast<uchar>((i - 170) * 3), 0);
}
}
}
int main() {
initializeAfmhotColormap();
// 입력 회색조 이미지 로드
std::string inputImagePath = "input_grayscale.png";
cv::Mat grayscaleImage = cv::imread(inputImagePath, cv::IMREAD_GRAYSCALE);
if (grayscaleImage.empty()) {
return -1;
}
int rows = grayscaleImage.rows;
int cols = grayscaleImage.cols;
// 새로운 RGB 이미지 생성
cv::Mat rgbImage(rows, cols, CV_8UC3);
// 각 픽셀 순회하며 컬러맵 적용
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
uchar intensity = grayscaleImage.at<uchar>(i, j);
cv::Vec3b rgbColor = afmhotColormap[intensity];
rgbImage.at<cv::Vec3b>(i, j) = rgbColor;
}
}
// 생성된 RGB 이미지 저장
std::string outputImagePath = "output_rgb.png";
bool success = cv::imwrite(outputImagePath, rgbImage);
if (success) {
std::cout << "RGB 이미지가 성공적으로 저장되었습니다: " << outputImagePath << std::endl;
} else {
std::cerr << "Error: RGB 이미지 저장에 실패했습니다." << std::endl;
}
return 0;
}