开发者

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 the开发者_开发百科re a way to do this without simply looping over every pixel and flipping its value?


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.


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


In Matlab, by using not we can convert 1's into 0's and 0's into 1's.

inverted_binary_image = not(binary_image)


[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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜