zoukankan      html  css  js  c++  java
  • Matlab 图像处理中出现纯黑或纯白是怎么回事?

    在图像处理中,经常会出现这图像显示为纯黑或者纯白的情况,这其实是数据类型转换时出错导致的。

    所涉及到的函数主要包括以下几个

    • imread
    • imshow
    • double、im2double
    • uint8、im2uint8

    imread

    图像文件经过 imread 函数读取后,获取到的图像数据类型为 uint8 。

    clear;close all;clc
    
    %% 导入图像
    % https://www.ece.rice.edu/~wakin/images/lena512.bmp
    img = imread('lena512.bmp');
    
    %% 查看变量
    whos img
    
    %% 输出结果
    %{
    
      Name        Size              Bytes  Class    Attributes
    
      img       512x512            262144  uint8  
    
    %}
     
    

    double

    double 是 matlab 默认的数据类型,即双精度浮点数据,允许小数点的和负数存在。

    在 matlab 中使用 double() 函数实现数据转型为 double 类型。

    clear;close all;clc
    
    %% 默认数据类型为 double
    x1 = 123.5;
    %{
      Name      Size            Bytes  Class     Attributes
    
      x1        1x1                 8  double  
    %}
    
    %% 默认数据转 double
    x2 = double(x1);
    whos x2
    %{
      Name      Size            Bytes  Class     Attributes
    
      x2        1x1                 8  double              
    %}
    
    %% uint8 转double
    x3 = uint8(245);
    whos x3
    %{
      Name      Size            Bytes  Class    Attributes
    
      x3        1x1                 1  uint8    
    %}
    
    x4 = double(x3);
    whos x4
    %{
      Name      Size            Bytes  Class     Attributes
    
      x4        1x1                 8  double   
    %}
    

    结论

    由于 double 属于浮点数据,精度高,取值范围广,因此输入数据转换为 double 类型后,数值一般不会发生改变,只是变成了浮点型数据,即数据允许小数点和负数存在了。

    uint8

    uint8 是一种无符号整型数据。取值范围为 0 到 255 之间的整数。

    double --> uint8

    clear;close all;clc
    
    x = -100:1:350;
    y = uint8(x);
    
    plot(x,y);
    xlabel('x');ylabel('y')
    

    值得注意的是,这里的取整采用的是四舍五入的方式,即就近取整原则。

    clear;close all;clc
    
    x = -1:0.1:2;
    y = uint8(x);
    
    plot(x,y);
    xlabel('x');ylabel('y')
    

    结论

    对于 double 类型的输入数据,uint8 将其 0 - 255 部分的数据取整,溢出的部分就近处理为 0 或 255。

    im2uint8

    uint8 --> im2uint8

    clear;close all;clc
    
    x = uint8(0:255);
    y = im2uint8(x);
    
    plot(x,y);
    xlabel('x');ylabel('y')
    axis square
    axis tight
    

    double --> im2uint8

    clear;close all;clc
    
    x = double(-100:255);
    y = im2uint8(x);
    
    plot(x,y);
    xlabel('x');ylabel('y')
    axis square
    axis tight
    

    clear;close all;clc
    
    x = double(0:0.1:1);
    y = im2uint8(x);
    
    plot(x,y);
    xlabel('x');ylabel('y')
    axis square
    axis tight
    

    结论

    1. 对于 uint8 类型数据,im2uint8 不对其做变换。
    2. 对于 double 类型数据,im2uint8 将其 0 - 1 部分重新映射到 0 - 255 之间,然后对于溢出的部分,就近处理为 0 或 255。

    im2double

    double --> im2double

    clear;close all;clc
    
    x = double(-255:255);
    y = im2double(x);
    
    plot(x,y);
    xlabel('x');ylabel('y')
    axis square
    axis tight
    

    uint8 --> im2double

    clear;close all;clc
    
    x = uint8(0:255);
    y = im2double(x);
    
    plot(x,y);
    xlabel('x');ylabel('y')
    axis square
    axis tight
    

    结论

    1. 对于 double 类型数据,im2double 不对其做变换。
    2. 对于 uint8 类型数据,im2double 将其 0 - 255 部分重新映射到 0 - 1 之间,这里 uint8 数据本身有溢出限制,因此不存在小于 0 或 大于 1 的部分。

    imshow

    uint8 --> imshow

    clear;close all;clc
    x =uint8(0:255);
    y = repmat(x,length(x),1);
    imshow(y)
    

    double --> imshow

    clear;close all;clc
    x =double(-255:255);
    y = repmat(x,length(x),1);
    imshow(y)
    

    clear;close all;clc
    x =double(0:0.01:1);
    y = repmat(x,length(x),1);
    imshow(y)
    

    结论

    1. 对于 uint8 数据,imshow 直接按照 0 - 255 将数值映射到灰度级上,0 对应纯黑,255 对应纯白,中间为渐变灰。
    2. 对于 double 数据,imhsow 仅保留其 0 - 1 之间的数值,并映射到灰度级上,0 对应纯黑,1 对应纯白,中间为渐变灰;对于溢出的部分,就近处理到 0 或 1 ,显示为纯黑或纯白。

    图像数据转换关系分析

    根据以上结论,可以总结出在图像处理中数据类型的转换类型关系图。

    当读取了一张图像后,数据就来到了图中的 uint8(0,255) 这个节点,表示数据类型为 uint8,取值范围为 0 到 255 ,然后,它可以使用前面提到的函数在 double 和uint8 之间任意地相互转换,但若想正常显示,则需要转换到 uint8 ,取值范围覆盖 0 到 255 之间,或转换到 double 取值范围覆盖 0 到 1。若无法对应以上规则,则有可能出现纯黑或者纯白的情况。

    参考资料

    1. https://www.cs.utah.edu/~germain/PPS/Topics/Matlab/uint8.html
    2. https://ww2.mathworks.cn/help/matlab/matlab_prog/integers.html
    3. https://ww2.mathworks.cn/help/matlab/ref/double.html
    4. https://ww2.mathworks.cn/help/matlab/matlab_prog/floating-point-numbers.html
    5. https://ww2.mathworks.cn/help/matlab/ref/imread.html
    6. https://www.ece.rice.edu/~wakin/images/
    © 版权声明
    文章版权归作者所有,未经允许请勿转载。
  • 相关阅读:
    HDU 2853 (KM最大匹配)
    HDU 2852 (树状数组+无序第K小)
    HDU 2851 (最短路)
    HDU 2846 (AC自动机+多文本匹配)
    MyBatis使用示例
    Hessian示例:Java和C#通信
    SQL Server2005配置同步复制
    【问】如何应对关系型数据库中列的不断增加
    Prolog学习:数独和八皇后问题
    Prolog学习:基本概念
  • 原文地址:https://www.cnblogs.com/gshang/p/14760358.html
Copyright © 2011-2022 走看看