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
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
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;
참고 항목
카테고리
Help Center 및 File Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!