Backgroud Added to images after imwrap transformations
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
DGM
on 25 Dec 2023
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.
More Answers (1)
Sulaymon Eshkabilov
on 25 Dec 2023
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 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!