Skip to content

Instantly share code, notes, and snippets.

@p208p2002
Created December 29, 2018 13:24
Show Gist options
  • Select an option

  • Save p208p2002/9c12c4fd64e7f4ab3cf177461264e895 to your computer and use it in GitHub Desktop.

Select an option

Save p208p2002/9c12c4fd64e7f4ab3cf177461264e895 to your computer and use it in GitHub Desktop.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment