Updating the question I am asking. I have successfully been able to convert from RGB to HSI and back again. I was given instruction by my professor to also increase the image intensity by adding ((1.0 - I) / 2.0) to I (intensity) before I output the image. When I do this it appear to be causing overflowing, hence all my checks for RGB, etc. If I subtract the above formula, it successfully darkens the image without the overflow issues. I can't seem to identify the cause of this overflow nor know how to correct it. I at first thought that it was related to values where R = G = B, but I have confirmed by outputting these variables and debugging that this isn't the case. What causes it is when the saturation, S, is equal to zero. When S = 0, I know that hue, H, is irrelevant. Any assistance would be greatly appreciated!
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
#define M_PI (3.14159265358979323846)
using namespace cv;
int main(int argc, char * argv[])
{
if (argc <= 1)
{
std::cerr << "Not enough arguments." << std::endl;
return (1);
}
Mat img = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
std::cout << img.channels() << std::endl;
if (img.empty())
{
std::cerr << "Unable to open the image " << argv[1] << "." << std::endl;
return (1);
}
namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
imshow("Original Image", img);
Mat hsi(img.rows, img.cols, img.type());
Mat newBGRimg = Mat::zeros(img.rows, img.cols, img.type());
double B, G, R, H, S, I, b, g, re;
int intB = 0, intG = 0, intR = 0;
for (int r = 0; r < img.rows; r++)
{
for (int c = 0; c < img.cols; c++)
{
b = img.at<Vec3b>(r, c)[0];
g = img.at<Vec3b>(r, c)[1];
re = img.at<Vec3b>(r, c)[2];
// Normalize colors
B = b / 255;
G = g / 255;
R = re / 255;
// BGR to HSI
double theta = acos((((R - G) + (R - B)) / 2) / (sqrt((pow((R - G), 2)) + ((R - B)*(G - B) + 0.0001))));
if (B <= G)
{
H = theta;
}
if (B > G)
{
H = (2 * M_PI) - theta;
}
if (H < 0)
H = 0;
if (H > 2 * M_PI)
H = 2 * M_PI;
double min = std::min({ B, G, R });
S = 1 - (3 * min) / (B + G + R); // / (B + G + R)
if (S < 0)
S = 0;
if (S > 1)
S = 1;
I = (B + G + R) / 3;
I = I + ((1.0 - I) / 2.0);
if (I < 0)
I = 0;
if (I > 1)
I = 1;
// HSI to BGR
if (H >= 0 && H < (2 * M_PI / 3))
{
B = I * (1 - S);
R = I * (1 + ((S * cos(H)) / (cos((M_PI / 3) - H))));
G = (3 * I) - (R + B);
B *= 255;
G *= 255;
R *= 255;
if (B > 255)
B = 255;
if (G > 255)
G = 255;
if (R > 255)
R = 255;
if (B < 0)
B = 0;
if (G < 0)
G = 0;
if (R < 0)
R = 0;
intB = (int)round(B);
intG = (int)round(G);
intR = (int)round(R);
//std::cout << intB << " " << intG << " " << intG << " " << r << " " << c << std::endl;
newBGRimg.at<Vec3b>(r, c)[0] = intB;
newBGRimg.at<Vec3b>(r, c)[1] = intG;
newBGRimg.at<Vec3b>(r, c)[2] = intR;
}
else if (H >= (2 * M_PI / 3) && H < (4 * M_PI / 3))
{
H = H - (2 * M_PI) / 3;
R = I * (1 - S);
G = I * (1 + ((S * cos(H)) / (cos((M_PI / 3) - H))));
B = (3 * I) - (R + G);
B *= 255;
G *= 255;
R *= 255;
if (B > 255)
B = 255;
if (G > 255)
G = 255;
if (R > 255)
R = 255;
if (B < 0)
B = 0;
if (G < 0)
G = 0;
if (R < 0)
R = 0;
intB = (int)round(B);
intG = (int)round(G);
intR = (int)round(R);
newBGRimg.at<Vec3b>(r, c)[0] = intB;
newBGRimg.at<Vec3b>(r, c)[1] = intG;
newBGRimg.at<Vec3b>(r, c)[2] = intR;
}
else if (H >= (4 * M_PI / 3))
{
H = H - (4 * M_PI) / 3;
G = I * (1 - S);
B = I * (1 + ((S * cos(H)) / (cos((M_PI / 3 - H)))));
R = (3 * I) - (G + B);
B *= 255;
G *= 255;
R *= 255;
if (B > 255)
B = 255;
if (G > 255)
G = 255;
if (R > 255)
R = 255;
if (B < 0)
B = 0;
if (G < 0)
G = 0;
if (R < 0)
R = 0;
intB = (int)round(B);
intG = (int)round(G);
intR = (int)round(R);
newBGRimg.at<Vec3b>(r, c)[0] = intB;
newBGRimg.at<Vec3b>(r, c)[1] = intG;
newBGRimg.at<Vec3b>(r, c)[2] = intR;
}
}
}
namedWindow("New RGB Image", CV_WINDOW_AUTOSIZE);
imshow("New RGB Image", newBGRimg);
imwrite("hsi-to-rgb.jpg", newBGRimg);
waitKey(0);
return 0;
}
In my original code I was trying to normalize the BGR values using for ex. B/B+G+R per DI 3rd edition supplemental material. . This should have been B/255 to normalize that to [0,1]. I believe the book was incorrect in suggesting this normalization method.