0
votes

I'm rather new to Matlab and am trying to convert a grayscale image into binary. The built in function im2bwI(I,level) only has one threshold that will make everything with a lower intensity value than the level black and everything higher white, but how can I make it so only values in a certain range become black? For example, how do I set it so pixels with intensity value 89 become black, but values 70 and 102 become white?

2

2 Answers

2
votes

You can use logical addressing for this. I'll assume that 0 is black and 255 is white and you have your grayscale image in ImageMatrix. Please note that the values of white and black depend on the color depth (6 bit, 8 bit etc.) of the image.

Here's the code:

% Replace all values of 89 with 0 (black).

ImageMatrix(ImageMatrix == 89) = 0;

% Replace all values of 70 or 102 with 255 (white).

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

For replacing a range of values from MinValue to MaxValue with NewValue:

% set some example values (replace values 192 ... 255 with 63):

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

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

Answer: (let I be the image to be processed)

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

Real MATLAB test(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