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

조회 수: 4 (최근 30일)
Emma Tompkins
Emma Tompkins 2017년 11월 3일
댓글: Emma Tompkins 2017년 11월 3일
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