opencv Mat 显示不同数据类型图像

        单通道float类型Mat在存数据时需要归一化, 即图像数据需要归一化到0~1之间。这是float类型图片的灰度空间。

             比如:

Mat a=Mat::zeros(200,200,CV_32FC1);
	  for(int row=0;row!=a.rows;row++)
		for(int col=0;col!=a.cols;col++)
		{
		a.at<float>(row,col)=0.5;
		}
		imshow(" test ",a);
		waitKey(0);
                        

                            若float数据<0,则会显示为黑色

Mat a=Mat::zeros(200,200,CV_32FC1);
	for(int row=0;row!=a.rows;row++)
		for(int col=0;col!=a.cols;col++)
		{
		a.at<float>(row,col)=-8;
		}
		imshow(" test ",a);
		waitKey(0);
                   

                             若float数据>1,则会显示为白色

 Mat a=Mat::zeros(200,200,CV_32FC1);
	for(int row=0;row!=a.rows;row++)
		for(int col=0;col!=a.cols;col++)
		{
		a.at<float>(row,col)=2;
		}
		imshow(" test ",a);
		waitKey(0); 
                   
 

    单通道uchar 类型Mat在显示图片时,Mat数据应在uchar数据范围内,即0~255。  若数据值不在0~255内,则Mat会以256为周期,把数据平移至0~255范围内。  比如 数据为256,则Mat会把数据转化成0,因此显示为黑色。

    <pre name="code" class="html">Mat a=Mat::zeros(200,200,CV_8UC1);
    for(int row=0;row!=a.rows;row++)
         for(int col=0;col!=a.cols;col++)
		{
		a.at<uchar>(row,col)=256;
		}
		imshow(" test ",a);
               waitKey(0);
                               
 

                                又如数据为-1,则Mat会把数据转换为255,显示全白。

</pre><pre name="code" class="html">Mat a=Mat::zeros(200,200,CV_8UC1);
    for(int row=0;row!=a.rows;row++)
         for(int col=0;col!=a.cols;col++)
		{
		a.at<uchar>(row,col)=-1;
		}
		imshow(" test ",a);
               waitKey(0);
      

 

                         
另外,可以用下面 这句话实现转换矩阵类型转换和整体元素线性变化。
   
      The method converts source pixel values to the target data type. saturate_cast\<\> is applied at
    the end to avoid possible overflows:


    \f[m(x,y) = saturate \_ cast<rType>( \alpha (*this)(x,y) +  \beta )\f]
    @param m output matrix; if it does not have a proper size or type before the operation, it is
    reallocated.
    @param rtype desired output matrix type or, rather, the depth since the number of channels are the
    same as the input has; if rtype is negative, the output matrix will have the same type as the input.
    @param alpha optional scale factor.
    @param beta optional delta added to the scaled values.
    比如:
          Mat A,B;
	A.convertTo(B,type,a,b);
       // B.(y,x)=A.(y,x)*a+b   
       //关于通道和数据类型:
       //输出矩阵通道(B)和输入矩阵通道(A)一样,type用来决定输出矩阵的数据类型。type =-1 则B将会和A同数据类型,也可根据自己的需要填写别的数据类型,比如CV_8U。

 


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值