How to convert binary image to MATLAB?

I have a binary image and you need to convert all 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 looping around each pixel and not reversing its value?

+4
source share
4 answers

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

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

Then just save the inverted image using the IMWRITE function.

+15
source

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 
+1
source

In Matlab, using not , we can convert 1 to 0 and 0 to 1.

 inverted_binary_image = not(binary_image) 
0
source

[filename, pathname] = uigetfile ({'*. bmp'}, 'Text as image');

 img=imread(filename); img=im2double(img); [r,c,ch]=size(img); %imshow(img); invert_img=img; if(ch==1) for i=1:r for j=1:c if(invert_img(i,j)==0) invert_img(i,j)=1; else invert_img(i,j)=0; end end end end 
-2
source

Source: https://habr.com/ru/post/1343358/


All Articles