Matlab转换为具有上限和下限的二进制

时间:2012-05-04 22:00:08

标签: image matlab image-processing matrix

我是Matlab的新手,我正在尝试将灰度图像转换为二进制图像。内置函数im2bwI(I,level)只有一个阈值,可以使强度值低于黑色等级的所有内容都高,但是如果只有一定范围内的值变为黑色,我怎样才能做到这一点?例如,如何设置强度值为89的像素变为黑色,但值70和102变为白色?

2 个答案:

答案 0 :(得分:2)

您可以为此使用逻辑寻址。我假设0是黑色,255是白色,你在ImageMatrix中有你的灰度图像。请注意,白色和黑色的值取决于图像的颜色深度(6位,8位等)。

以下是代码:

%将所有89的值替换为0(黑色)。

ImageMatrix(ImageMatrix == 89) = 0;

%将所有70或102的值替换为255(白色)。

ImageMatrix(ImageMatrix == 70 | Imagematrix == 102) = 255;

使用MinValueMaxValue范围内的值替换为NewValue

%设置一些示例值(将值替换为192 ... 255和63):

MinValue = 192;
MaxValue = 255;
NewValue = 63;

ImageMatrix(ImageMatrix >= MinValue & ImageMatrix <= MaxValue) = NewValue;

答案 1 :(得分:0)

答案: (让我成为要处理的图像)

I(I == 89) = 0
I(I == 70 | I == 102) = 255

Real MATLAB测试(R2012a):

K>> I = ceil(rand( 12 ) * 34 + 69)

I =

    98    94   101    74   102    80    78    86    78   100   101    74
   100   103   102   100    72   101    99    98    88    96   101    87
    74    74    83    88    94    94    84   100    87   100    86    88
    96    90    85    91    99    84    94    96    70    81    77    85
    73    95    91    71    78    84    72    99    98    89    93   103
    88    71    83    97    90    71    76    93   100    81    99    89
   101    81    86    75    88    88    72    90    87    80    94    89
    99    73    86    99    86    74   101   102    81    70    75    77
    95   100    78    92    92    70    81    91    90    89    85    74
    93    86    95    88    70    92    86    83   100    77    82    71
    79    84    87    95    83    97    89    87    82    98    82    85
    80    93    92    84   100    89    85    85    97    87    79    99

K>> I == 89

ans =

     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     1     0     0     0     0     0
     0     0     0     0     0     1     0     0     0     0     0     0

K>> I == 70

ans =

     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     1     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     1     0     0
     0     0     0     0     0     1     0     0     0     0     0     0
     0     0     0     0     1     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0

K>> I == 102

ans =

     0     0     0     0     1     0     0     0     0     0     0     0
     0     0     1     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     1     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0

K>> I(I == 89) = 0

I =

    98    94   101    74   102    80    78    86    78   100   101    74
   100   103   102   100    72   101    99    98    88    96   101    87
    74    74    83    88    94    94    84   100    87   100    86    88
    96    90    85    91    99    84    94    96    70    81    77    85
    73    95    91    71    78    84    72    99    98     0    93   103
    88    71    83    97    90    71    76    93   100    81    99     0
   101    81    86    75    88    88    72    90    87    80    94     0
    99    73    86    99    86    74   101   102    81    70    75    77
    95   100    78    92    92    70    81    91    90     0    85    74
    93    86    95    88    70    92    86    83   100    77    82    71
    79    84    87    95    83    97     0    87    82    98    82    85
    80    93    92    84   100     0    85    85    97    87    79    99

K>> I(I == 70 | I == 102) = 255

I =

    98    94   101    74   255    80    78    86    78   100   101    74
   100   103   255   100    72   101    99    98    88    96   101    87
    74    74    83    88    94    94    84   100    87   100    86    88
    96    90    85    91    99    84    94    96   255    81    77    85
    73    95    91    71    78    84    72    99    98     0    93   103
    88    71    83    97    90    71    76    93   100    81    99     0
   101    81    86    75    88    88    72    90    87    80    94     0
    99    73    86    99    86    74   101   255    81   255    75    77
    95   100    78    92    92   255    81    91    90     0    85    74
    93    86    95    88   255    92    86    83   100    77    82    71
    79    84    87    95    83    97     0    87    82    98    82    85
    80    93    92    84   100     0    85    85    97    87    79    99