簡體   English   中英

用二進制蒙版蒙版RGB圖像

[英]Masking an RGB Image with Binary Mask

我在讀入的MATLAB中有一個RGB圖像(M x N x 3矩陣)。對於圖像,我也有一個二進制蒙版(M x N矩陣),對於某些感興趣的區域而言,它僅為0,而在其他區域則為1。

我試圖弄清楚如何用該二進制蒙版遮罩RGB圖像。 我嘗試過更改數據類型(使用double或uint8來查看結果是否發生更改,但有時它們不會更改或出現錯誤),並且我嘗試使用了諸如conv2,immultiply,imfilter等各種函數。 。

我目前要做的是嘗試將蒙版單獨應用(因為它的大小為M x N)到原始圖像的每個R,G和B通道。 掩碼為0的任何地方,我都希望在原始圖像中精確地為0,而掩碼為1的任何地方,我只想單獨留下。

到目前為止,上述所有功能似乎都沒有起作用。 顯然,我知道可以使用的方法是,如果我只是對所有這些進行了遍歷並進行了for循環,但這會很糟糕,因為MATLAB具有這些圖像函數,但是我似乎無法使它們起作用。

有時,imfilter或immultiply(取決於我如何弄亂圖像)只會使MATLAB完全停頓並崩潰。 有時它們很快完成,但是我要么得到全白圖像,要么得到全黑圖像(通過imshow和imagesc)。

我已經檢查過以確保我的圖像通道的大小與蒙版匹配,並且已經檢查了圖像和蒙版中的值是否正確。 我似乎無法使實際的遮罩操作正常工作。

有什么想法嗎? 也許我在MATLAB的規則中缺少什么?

這是當前的嘗試:

% NOTE: The code may not be "elegant" but I\'ll worry about optimization later.
%
% Setup image and size
image = imread(im);
[numrows, numcols, temp] = size(image); % not used currently

% Request user polygon for ROI
bw = roipoly(image);

% Set up the mask -- it is indeed all 0's and 1's
t = double(imcomplement(bw));

% "Mask" the image
z = double(image);    % Double to match up with t as a double
z(:, :, 1) = imfilter(z(:, :, 1), t);
z(:, :, 2) = imfilter(z(:, :, 2), t);
z(:, :, 3) = imfilter(z(:, :, 3), t);
imshow(z); figure; imagesc(z);

=================

編輯

發現以下作品:

im_new = im_old .* repmat(mask, [1,1,3]);   % if both image and mask are uint8
imshow(im_new);

您在imfilter()濫用imfilter() Imfilter用於線性濾波器操作,而不用於屏蔽或閾值化。 最好這樣做:

z = image;          % image() is also a function. 
                    % Overwriting this name should be avoided

% Request user polygon for ROI
bw = roipoly(z);

% Create 3 channel mask
mask_three_chan = repmat(bw, [1, 1, 3]);

% Apply Mask
z(~mask_three_chan) = 0;

% Display
imshow(z);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM