本文介绍了RGB 到 HSI 和 HSI 到 RGB 转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 RGB 转换为 HSI 并将其还原.(任务需要从头开始.)

I am trying to covert RGB to HSI and revert it. (The task is required to have it from scratch.)

RGBHSI 的转换中,饱和度和强度输出都很好.但是我似乎没有在 Hue 的制定中遇到问题.示例输出:

In RGB to HSI convertion, Saturation and Intensity outputs are fine. But I don't seem to get the problem in the formulation of Hue.example output:

Red = 255, Green = 255, Blue =  255
Hue  = -2147483648, Saturation = 0, Intensity = 255

Red = 252, Green = 255, Blue =  255
Hue  = 3, Saturation = 0.00787402, Intensity = 254

我使用这个计算器来检查我的输出.

I use this calculator to check my outputs.

请告诉我出了什么问题.谢谢.

Please let me know what's wrong. Thanks.

#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "rgb.h"
#include <cmath>
#include <algorithm>
#include <fstream>
using namespace std;

int main()
{
  char infname[256];
  ofstream outputFile, outputFile2;
  outputFile.open("RGB_HSI.txt");
  outputFile2.open("HSI_RGB.txt");

  cout << "Enter input image  : ";
  cin >> infname;
  IplImage *img = cvLoadImage(infname, 1);
  RgbImage pic(img);
  int H = img->height;
  int W = img->width;

 for (int j=0;j<H;j++)
 for (int i=0;i<W;i++) {

     double temp = 0;
     double R =(double) pic[j][i].r;
     double G =(double) pic[j][i].g;
     double B =(double) pic[j][i].b;
     double intensity = 0;
     double hue = 0;
     double saturation = 0;
     int resultHue = 0;
     double resultSaturation = 0;
     int resultIntensity = 0;

   intensity = (R + G + B) / 3;

   if ((R + G + B) == 765) {
      saturation = 0;
      hue = 0;
        }

  double minimum = min(R, min(G, B));

  if (intensity > 0) {
   saturation = 1 - minimum / intensity;
   }

  else if (intensity == 0) {
   saturation = 0;
   }


  temp = (R - (G/2) - (B/2)) / (sqrt((R*R) + (G*G) + (B*B) - (R*G) - (R*B) - (G*B)));
  if (G >= B) {
    hue = acos(temp);
    outputFile<<"1. temp = "<<temp<<", H = "<<hue<<endl;
   }

  else if (B > G) {
    hue = 360 - acos(temp);
    outputFile<<"2. temp = "<<temp<<", H = "<<hue<<endl;
   }

  resultHue = (int) hue;
  resultSaturation = saturation;
  resultIntensity = (int) intensity;

//outputFile2<<"image = "<<pic[j][i]<<endl;
outputFile<<"Red = "<<R<<", Green = "<<G<<", Blue =  "<<B<<endl;
outputFile<<"Hue  = "<<resultHue<<", Saturation = "<<resultSaturation<<", Intensity = "<<resultIntensity<<endl;



//converting HSI to RGB

int backR = 0, backG = 0, backB = 0;

if (resultHue == 0){
   backR = (int) (resultIntensity + (2 * resultIntensity * resultSaturation));
   backG = (int) (resultIntensity - (resultIntensity * resultSaturation));
   backB = (int) (resultIntensity - (resultIntensity * resultSaturation));
  }

else if ((0 < resultHue) && (resultHue < 120)) {
   backR = (int) (resultIntensity + (resultIntensity * resultSaturation) * cos(resultHue) / cos(60-resultHue));
   backG = (int) (resultIntensity + (resultIntensity * resultSaturation) * (1 - cos(resultHue) / cos(60-resultHue)));
   backB = (int) (resultIntensity - (resultIntensity * resultSaturation));
  }

else if ( resultHue == 120 ){
   backR = (int) (resultIntensity - (resultIntensity * resultSaturation));
   backG = (int) (resultIntensity + (2 * resultIntensity * resultSaturation));
   backB = (int) (resultIntensity - (resultIntensity * resultSaturation));
  }

else if ((120 < resultHue) && (resultHue < 240)) {
   backR = (int) (resultIntensity - (resultIntensity * resultSaturation));
   backG = (int) (resultIntensity + (resultIntensity * resultSaturation) * cos(resultHue-120) / cos(180-resultHue));
   backB = (int) (resultIntensity + (resultIntensity * resultSaturation) * (1 - cos(resultHue-120) / cos(180-resultHue)));
  }

else if (resultHue == 240) {
   backR = (int) (resultIntensity - (resultIntensity * resultSaturation));
   backG = (int) (resultIntensity - (resultIntensity * resultSaturation));
   backB = (int) (resultIntensity + (2 * resultIntensity * resultSaturation));
  }

else if ((240 < resultHue) && (resultHue < 360)) {
   backR = (int) (resultIntensity + (resultIntensity * resultSaturation) * (1 - cos(resultHue-240) / cos(300-resultHue)));
   backG = (int) (resultIntensity - (resultIntensity * resultSaturation));
   backB = (int) (resultIntensity + (resultIntensity * resultSaturation) * cos(resultHue-240) / cos(300-resultHue));
  }

//outputpic[j][i] = (int) (R + G + B);
//outputFile2<<"output = "<<outputpic[j][i]<<endl;
outputFile2<<"Hue  = "<<resultHue<<", Saturation = "<<resultSaturation<<", Intensity = "<<resultIntensity<<endl;
outputFile2<<"Red = "<<backR<<", Green = "<<backG<<", Blue =  "<<backB<<endl;
}


outputFile.close();
cout << "\nRGB_HSI values printed as text file: RGB_HSI.text\n";
outputFile2.close();
cout << "\nHSI_RGB values printed as text file: HSI_RGB.text\n";

   return 0;
    }

推荐答案

从其他人的回答看来,当您计算 temp 时,R = G = B 似乎存在除以零的问题 但也从我可以告诉你的是使用 degrees 和三角函数,但他们期待 radians 即:

From others answer it looks like there is a divide by zero issue when R = G = B when you calculate temp but also from what I can tell you are using degrees with the trigonometric function but they are expecting radians i.e.:

#include <cmath>
#include <iostream>

int main()
{
   double pi = atan(1)*4 ;
   std::cout << cos(180) << std::endl ;
   std::cout << cos(360) << std::endl ;
   std::cout << cos(pi) << std::endl ;
   std::cout << cos(2*pi) << std::endl ;
}

这篇关于RGB 到 HSI 和 HSI 到 RGB 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 04:42