找到的相关代码,运行后点击窗口的“肤色检测”按钮会出现闪退,想了好久不知道问题在哪
函数代码
//显示肤色BMP图像
void ShowSkinBMPImage(char *Image, int *sImage, int wImage, int hImage, int xPos, int yPos)
{
int i,j;
int R,G,B;
for (i=0; i<hImage; i++){
for (j=0; j<wImage; j++){
R = G = B = 0;
if (SkinImage[i*wImage+j] == 1){
//获取像素rgb
R = (BYTE) Image[i*3*wImage+j*3];
G = (BYTE) Image[i*3*wImage+j*3+1];
B = (BYTE) Image[i*3*wImage+j*3+2];
}
SetPixel(hWinDC, j+xPos, i+yPos, RGB(R,G,B));//将指定坐标处的像素设为指定的颜色
}
}
}
//HSV颜色空间肤色检测
void SkindetectionHSV(char *srcImage, int *sImage, int width, int height)
{
int i, j;
float R,G,B;
float max = 0,min = 0;
float *h=0,*s=0,*v=0;
for (j = 0; j < height; j++){
for (i = 0; i < width; i++){
sImage[i*width+j] = 0;
}
}
for(j = 0; j < height; j++){
for(i = 0; i < width; i++){
R = (BYTE) srcImage[i*3*width+j*3];
G = (BYTE) srcImage[i*3*width+j*3+1];
B = (BYTE) srcImage[i*3*width+j*3+2];
//归一化
R=R/255;
G=G/255;
B=B/255;
//rgb转换为hsv
max = max(R,G,B);
min = min(R,G,B);
if(max == min)
*h = 0;
else if(max == R && G>=B)
*h = 60*((G-B)/(max-min));
else if(max == R && G<B)
*h = 60*((G-B)/(max-min)) + 360;
else if(max == G)
*h = 60*((B-R)/(max-min)) + 120;
else if(max == B)
*h = 60*((R-G)/(max-min)) + 240;
/*if(max == 0)
*s = 0;
else
*s = 1 - (min/max);
*v = max;*/
if ((*h >= 2) && (*h <= 13)){
sImage[i*width+j] = 1;
}
}
}
}
调用代码
case SkinPhoto://肤色检测
SkindetectionHSV(Image, SkinImage, ImageWidth, ImageHeight);
ShowSkinBMPImage(Image, SkinImage, ImageWidth, ImageHeight, 600, 0);
break;