필터 지우기
필터 지우기

Backgroud Added to images after imwrap transformations

조회 수: 2 (최근 30일)
Farhan
Farhan 2023년 12월 25일
댓글: Farhan 2023년 12월 25일
I am performing simple transformations but the Matlab adds backgroud to the image to make it Rectangle. The image has transparent backgroud. I know how to read the image and remove background but I fail to remove it after the transformations
I=imread('d.png');
I=flipud(I);
T = [1 0
1 1];
figure;
T(3,3)=1;
[Iw,R]=imwarp(I, affine2d(T'));
h=imshow(Iw);
axis on xy;
set(h,'XData',R.XWorldLimits,'YData',R.YWorldLimits);
set(gca,'XLim',R.XWorldLimits,'YLim',R.YWorldLimits);
The following is being used
After the transformation the below image is displayed
I do not want this background to appear

채택된 답변

DGM
DGM 2023년 12월 25일
Your image has alpha, but you're not using it.
% you need to read the whole image
[I,~,alpha] = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1575777/image.png');
% i don't know why you're flipping this,
% but the image will be upside-down when displayed (unless you display it upside-down)
% and it will be upside-down when you save it
I = flipud(I);
alpha = flipud(alpha);
% build T
T = [1 0; 1 1];
T(3,3) = 1;
% you could attach the alpha and transform this as a single RGBA array
% but imshow() and imwrite() won't know what to do with it.
[Iw,R] = imwarp(I, affine2d(T'));
[alphaw,~] = imwarp(alpha, affine2d(T'));
% display it
h = imshow(Iw);
h.AlphaData = alphaw; % this is the only way you can use alpha in imshow()
axis on xy; % you're displaying it upside-down
set(h,'XData',R.XWorldLimits,'YData',R.YWorldLimits);
set(gca,'XLim',R.XWorldLimits,'YLim',R.YWorldLimits);
% save it with alpha
imwrite(Iw,'out.png','alpha',alphaw)
As the comments state, you're creating an upside-down image, but you're also viewing it upside-down. I don't know what that means to you.
  댓글 수: 2
Farhan
Farhan 2023년 12월 25일
I didn't understood teh code much, Thanks for such clear comments now I know much of the code. I just wanted to do transformation someone helped me wrote this code. I will change it as you suggested. Thanks
Farhan
Farhan 2023년 12월 25일
played a bit with your code, now because of your comments I understand what I am doing. Thanks a lot again, it really helped a lot

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 12월 25일
Here is the complete code (your diamond image was used with DIAM.png name):
I=imread('DIAM.png');
I=flipud(I);
T = [1 0
1 1];
figure
imshow(I)
title('Original')
figure;
T(3,3)=1;
[Iw,R]=imwarp(I, affine2d(T'));
% Convert the Image to Grayscale:
Gray_img = rgb2gray(Iw);
% Create a Mask (called B_mask) for the Background Color:
B_mask = Gray_img ==0; % Adjust the threshold based on the transformed image
% Apply the mask (B_mask) to remove the background color
Final_img = Iw;
Final_img(repmat(B_mask, [1, 1, 3])) = 255; % Set background pixels to white
h=imshow(Final_img);
axis on xy;
set(h,'XData',R.XWorldLimits,'YData',R.YWorldLimits);
set(gca,'XLim',R.XWorldLimits,'YLim',R.YWorldLimits);
title('Transformed Image')

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by