Removing the black background from a non-rectangular RGB image?

4 次查看(过去 30 天)
I have a non-rectangular RGB TIFF that before I bring into matlab has no background. When I bring it into Matlab there is a back background (value 0). I want to be able to remove this background, so I guess set all 0 values to NAN?
How do I go about this? I tried
I(I(:,:)==0) = NaN;
But I end up with a one row matrix full of NaN and I don't know if this in looking at all three layers.
  1 个评论
Jannik
Jannik 2017-11-3
maybe try to use a cell
img = imread('some_rgb_image');
newimg = cell(1,1,1);
for i = 1:size(img,2)
for j = 1:size(img,1)
if (img(i,j,1) ~= 0 && img(i,j,2) ~= 0 && img(i,j,3) ~=0)
newimg{i,j,1} == img(i,j,1);
newimg{i,j,2} == img(i,j,2);
newimg{i,j,3} == img(i,j,3);
end
end
end
it creates a cell where the black pieces are empty

请先登录,再进行评论。

回答(1 个)

Chenchal
Chenchal 2017-11-3
编辑:Chenchal 2017-11-3
By definition in matlab the data is a rectangular matrix. you are wanting to change the transparency of image so the background will show. here is code snippet:
% code
yoda = imread('Yoda_5_transp_gif.png');
h=subplot(1,2,1);
set(get(h,'Parent'),'Color',[0.7 1 1]) % background color
im = imshow(yoda);
%background is white, should show background color
subplot(1,2,2)
im = imshow(yoda)
yodaNan = ones(size(yoda,1),size(yoda,2));% all colored area should be opaque (1)
yodaNan(yoda(:,:,1)==255) = 0;% all 'non-color [255 here, NaN in your case] should be transparent (0)
im.AlphaData = yodaNan;
  1 个评论
Emma Tompkins
Emma Tompkins 2017-11-3
it did work,
h=subplot(1,2,1);
set(get(h,'Parent'),'Color',[0.7 1 1]) % background color
im = imshow(I);
%background is white, should show background color
subplot(1,2,2)
im = imshow(I)
INan = ones(size(I,1),size(I,2));% all colored area should be opaque (1)
INan(I(:,:,1)==0) = NaN;% all 'non-color [255 here, NaN in your case] should be transparent (0)
im.AlphaData = INan;
but then when I want to look at INan along I just see the black background with the rest white. Which unfortunately isn't what I want as I don't want the background to be included in the rest of my analysis. But thank you

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by