Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Roman_T_
New Contributor I
96 Views

ippiRGBToHSV_8u_C3R results and pseudocode

Jump to solution

Hi all,

I found the next pseudocode for ippiRGBToHSV_8u_C3R in IPP documentation

The conversion algorithm from RGB to HSV can be represented in pseudocode as follows:
// Value: V = max(R,G,B); // Saturation: temp = min(R,G,B); if V = 0 then // achromatics case S = 0//
H = 0 else // chromatics case S = (V - temp)/V // Hue: Cr = (V - R) / (V - temp) Cg = (V -
G) / (V - temp) Cb = (V - B) / (V - temp) if R = V then H = Cb - Cg if G = V then H = 2 + Cr - Cb if B = V
then H = 4 + Cg - Cr H = 60*H if H < 0 then H = H + 360
The computed H,S,V values are scaled to the full range of the destination data type

I realized this pseudocode in small console application

int GetHSV(int nR,int nG, int nB,int* pnH, int* pnS, int* pnV)
{
    int nH,nS,nV;
    int nCr,nCg,nCb;
    int nResult=0;
    int nTemp=0;
    nV=nR;
    if(nG>nV) nV=nG;
    if(nB>nV) nV=nB;
    nTemp=nR;
    if(nG<nTemp) nTemp=nG;
    if(nB<nTemp) nTemp=nB;
    if(nV==0)
    {
        nH=0;
        nS=0;
    }
    else
    {
        nS = (nV - nTemp);
        nCr=(nV-nR)/(nV-nTemp);
        nCg=(nV-nG)/(nV-nTemp);
        nCb=(nV-nB)/(nV-nTemp);
        if(nR==nV) nH=nCb-nCg;
        if(nG==nV) nH=2+nCr-nCb;
        if(nB==nV) nH=4+nCg-nCr;
        nH=nH*60;
        if(nH<0) nH=nH+360;
    }
    *pnH=(int)(nH*0.71);
    *pnS=nS;
    *pnV=nV;
    return nResult;
}

I also made a small console application, that uses  ippiRGBToHSV_8u_C3R  for color conversion.

The results are very strange. When we have simple colors like red, green, cyan, magenta, everything is fine, the results of two console applications are the same.

But when I try something else (for example RGB(198,97,27)), results are different.

Is my relization of pseudocode correct? If it's correct where can I get the real pseudocode of  ippiRGBToHSV_8u_C3R?

Best regards,
Roman

 

0 Kudos
1 Solution
Sergey_P_Intel1
Employee
96 Views

See right code below(int vs float):

int GetHSV_32f(int nR, int nG, int nB, int* pnH, int* pnS, int* pnV)

{

float nH, nS, nV;

float nCr, nCg, nCb;

float nResult = 0;

float nTemp = 0;

nV = nR;

if (nG>nV) nV = nG;

if (nB>nV) nV = nB;

nTemp = nR;

if (nG<nTemp) nTemp = nG;

if (nB<nTemp) nTemp = nB;

if (nV == 0)

{

nH = 0;

nS = 0;

}else{

nS = (nV - nTemp)/nV;

nCr = (nV - nR) / (nV - nTemp);

nCg = (nV - nG) / (nV - nTemp);

nCb = (nV - nB) / (nV - nTemp);

if (nR == nV) nH = nCb - nCg;

if (nG == nV) nH = 2 + nCr - nCb;

if (nB == nV) nH = 4 + nCg - nCr;

nH = nH * 60;

if (nH<0) nH = nH + 360;

}

*pnH = (int)(nH*0.71);

*pnS = nS*255.f;

*pnV = nV;

return nResult;

}

View solution in original post

2 Replies
Sergey_P_Intel1
Employee
97 Views

See right code below(int vs float):

int GetHSV_32f(int nR, int nG, int nB, int* pnH, int* pnS, int* pnV)

{

float nH, nS, nV;

float nCr, nCg, nCb;

float nResult = 0;

float nTemp = 0;

nV = nR;

if (nG>nV) nV = nG;

if (nB>nV) nV = nB;

nTemp = nR;

if (nG<nTemp) nTemp = nG;

if (nB<nTemp) nTemp = nB;

if (nV == 0)

{

nH = 0;

nS = 0;

}else{

nS = (nV - nTemp)/nV;

nCr = (nV - nR) / (nV - nTemp);

nCg = (nV - nG) / (nV - nTemp);

nCb = (nV - nB) / (nV - nTemp);

if (nR == nV) nH = nCb - nCg;

if (nG == nV) nH = 2 + nCr - nCb;

if (nB == nV) nH = 4 + nCg - nCr;

nH = nH * 60;

if (nH<0) nH = nH + 360;

}

*pnH = (int)(nH*0.71);

*pnS = nS*255.f;

*pnV = nV;

return nResult;

}

View solution in original post

Roman_T_
New Contributor I
96 Views

Hi Sergey,

Thank you very much for a fast reply!

 

Best regards,
Roman

Reply