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

254 ビュー (過去 30 日間)
Clara
Clara 2014 年 7 月 2 日
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 件のコメント
Cedric
Cedric 2014 年 7 月 2 日
Here is a hint
>> A = randi(5 , 3, 4)
A =
5 5 2 5
5 4 3 1
1 1 5 5
>> 5-A
ans =
0 0 3 0
0 1 2 4
4 4 0 0
Here you see that 5-A operates on the whole array A, without the necessity to implement a loop.
Cedric
Cedric 2014 年 7 月 2 日
編集済み: Cedric 2014 年 7 月 2 日
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 ..

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2014 年 7 月 2 日
Try this
inverseGrayImage = uint8(255) - grayImage;
  1 件のコメント
Mark Quesada
Mark Quesada 2019 年 3 月 26 日
You animal, that was spot on; Worked right out of the gate!

サインインしてコメントする。

その他の回答 (1 件)

Roche de Guzman
Roche de Guzman 2021 年 1 月 14 日
I = imcomplement(I)
  1 件のコメント
Victor Mtsimbe Norrild
Victor Mtsimbe Norrild 2021 年 3 月 17 日
You animal, that was spot on; Worked right out of the gate!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBlue についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by