图片出错成这样
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
void cvtcolorRGB2HSV(unsigned char *src,unsigned char *dst,int width,int height)//,int widthstep)
{ int LineByte = WIDTHBYTES(width*3*8);
float min,max;
float b,g,r;
float h,s,v;
float delta;
for( int i = 0;i < height;i++)
{
//cout<<endl;
for(int j = 0;j < width;j++)
{
b = (float)src[i*LineByte + j*3+0];
g = (float)src[i*LineByte + j*3+1];
r = (float)src[i*LineByte + j*3+2];
min = MIN( r, g);
min = MIN(min, b);
max = MAX( r, g);
max = MAX(max, b);
v = max;
delta = max -min;
if( max != 0 )
{
s = delta / max;
}
else
{
s = 0;
h = -1;
dst[i*LineByte+j*3 + 0] = (unsigned char)h;
dst[i*LineByte+j*3 + 1] = (unsigned char)s;
dst[i*LineByte+j*3 + 2] = (unsigned char)v;
return ;
}
if( r == max )
{
h = (g - b)/ delta;
}
else if(g = max)
{
h = 2 + (b - r)/delta;
}
else
{
h = 4 + (r - g)/delta;
}
h *= 60;
if( h < 0 )
{
h += 360;
}
dst[i*LineByte + j*3 + 0] = (unsigned char)h;
dst[i*LineByte + j*3 + 1] = (unsigned char)s;
dst[i*LineByte + j*3 + 2] = (unsigned char)v;
}
}
}
int main(int argc,char *argv[])
{
IplImage *temp = cvLoadImage("lena.bmp",1);
IplImage *temp1 = cvCreateImage(cvGetSize(temp),temp->depth,temp->nChannels);
unsigned char *src = (unsigned char *)(temp->imageData);
unsigned char *dst = (unsigned char *)(temp1->imageData);
int Width = temp->width;
int Height = temp->height;
cvtcolorRGB2HSV(src,dst,temp->width,temp->height);
cvNamedWindow("1");
cvShowImage("1",temp1);
cvWaitKey(0);
return 0;
}
用这个程序出来的图片 几乎全红 与用opencv中自带装换的图片完全不一样 是hsv的排列存储顺序的问题么 求大大指点