c++ - 不正确的输出 RGB 到 HSI 转换

标签 c++ opencv image-manipulation

我正在尝试将给定图像的 RGB 转换为 HSI。 但我似乎无法获得正确的输出。 强度已经合适了。但是当 R + G + B 不等于 765 时,色相和饱和度始终给出相同的结果 -2147483648。 我用这个计算器检查过: http://www.had2know.com/technology/hsi-rgb-color-converter-equations.html 并使用了这个公式: http://web2.clarkson.edu/class/image_process/RGB_to_HSI.pdf 请指出我做错了什么..谢谢。

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


int main()
{
  char infname[256];

  cout << "Enter input image  : ";
  cin >> infname;
  IplImage *img = cvLoadImage(infname, 0);
  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++) {
            const double PI = 4.0*atan(1.0);
        double norm = 0;
        double R =(double) pic[j][i].r;
        double G =(double) pic[j][i].g;
        double B =(double) pic[j][i].b;
        double omega = 0;
        double intensity = 0;
        double hue = 0;
        double saturation = 0;
            double r = 0;
            double g = 0;
            double b = 0;

        //Intensity
        intensity = (double) (R + G + B) / (3.0*255);         

        //norm colours
        norm = sqrt(pow(r,2) + pow(g,2) + pow(b,2));
        r = (double) (R / norm);
        g = (double) (G / norm);
        b = (double) (B / norm);

        //Saturation and Hue
        if (R + G + B == 765) {
            saturation = 0;
            hue = 0;
        }

        else {
            double tmp = min(r, min(g, b));
            saturation = 1.0 - ((3.0 * tmp)/ (double)(r + g + b));
               if (saturation < 0.5 ){
                 saturation = 0;
                    }
                else if (saturation >= 0.5){
                 saturation = 1;
                  }
             }
        if (saturation != 0) {
            omega = 0.5 * ((r-g) + (r-b)) / sqrt(pow ((r-g),2) + (r-b)*(g-b));
            omega = acos(omega);
            if (B <= G) {
                hue = omega;
            }
            else if (B > G) {
                hue = 2 * PI - omega;
            }
        }

        //convert it to degrees
        int resultHue = (int) round((hue * 180.0) / PI);
        int resultSaturation = (int) (saturation*100.0);
        int resultIntensity = (int) round(intensity * 255);

cout<<"Red = "<<R<<", Green = "<<G<<", Blue =  "<<B<<endl;
cout<<"Hue  = "<<resultHue<<", Saturation = "<<resultSaturation<<", Intensity = "<<resultIntensity<<endl;


}

   return 0;
    }

最佳答案

您使用 rgb 计算 norm,但此时它们始终为 0 .

关于c++ - 不正确的输出 RGB 到 HSI 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15451459/

相关文章:

python - OpenCV - 从图像中删除文本

c++ - 为什么按值传递参数通常会使编译器更轻松地优化代码?

Java 图形字体 - 如何确保字符位于特定区域?

c# - 是否有任何用于扭曲/扭曲图像的 .NET 工具?

c++ - 传递参数并给它们赋值会导致运行时错误

C++:如何验证已删除的指针

c++ - 使用 g++ 4.9.0 打包扩展失败,但适用于 clang++ 3.4

c++ - 我想删除背景(就像黑环)

matlab - Viola jones 人脸检测...如何使用 opencv haar 级联中的倾斜特征

node.js - Node JS Sharp - 转换为 JPG 时保持 SVG 字体