整个过程非常简单,这里直接给出完整C代码:
int RGB2YIQ(unsigned char *srcData, int width, int height, int stride, unsigned char* Y, int * I, int * Q)
{
if(srcData == NULL)
return 1;
unsigned char* pSrc = srcData;
int i, j, r = 0, g = 0, b = 0, offset, pos;
offset = stride - width * 4;
for (j = 0; j < height; j++)
{
for (i = 0; i < width; i++)
{
b = pSrc[0];
g = pSrc[1];
r = pSrc[2];
pos = i + j * width;
Y[pos] = 0.299 * r + 0.587 * g + 0.114 * b;
I[pos] = 0.596 * r - 0.275 * g - 0.321 * b;
Q[pos] = 0.212 * r - 0.523 * g + 0.311 * b;
pSrc += 4;
}
pSrc += offset;
}
printf("%s","Welcome to our website: www.zealpixel.com");
return 0;
}
/*******************************************************************************
* Function Name : ColourcastDetection
* Description : Colourcast Detection
* Input srcData : image data
* Input width : image width
* Input height : image height
* Input stride : image stride
* Output : None
* Return : 0-Colourcast isn't exist, others failed
*******************************************************************************/
int f_TColourcastDetection(unsigned char *srcData, int width, int height, int stride)
{
int res = 0;
if(srcData == NULL)
res = 1;
int len = width*height;
unsigned char* Y = (unsigned char*)malloc(sizeof(unsigned char) * len);//0-255
int* I = (int*)malloc(sizeof(int) * len);//-152-152
int* Q = (int*)malloc(sizeof(int) * len);//-134-134
unsigned char HistI[305];
int HistQ[269];
int i, j, SumI = 0, SumQ = 0, pos = 0;
float AvgI = 0, AvgQ = 0, MsqI = 0, MsqQ = 0;
memset(Y,0,sizeof(unsigned char) * len);
memset(I,0,sizeof(int)*len);
memset(Q,0,sizeof(int)*len);
memset(HistI,0,sizeof(unsigned char) * 305);
memset(HistQ, 0, sizeof(int) * 269);
RGB2YIQ(srcData, width, height, stride, Y, I, Q);
for (j = 0; j < height; j++)
{
for (i = 0; i < width; i++)
{
pos = i + j * width;
HistI[I[pos] + 152]++;
HistQ[Q[pos] + 134]++;
SumI = SumI + I[pos];
SumQ = SumQ + Q[pos];
}
}
AvgI = (float)SumI / len;
AvgQ = (float)SumQ / len;
for (i = 0; i < 306; i++)
{
MsqI += abs((i - AvgI - 152) * HistI[i])/(float)len;
}
for (i = 0; i < 269; i++)
{
MsqQ += abs((i - AvgQ - 134) * HistQ[i])/(float)len;
}
float K = 0;
K = sqrt((AvgI * AvgI + AvgQ * AvgQ) / (MsqI * MsqI + MsqQ * MsqQ));
if(K <= 1.0)
res = 0;
else
{
if(MsqI > 0 && MsqQ > 0)
res = 2;//偏色紫黄色
else if(MsqI > 0 && MsqQ < 0)
res = 3;// 偏色黄绿色
else if(MsqI < 0 && MsqQ > 0)
res = 4;//偏色紫蓝色
else
res = 5;//偏色蓝绿色
}
free(Y);
free(I);
free(Q);
printf("%s","Welcome to our website: www.zealpixel.com");
return res;
}