How to save the image WITHOUT the white background using imwrite (advanced problem)
Show older comments
Hi
RGB = imread('bird1.png');
figure
imshow(RGB)
Then I get:

Then:
s = size(RGB);
rectangle('position',[1 1 s(2) s(1)], 'edgecolor', [1 0 0])
rectangle('position',[0 0 s(2) s(1)], 'edgecolor', [1 0 0])
I get:

Now I would like to save this image WITHOUT the white background, only the content within the red box (including the red box).
Imwrite needs to use 'RGB', however after the red box been added onto the image, RGB is no longer the original RGB. It WON'T be correct just to use
imwrite(RGB, 'bird1.png');
Could anyone provide correct solution to this question please?
3 Comments
Adam
on 3 Sep 2019
You can turn all the edge pixels in the image to red easily if that is what you want, but your question also talks about saving without the white background. An image is saved (in most formats I am aware of) as a 2d matrix of values. If the background isn't white what are you wanting to replace it with? You can't just have nothing there.
Salad Box
on 3 Sep 2019
Adam
on 3 Sep 2019
Ah well, that is different! None of the images you previously showed had any white background outside the red rectangle so I assumed you meant the white within it.
Accepted Answer
More Answers (1)
Johannes Fischer
on 3 Sep 2019
% read image as NxMx3 rgb matrix
RGB = imread('bird1.png');
imshow(RGB)
s = size(RGB);
rectangle('position',[1 1 s(2) s(1)], 'edgecolor', [1 0 0])
rectangle('position',[0 0 s(2) s(1)], 'edgecolor', [1 0 0])
% get a handle of the axis
F = getframe(gca);
% and save the color information of the axis, which is stored as RGB in
% the field 'cdata'
imwrite(F.cdata, 'bird2.png')
5 Comments
Salad Box
on 3 Sep 2019
Johannes Fischer
on 3 Sep 2019
Actually,
rectangle('position',[1 1 s(2)-1 s(1)-1], 'edgecolor', [1 0 0])
should be sufficient for the whole rectangle, because the 3rd and 4th entry for 'position' are the width and the height, and since your start at 1, these need to be deducted.
You're welcome :)
Image Analyst
on 3 Sep 2019
Salad Box, I recommend that you do not use Johannes solution. The reason is it's basically grabbing the bitmap from the displayed image. It is not the same size as the original -- I checked -- it's one pixel bigger. It's even worse (more the wrong size) should you ever resize your figure. I recommend you use my Answer instead because it will maintain the original image size. Just execute these two lines to show it:
[rows, columns, numberOfCOlorChannels] = size(RGB)
[rowsF, columnsF, numberOfCOlorChannelsF] = size(F.cdata)
rows =
305
columns =
200
numberOfCOlorChannels =
3
rowsF =
305
columnsF =
201
numberOfCOlorChannelsF =
3
Salad Box
on 3 Sep 2019
Johannes Fischer
on 3 Sep 2019
Interesting... it seems to be larger by one pixel in row or column dimension, depending on the input size. I thought it would keep the size as long as no resizing is performed.
But you're right, you're solution is more robust, and in terms of batch processing probably much faster.
Categories
Find more on Images 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!
