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

5 visualizzazioni (ultimi 30 giorni)
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 Commento
Jannik
Jannik il 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

Accedi per commentare.

Risposte (1)

Chenchal
Chenchal il 3 Nov 2017
Modificato: Chenchal il 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;
  1 Commento
Emma Tompkins
Emma Tompkins il 3 Nov 2017
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

Accedi per commentare.

Categorie

Scopri di più su Visual Exploration in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by