输出RGB到HSI转换错误
incorrect output RGB to HSI convertion
我正在尝试将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;
}
您使用r
, g
和b
计算norm
,但它们在该点始终为0。
#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;
outputFile.open("RGB_HSI.txt");
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++) {
double norm = 0;
double omega = 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;
double r = 0;
double g = 0;
double b = 0;
int resultHue = 0;
int resultSaturation = 0;
int resultIntensity = 0;
//Intensity
intensity = (double) (R + G + B) / (3.0*255);
//norm colours
norm = sqrt((R*R) + (G*G) + (B*B));
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(((r-g) * (r-g)) + (r-b)*(g-b));
omega = acos(omega);
if (B <= G) {
hue = omega;
}
else if (B > G) {
hue = 2 * M_PI - omega;
}
}
//convert it to degrees
resultHue = (int) round((hue * 180.0) / M_PI);
resultSaturation = (int) (saturation);
resultIntensity = (int) round(intensity * 255);
if ((R == 0) && (G == 0) && ( B== 0 )) {
resultHue = 0;
resultSaturation = 0;
resultIntensity = 0;
}
outputFile<<"Red = "<<R<<", Green = "<<G<<", Blue = "<<B<<endl;
outputFile<<"Hue = "<<resultHue<<", Saturation = "<<resultSaturation<<", Intensity = "<<resultIntensity<<endl;
}
outputFile.close();
cout << "nRGB_HSI printed as text file: RGB_HSI.textn";
return 0;
}
相关文章:
- C++使用params创建线程函数会导致转换错误
- 为什么g++在未执行的代码处标记强制转换错误
- 为什么C++在将浮点数转换为字符时没有显示缩小转换错误?
- 如何在构建对象堆栈时解决转换错误?
- 执行具有转换错误的过程
- 指向类成员函数的指针中存在类型转换错误
- 为什么我在这里收到C++没有已知的转换错误?
- 增加图形转换错误
- 尝试向 COM 对象添加另一个接口时出现静态强制转换错误 C2440
- 返回带有另一个类的数据成员的构造函数?遇到转换错误?
- 指针和双重转换错误,代码没有
- 自定义函数转换错误?
- 将 C 程序转换为具有大量字符* 和无符号字符转换错误的C++
- 如何在我使用过的Arduino库之一中固定构造函数中的转换错误
- 试图使用strCMP时的char转换错误
- 我在 TSP 的C++解决方案中遇到转换错误
- "double(*)(string, double, double)"尝试传递函数C++时"double"转换错误
- 此语法中的构造函数转换错误
- Opengl 精度转换错误编译错误 E0415
- 无效的转换错误和无效的类型