2013-03-17 64 views
3

我試圖隱蔽RGBHSI並恢復它。 (任務需要從頭開始。)RGB到HSI和HSI到RGB轉換

RGBHSI轉換,飽和度和強度輸出都很好。但我似乎沒有在順化的制定中遇到問題。 輸出示例:

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 

我用this calculator檢查我的輸出。

請讓我知道什麼是錯。謝謝。

#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; 
    } 
+1

請更正縮進。它會讓人們更關注你的問題,而不是試圖理解你的代碼。 – Synxis 2013-03-17 17:17:14

+1

數量-2147483648建議您有符號/無符號整數的問題(或者可能是鑄造的NaN/INF爲int。) – 2013-03-17 17:17:50

+2

要求人們在代碼中發現錯誤不是特別富有成效。您應該使用調試器(或者添加打印語句)來分析問題,追蹤程序的進度,並將其與預期發生的情況進行比較。只要兩者發生分歧,那麼你就發現了你的問題。 (然後如果有必要,你應該構造一個[最小測試用例](http://sscce.org)。) – 2013-03-17 17:19:12

回答

1

的問題是在這條線:

temp = (R - (G/2) - (B/2))/(sqrt((R*R) + (G*G) + (B*B) - (R*G) - (R*B) - (G*B))); 

R = G = B,那麼你有一個被零除:

R² - G² - B² - RG - RB - GB = R² + R² + R² - R² - R² - R² = 0 

實際上,我驚奇地發現這並沒有墜毀...

在這種情況下,只是分配0到色調。從您的鏈接中:

中性色 - 白色,灰色和黑色 - 爲方便起見設置爲0°。

1

從別人回答它看起來像有被零除問題時R = G = B當你計算temp而且從我可以告訴您正在使用degree s的三角函數,但他們都期待radian小號即:

#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 ; 
}