Instantly share code, notes, and snippets.

Embed
What would you like to do?
//-----------------------------Gray level histogram computation ------------------------------------
void utility::ComputeHistogramGrey(image& src, image& tgt, ROI roi)
{
int histogram_1[256] = { 0 };
int histogram_2[256] = { 0 };
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
histogram_1[src.getPixel(i, j)] = histogram_1[src.getPixel(i, j)] + 1;
}
else {
histogram_2[src.getPixel(i, j)] = histogram_2[src.getPixel(i, j)] + 1;
}
}
}
int max = histogram_1[0];
for (int i = 1; i < 256; i++) {
if (histogram_1[i] > max)
max = histogram_1[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_1[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_1.pgm");
max = histogram_2[0];
for (int i = 1; i < 256; i++) {
if (histogram_2[i] > max)
max = histogram_2[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_2[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_2.pgm");
}
//----------------------Gray level histogram equalisation-------------------
void utility::EqualizeHistogramGrey(image& src, image& tgt, ROI roi)
{
int histogram_1[256] = { 0 };
int histogram_2[256] = { 0 };
int histogram_1_cdf[256] = { 0 };
int histogram_2_cdf[256] = { 0 };
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
histogram_1[src.getPixel(i, j)] = histogram_1[src.getPixel(i, j)] + 1;
}
else {
histogram_2[src.getPixel(i, j)] = histogram_2[src.getPixel(i, j)] + 1;
}
}
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < 256; i++) {
sum1 = sum1 + histogram_1[i];
sum2 = sum2 + histogram_2[i];
histogram_1_cdf[i] = sum1;
histogram_2_cdf[i] = sum2;
}
int min_1 = histogram_1_cdf[0];
int min_2 = histogram_2_cdf[0];
for (int i = 1; i < 256; i++) {
if (histogram_1_cdf[i] < min_1)
min_1 = histogram_1_cdf[i];
if (histogram_2_cdf[i] < min_2)
min_2 = histogram_2_cdf[i];
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
int m_n = roi.M_N(0);
double h = ((double)(histogram_1_cdf[src.getPixel(i, j)] - min_1) / (double)(m_n - min_1)) * 255;
tgt.setPixel(i, j, (int)h);
}
else {
int m_n = roi.M_N(1);
double h = ((double)(histogram_2_cdf[src.getPixel(i, j)] - min_2) / (double)(m_n - min_2)) * 255;
tgt.setPixel(i, j, (int)h);
}
}
else
tgt.setPixel(i, j, src.getPixel(i, j));
}
}
//---------------Color histogram computation for RGB-----------------------
void utility::ComputeHistogramColor(image& src, image& tgt, ROI roi)
{
int histogram_1[256] = { 0 };
int histogram_2[256] = { 0 };
int histogram_3[256] = { 0 };
int histogram_4[256] = { 0 };
int histogram_5[256] = { 0 };
int histogram_6[256] = { 0 };
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
histogram_1[src.getPixel(i, j, RED)] = histogram_1[src.getPixel(i, j, RED)] + 1;
histogram_3[src.getPixel(i, j, GREEN)] = histogram_3[src.getPixel(i, j, GREEN)] + 1;
histogram_5[src.getPixel(i, j, BLUE)] = histogram_5[src.getPixel(i, j, BLUE)] + 1;
}
else {
histogram_2[src.getPixel(i, j, RED)] = histogram_2[src.getPixel(i, j, RED)] + 1;
histogram_4[src.getPixel(i, j, GREEN)] = histogram_4[src.getPixel(i, j, GREEN)] + 1;
histogram_6[src.getPixel(i, j, BLUE)] = histogram_6[src.getPixel(i, j, BLUE)] + 1;
}
}
}
int max = histogram_1[0];
for (int i = 1; i < 256; i++) {
if (histogram_1[i] > max)
max = histogram_1[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_1[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_1_RED.pgm");
max = histogram_3[0];
for (int i = 1; i < 256; i++) {
if (histogram_3[i] > max)
max = histogram_3[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_3[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_1_GREEN.pgm");
max = histogram_5[0];
for (int i = 1; i < 256; i++) {
if (histogram_5[i] > max)
max = histogram_5[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_5[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_1_BLUE.pgm");
max = histogram_2[0];
for (int i = 1; i < 256; i++) {
if (histogram_2[i] > max)
max = histogram_2[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_2[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_2_RED.pgm");
max = histogram_4[0];
for (int i = 1; i < 256; i++) {
if (histogram_4[i] > max)
max = histogram_4[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_4[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_2_GREEN.pgm");
max = histogram_6[0];
for (int i = 1; i < 256; i++) {
if (histogram_6[i] > max)
max = histogram_6[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_6[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_2_BLUE.pgm");
}
/*--------------Histogram Equalization for color images ---------------*/
void utility::EqualizeHistogramColor(image& src, image& tgt, ROI roi)
{
for (int c = 0; c < 3; c++) {
int histogram_1[256] = { 0 };
int histogram_2[256] = { 0 };
int histogram_1_cdf[256] = { 0 };
int histogram_2_cdf[256] = { 0 };
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
histogram_1[src.getPixel(i, j, c)] = histogram_1[src.getPixel(i, j, c)] + 1;
}
else {
histogram_2[src.getPixel(i, j, c)] = histogram_2[src.getPixel(i, j, c)] + 1;
}
}
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < 256; i++) {
sum1 = sum1 + histogram_1[i];
sum2 = sum2 + histogram_2[i];
histogram_1_cdf[i] = sum1;
histogram_2_cdf[i] = sum2;
}
int min_1 = histogram_1_cdf[0];
int min_2 = histogram_2_cdf[0];
for (int i = 1; i < 256; i++) {
if (histogram_1_cdf[i] < min_1)
min_1 = histogram_1_cdf[i];
if (histogram_2_cdf[i] < min_2)
min_2 = histogram_2_cdf[i];
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
tgt.setPixel(i, j, RED, src.getPixel(i, j, RED));
tgt.setPixel(i, j, GREEN, src.getPixel(i, j, GREEN));
tgt.setPixel(i, j, BLUE, src.getPixel(i, j, BLUE));
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
int m_n = roi.M_N(0);
double h = ((double)(histogram_1_cdf[src.getPixel(i, j, c)] - min_1) / (double)(m_n - min_1)) * 255;
tgt.setPixel(i, j, c, (int)h);
}
else {
int m_n = roi.M_N(1);
double h = ((double)(histogram_2_cdf[src.getPixel(i, j, c)] - min_2) / (double)(m_n - min_2)) * 255;
tgt.setPixel(i, j, c, (int)h);
}
}
}
if (c == 0)
tgt.save("histogram_Normalized_RED.ppm");
else if (c == 1)
tgt.save("histogram_Normalized_GREEN.ppm");
else
tgt.save("histogram_Normalized_BLUE.ppm");
}
}
/*--------Histogram equalization for RGB channels together ---------------------*/
void utility::EqualizeHistogramColorAll(image& src, image& tgt, ROI roi)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
tgt.setPixel(i, j, RED, src.getPixel(i, j, RED));
tgt.setPixel(i, j, GREEN, src.getPixel(i, j, GREEN));
tgt.setPixel(i, j, BLUE, src.getPixel(i, j, BLUE));
}
for (int c = 0; c < 3; c++) {
int histogram_1[256] = { 0 };
int histogram_2[256] = { 0 };
int histogram_1_cdf[256] = { 0 };
int histogram_2_cdf[256] = { 0 };
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
histogram_1[src.getPixel(i, j, c)] = histogram_1[src.getPixel(i, j, c)] + 1;
}
else {
histogram_2[src.getPixel(i, j, c)] = histogram_2[src.getPixel(i, j, c)] + 1;
}
}
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < 256; i++) {
sum1 = sum1 + histogram_1[i];
sum2 = sum2 + histogram_2[i];
histogram_1_cdf[i] = sum1;
histogram_2_cdf[i] = sum2;
}
int min_1 = histogram_1_cdf[0];
int min_2 = histogram_2_cdf[0];
for (int i = 1; i < 256; i++) {
if (histogram_1_cdf[i] < min_1)
min_1 = histogram_1_cdf[i];
if (histogram_2_cdf[i] < min_2)
min_2 = histogram_2_cdf[i];
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
int m_n = roi.M_N(0);
double h = ((double)(histogram_1_cdf[src.getPixel(i, j, c)] - min_1) / (double)(m_n - min_1)) * 255;
tgt.setPixel(i, j, c, (int)h);
}
else {
int m_n = roi.M_N(1);
double h = ((double)(histogram_2_cdf[src.getPixel(i, j, c)] - min_2) / (double)(m_n - min_2)) * 255;
tgt.setPixel(i, j, c, (int)h);
}
}
}
}
}
/* HSI histogram generation ----------------------------------------*/
void utility::ComputeHistogramHSI(image& src, image& tgt, ROI roi)
{
int histogram_1[360] = { 0 }; //Hue
int histogram_2[360] = { 0 };
int histogram_3[100] = { 0 }; //Saturation
int histogram_4[100] = { 0 };
int histogram_5[256] = { 0 }; //Intensity
int histogram_6[256] = { 0 };
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
double* HSI = RGB_HSI((double)src.getPixel(i, j, RED), (double)src.getPixel(i, j, GREEN), (double)src.getPixel(i, j, BLUE));
src.setPixel(i, j, RED, (int)HSI[0]); //Hue
src.setPixel(i, j, GREEN, (int)(HSI[1])); //Saturation
src.setPixel(i, j, BLUE, (int)(HSI[2])); //Intensity
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
histogram_1[src.getPixel(i, j, RED)] = histogram_1[src.getPixel(i, j, RED)] + 1;
histogram_3[src.getPixel(i, j, GREEN)] = histogram_3[src.getPixel(i, j, GREEN)] + 1;
histogram_5[src.getPixel(i, j, BLUE)] = histogram_5[src.getPixel(i, j, BLUE)] + 1;
}
else {
histogram_2[src.getPixel(i, j, RED)] = histogram_2[src.getPixel(i, j, RED)] + 1;
histogram_4[src.getPixel(i, j, GREEN)] = histogram_4[src.getPixel(i, j, GREEN)] + 1;
histogram_6[src.getPixel(i, j, BLUE)] = histogram_6[src.getPixel(i, j, BLUE)] + 1;
}
}
}
int max = histogram_1[0];
for (int i = 1; i < 360; i++) {
if (histogram_1[i] > max)
max = histogram_1[i];
}
tgt.resize(360, 360);
for (int i = 0; i < 360; i++)
for (int j = 0; j < 360; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 360; i++) {
int j = (int)(((double)histogram_1[i] / (double)max) * 360);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_1_HUE.pgm");
max = histogram_3[0];
for (int i = 1; i < 100; i++) {
if (histogram_3[i] > max)
max = histogram_3[i];
}
tgt.resize(100, 100);
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 100; i++) {
int j = (int)(((double)histogram_3[i] / (double)max) * 100);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_1_SATURATION.pgm");
max = histogram_5[0];
for (int i = 1; i < 256; i++) {
if (histogram_5[i] > max)
max = histogram_5[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_5[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_1_INTENSITY.pgm");
max = histogram_2[0];
for (int i = 1; i < 360; i++) {
if (histogram_2[i] > max)
max = histogram_2[i];
}
tgt.resize(360, 360);
for (int i = 0; i < 360; i++)
for (int j = 0; j < 360; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 360; i++) {
int j = (int)(((double)histogram_2[i] / (double)max) * 360);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_2_HUE.pgm");
max = histogram_4[0];
for (int i = 1; i < 100; i++) {
if (histogram_4[i] > max)
max = histogram_4[i];
}
tgt.resize(100, 100);
for (int i = 0; i < 100; i++)
for (int j = 0; j < 100; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 100; i++) {
int j = (int)(((double)histogram_4[i] / (double)max) * 100);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_2_SATURATION.pgm");
max = histogram_6[0];
for (int i = 1; i < 256; i++) {
if (histogram_6[i] > max)
max = histogram_6[i];
}
tgt.resize(256, 256);
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256; j++)
tgt.setPixel(i, j, 255);
for (int i = 0; i < 256; i++) {
int j = (int)(((double)histogram_6[i] / (double)max) * 256);
for (int k = j; k >= 0; k--)
tgt.setPixel(i, k, 0);
}
tgt.save("histogram_roi_2_INTENSITY.pgm");
}
/*------- HSI histogram equalization ---------------------------------------------*/
void utility::EqualizeHistogramHSI(image& src, image& tgt, ROI roi)
{
tgt.resize(src.getNumberOfRows(), src.getNumberOfColumns());
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
double* HSI = RGB_HSI((double)src.getPixel(i, j, RED), (double)src.getPixel(i, j, GREEN), (double)src.getPixel(i, j, BLUE));
src.setPixel(i, j, RED, (int)HSI[0]); //Hue
src.setPixel(i, j, GREEN, (int)HSI[1]); //Saturation
src.setPixel(i, j, BLUE, (int)HSI[2]); //Intensity
}
int size = 0;
for (int c = 0; c < 3; c++) {
if (c == 0)
size = 360;
if (c == 1)
size = 100;
else
size = 256;
int* histogram_1 = new int[size];
int* histogram_2 = new int[size];
int* histogram_1_cdf = new int[size];
int* histogram_2_cdf = new int[size];
for (int i = 0; i < size; i++) {
histogram_1[i] = 0;
histogram_2[i] = 0;
histogram_1_cdf[i] = 0;
histogram_2_cdf[i] = 0;
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
histogram_1[src.getPixel(i, j, c)] = histogram_1[src.getPixel(i, j, c)] + 1;
}
else {
histogram_2[src.getPixel(i, j, c)] = histogram_2[src.getPixel(i, j, c)] + 1;
}
}
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < size; i++) {
sum1 = sum1 + histogram_1[i];
sum2 = sum2 + histogram_2[i];
histogram_1_cdf[i] = sum1;
histogram_2_cdf[i] = sum2;
}
int min_1 = histogram_1_cdf[0];
int min_2 = histogram_2_cdf[0];
for (int i = 1; i < size; i++) {
if (histogram_1_cdf[i] < min_1)
min_1 = histogram_1_cdf[i];
if (histogram_2_cdf[i] < min_2)
min_2 = histogram_2_cdf[i];
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
tgt.setPixel(i, j, RED, src.getPixel(i, j, RED));
tgt.setPixel(i, j, GREEN, src.getPixel(i, j, GREEN));
tgt.setPixel(i, j, BLUE, src.getPixel(i, j, BLUE));
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
int value = roi.InROI(i, j);
if (value != -1) {
if (value == 0) {
int m_n = roi.M_N(0);
double h = ((double)(histogram_1_cdf[src.getPixel(i, j, c)] - min_1) / (double)(m_n - min_1)) * (size - 1);
tgt.setPixel(i, j, c, (int)h);
}
else {
int m_n = roi.M_N(1);
double h = ((double)(histogram_2_cdf[src.getPixel(i, j, c)] - min_2) / (double)(m_n - min_2)) * (size - 1);
tgt.setPixel(i, j, c, (int)h);
}
}
}
for (int i = 0; i < src.getNumberOfRows(); i++)
for (int j = 0; j < src.getNumberOfColumns(); j++) {
double* RGB = HSI_RGB((double)tgt.getPixel(i, j, RED), (double)tgt.getPixel(i, j, GREEN), (double)tgt.getPixel(i, j, BLUE));
tgt.setPixel(i, j, RED, (int)RGB[0]); //Hue
tgt.setPixel(i, j, GREEN, (int)(RGB[1])); //Saturation
tgt.setPixel(i, j, BLUE, (int)(RGB[2])); //Intensity
}
if (c == 0)
tgt.save("histogram_Normalized_H.ppm");
else if (c == 1)
tgt.save("histogram_Normalized_S.ppm");
else
tgt.save("histogram_Normalized_I.ppm");
}
}
//---------------------RGB to HSI---------------------------
double* utility::RGB_HSI(double red, double green, double blue)
{
double H, S, I;
double total = red + green + blue;
red = red / total;
green = green / total;
blue = blue / total;
double Min = red < green ? red : green;
Min = Min < blue ? Min : blue;
double root = sqrt(pow(red - green, 2) + ((red - blue) * (green - blue)));
if (blue <= green)
H = acos((((2.0 * red) - green - blue) * 0.5) / root);
else
H = (3.141592 * 2.0) - acos((((2.0 * red) - green - blue) * 0.5) / root);
S = 1.0 - (3.0 * Min);
I = total / 3.0;
double* HSI = new double[3];
HSI[0] = H * (180.0 / 3.141592);
HSI[1] = S * 100.0;
HSI[2] = I;
if (HSI[0] < 0)
HSI[0] = 0;
if (HSI[0] > 360)
HSI[0] = 360.0;
if (HSI[1] < 0)
HSI[1] = 0;
if (HSI[1] > 100)
HSI[1] = 100.0;
if (HSI[2] < 0)
HSI[2] = 0;
if (HSI[2] > 255)
HSI[2] = 255.0;
return HSI;
}
//---------------------HSI to RGB---------------------------
double* utility::HSI_RGB(double H, double S, double I)
{
double red = 0.0, green = 0.0, blue = 0.0;
H = H * (3.141592 / 180.0);
S = S / 100.0;
I = I / 255.0;
double x = I * (1 - S);
double y = I * (1 + ((S * cos(H)) / cos((3.141592 / 3) - H)));
double z = (3 * I) - (x + y);
if (H < 2 * 3.141592 / 3) {
red = y;
green = z;
blue = x;
}
else if ((2 * 3.141592 / 3 <= H) && (H < 4 * 3.141592 / 3)) {
H = H - (2 * 3.141592 / 3);
y = I * (1 + ((S * cos(H)) / cos((3.141592 / 3) - H)));
z = (3 * I) - (x + y);
red = x;
green = y;
blue = z;
}
else if ((4 * 3.141592 / 3 <= H) && (H < 2 * 3.141592)) {
H = H - (4 * 3.141592 / 3);
y = I * (1 + ((S * cos(H)) / cos((3.141592 / 3) - H)));
z = 3 * I - (x + y);
red = z;
blue = y;
green = x;
}
double* RGB = new double[3];
RGB[0] = red * 255.0;
RGB[1] = green * 255.0;
RGB[2] = blue * 255.0;
if (RGB[0] < 0)
RGB[0] = 0.0;
if (RGB[0] > 255)
RGB[0] = 255.0;
if (RGB[1] < 0)
RGB[1] = 0.0;
if (RGB[1] > 255)
RGB[1] = 255.0;
if (RGB[2] < 0)
RGB[2] = 0.0;
if (RGB[2] > 255)
RGB[2] = 255.0;
return RGB;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment