How to save the image WITHOUT the white background using imwrite (advanced problem)

12 vues (au cours des 30 derniers jours)
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 commentaires
Salad Box
Salad Box le 3 Sep 2019
Hi Adam,
The reason that I need to remove the border/background of the image is I have to put the image on to my own background (dark-grey or black). See example below.
bird4.png
One more requirement is that I have to add box around the image (in this case it is red box). It would look extremely odd/ inappropriate to present a white border between the red box and the black background as the left one in the above image.
Above bird image is just an example image.
In reality all my images are small.
iptsetpref('ImshowBorder','tight');
would work on large images but won't work on small images as stated in the official documents.
bird5.png
Please help.
Adam
Adam le 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.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 3 Sep 2019
Try this:
% To burn the color into the image itself.
[rows, columns, numberOfCOlorChannels] = size(RGB)
RGB(:, 1, 1:3) = repmat([255, 0, 0], rows, 1);
RGB(:, end, 1:3) = repmat([255, 0, 0], rows, 1);
RGB(1, :, 1:3) = repmat([255, 0, 0], columns, 1);
RGB(end, :, 1:3) = repmat([255, 0, 0], columns, 1);
imshow(RGB);
imwrite(RGB, filename);
It's much more robust than saving the bitmap in the overlay, which is subject to change whenever you resize the figure window.
  1 commentaire
Salad Box
Salad Box le 3 Sep 2019
Modifié(e) : Salad Box le 3 Sep 2019
I can confirm this method is a lot faster and safer.

Connectez-vous pour commenter.

Plus de réponses (1)

Johannes Fischer
Johannes Fischer le 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 commentaires
Salad Box
Salad Box le 3 Sep 2019
Actually as I used Johannes Fischer's method on batch size (approximate 300 images), my Matlab crashed.
I restarted my PC and Matlab, I got this message:
"Warning: MATLAB previously crashed due to a low-level graphics error. To
prevent another crash in this session, MATLAB is using software OpenGL instead
of using your graphics hardware. To save this setting for future sessions, use
the opengl('save', 'software') command. For more information, see Resolving
Low-Level Graphics Issues. "
>>
I hope this has got nothing to do with your method Johannes Fischer.
Johannes Fischer
Johannes Fischer le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by