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
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