Removing the black background from a non-rectangular RGB image?
3 views (last 30 days)
Show older comments
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 Comment
Jannik
on 3 Nov 2017
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
Answers (1)
Chenchal
on 3 Nov 2017
Edited: Chenchal
on 3 Nov 2017
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;
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!