[OpenCV] Convert RGB to Web-Safe Colors
Convert RGB to Web-Safe Colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <opencv2/opencv.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
using namespace cv; | |
int convertToWebSafe(int color) { | |
//0 51 102 153 204 255 | |
// 25 76 127 178 229 | |
if (color >= 229) | |
return 255; | |
else if (color >= 178) | |
return 204; | |
else if (color >= 127) | |
return 153; | |
else if (color >= 76) | |
return 102; | |
else if (color >= 25) | |
return 51; | |
else | |
return 0; | |
} | |
int main() | |
{ | |
Mat3b image= imread("C:/img/Fig0608(RGB-full-color-cube).tif");//image | |
Mat3b srcImage = imread("C:/img/Fig0608(RGB-full-color-cube).tif");//src image | |
int i, j; | |
int r, g, b; | |
printf("%d,%d",image.size().width, image.size().height); | |
for (i = 0; i < image.size().width;i++) { //access each pixel | |
for (j = 0; j < image.size().height;j++) { | |
b = image.at<cv::Vec3b>(j, i)[0]; //b | |
g = image.at<cv::Vec3b>(j, i)[1]; //g | |
r = image.at<cv::Vec3b>(j, i)[2]; //r | |
// | |
image.at<cv::Vec3b>(j, i)[0] = convertToWebSafe(b); | |
image.at<cv::Vec3b>(j, i)[1] = convertToWebSafe(g); | |
image.at<cv::Vec3b>(j, i)[2] = convertToWebSafe(r); | |
//printf("%d", image.at<cv::Vec3b>(j, i)[0]); | |
} | |
} | |
imshow("web safe", image); | |
imshow("src", srcImage); | |
waitKey(0); | |
return 0; | |
} |
留言
張貼留言