图像数据拉伸显示2(可以显示任何通道,也可以显示全部通道)


// 本程序输入一个src float 类型的数据,经过数据的拉伸显示为图片, 用到opencv 库中的显示
//fileName 显示窗口的名字
// src 为 若为CV_32FC1 则输入为 mat.data
// src 为float 类型的buffer 数据
// width src 的宽
// height src 的高
// channel 通道数
// showDim 要显示的通道 , 当showDim为-1 时候,显示彩色图像,即三通道显示
//
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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值