![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1575852/image.png)
Backgroud Added to images after imwrap transformations
조회 수: 2 (최근 30일)
이전 댓글 표시
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1575777/image.png)
After the transformation the below image is displayed
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1575782/image.png)
I do not want this background to appear
댓글 수: 0
채택된 답변
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1575852/image.png)
추가 답변 (1개)
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')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!