Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I invert a binary image in MATLAB?

I have a binary image and need to convert all of the black pixels to white pixels and vice versa. Then I need to save the new image to a file. Is there a way to do this without simply looping over every pixel and flipping its value?

like image 437
Ofir A. Avatar asked Mar 11 '11 18:03

Ofir A.


2 Answers

If you have a binary image binImage with just zeroes and ones, there are a number of simple ways to invert it:

binImage = ~binImage;
binImage = 1-binImage;
binImage = (binImage == 0);

Then just save the inverted image using the function IMWRITE.

like image 113
gnovice Avatar answered Oct 04 '22 22:10

gnovice


You can use imcomplement matlab function. Say you have a binary image b then,

bc = imcomplement(b); % gives you the inverted version of b
b = imcomplement(bc); % returns it to the original b
imwrite(bc,'c:\...'); % to save the file in disk
like image 40
Myke Avatar answered Oct 04 '22 20:10

Myke