MATLAB Answers

How can I reverse black and white in a grayscale image?

468 views (last 30 days)
I have a grayscale image and I'm trying to reverse the black and white in it as an exercise. I think I'm supposed to use for loops in some way to access the colormap so the entire image matrix is composed of 1's and 0's (at which point I could switch the two by subtracting 1 from all values) but I don't know how to get this matrix in the first place. Thank you!
  2 Comments
Cedric Wannaz
Cedric Wannaz on 2 Jul 2014
And here is a second hint:
>> I = imread('board.tif');
>> J = rgb2gray(I);
>> size(J)
ans =
648 306
>> min(J(:))
ans =
0
>> max(J(:))
ans =
255
so pixels' "grayscale" level seem to be coded with (unsigned) integers in the range 0 to 255.
Note that you can visualize J with
>> imshow( J ) ;
Now maybe there is an operation that you could perform on J which would reverse the scale ..

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 2 Jul 2014
Try this
inverseGrayImage = uint8(255) - grayImage;
  1 Comment
Mark Quesada
Mark Quesada on 26 Mar 2019
You animal, that was spot on; Worked right out of the gate!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!