2011-03-11 60 views
4

我有一個二進制圖像,需要將所有的黑色像素轉換爲白色像素,反之亦然。然後我需要將新圖像保存到文件中。有沒有辦法做到這一點,而不是簡單地遍歷每個像素並翻轉其價值?如何在MATLAB中反轉二進制圖像?

+0

向我們展示一些代碼! – Benjamin 2011-03-11 18:49:02

+0

這取決於[你正在處理什麼樣的圖像](http://www.mathworks.com/help/techdoc/creating_plots/f2-10709.html)(RGB,索引,灰度,二進制),但是答案可能已經在這裏覆蓋:[我如何反轉灰度圖像並將其轉換爲二進制圖像在MATLAB?](http://stackoverflow.com/questions/2980860/how-do-i-invert-a-灰度圖像,並將它轉換爲二進制圖像在MATLAB中) – gnovice 2011-03-11 18:49:03

+0

@gnovice我正在處理二進制圖像。我想要移動圖像中的所有像素,並將0改爲1,反之亦然,但我不是Matlab專家,我不知道如何去做。感謝您的回覆。 – 2011-03-11 18:55:30

回答

15

如果你只用零和一的二進制圖像binImage,有一些簡單的方法來得到反效果:

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

然後,只需使用功能IMWRITE保存倒影。

+0

非常感謝。 – 2011-03-11 19:18:51

1

您可以使用imcomplement matlab函數。說你有B,則二值圖像,

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 
-2

[文件名,路徑名] = uigetfile({ '* BMP。'}, '文本作爲圖像');

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

與前面介紹的解決方案相比,這是一個非常低效的解決方案,甚至不考慮所請求的圖像保存。 – mikkola 2015-11-30 17:29:08

0

在Matlab中,通過使用not我們可以1的轉換成0和0到1的

inverted_binary_image = not(binary_image) 
+0

'not'函數在使用'〜'運算符時正是所謂的。 – gnovice 2017-11-01 16:13:18

相關問題