int convertStretch(char * fileName,float * src, int width, int height, int channel, int showDim)
{
int i , j;
Mat temp;
if(0 == showDim || 1 == showDim || 2 == showDim || 3 == showDim)
{
float tmax,tmin;
temp = Mat(height, width, CV_8UC1);
float tempF;
tmax = src[0 + showDim];
tmin = src[0 + showDim];
float scale = 0;
for(i = 0; i < height; i ++)
{
for(j = 0; j < width; j ++)
{
tempF = src[(i * width + j) * channel + showDim];
if(tmax < tempF)
{
tmax = tempF;
}
if(tmin > tempF)
{
tmin = tempF;
}
}
}
printf(" %s max= %f min = %f\n", fileName, tmax, tmin);
scale = 255.0f / (tmax - tmin);
for(i = 0; i < height; i ++)
{
for(j = 0; j < width; j ++)
{
temp.data[i * width + j] = (unsigned char)((int)((src[(i * width + j) * channel + showDim] - tmin) * scale));
}
}
}else if(-1 == showDim)
{
float tmax0, tmax1, tmax2, tmin0, tmin1, tmin2;
temp = Mat(height, width, CV_8UC3);
float tempF0, tempF1, tempF2;
float scale0, scale1, scale2;
tmax0 = src[0];
tmin0 = src[0];
tmax1 = src[1];
tmin1 = src[1];
tmax2 = src[2];
tmin2 = src[2];
for(i = 0; i < height; i ++)
{
for(j = 0; j < width; j ++)
{
tempF0 = src[(i * width + j) * channel + 0];
tempF1 = src[(i * width + j) * channel + 1];
tempF2 = src[(i * width + j) * channel + 2];
if(tmax0 < tempF0)
{
tmax0 = tempF0;
}
if(tmin0 > tempF0)
{
tmin0 = tempF0;
}
if(tmax1 < tempF1)
{
tmax1 = tempF1;
}
if(tmin1 > tempF1)
{
tmin1 = tempF1;
}
if(tmax2 < tempF2)
{
tmax2 = tempF2;
}
if(tmin2 > tempF2)
{
tmin2 = tempF2;
}
}
}
printf(" %s max0= %f min0 = %f\n", fileName, tmax0, tmin0);
scale0 = 255.0f / (tmax0 - tmin0);
printf(" %s max1= %f min1 = %f\n", fileName, tmax1, tmin1);
scale1 = 255.0f / (tmax1 - tmin1);
printf(" %s max2= %f min2 = %f\n", fileName, tmax2, tmin2);
scale2 = 255.0f / (tmax2 - tmin2);
for(i = 0; i < height; i ++)
{
for(j = 0; j < width; j ++)
{
temp.data[(i * width + j) * 3 + 0] = (unsigned char)((int)((src[(i * width + j) * channel + 0] - tmin0) * scale0));
temp.data[(i * width + j) * 3 + 1] = (unsigned char)((int)((src[(i * width + j) * channel + 1] - tmin1) * scale1));
temp.data[(i * width + j) * 3 + 2] = (unsigned char)((int)((src[(i * width + j) * channel + 2] - tmin2) * scale2));
}
}
}
imshow(fileName, temp);
waitKey(0);
return 0;
}