How to save a modified image in MATLAB?

I want to read an image in MATLAB, draw a rectangle on it, and then save the image.

Also, I'm just learning MATLAB - be careful. It seems to be easy, but I can't do it.

im = imread('image.tif'); imshow(im); rectangle('Position', [100, 100, 10, 10]); imwrite(im, 'image2.tif'); 

Even if I see a rectangle in the image, the saved image does not display the rectangle. How to save image and display rectangle?

FWIW, I already tried saveas() , but that gives me a HUGE image. Is there a way to use saveas() and make the saved image the right size?

+12
file-io matlab image-manipulation
Feb 22 '09 at 19:03
source share
8 answers

Actually an error on the MathWorks site about this problem. Too bad they don’t talk about the real answer (since IMHO, raising a ruler to your monitor is not a real solution).

Using the print command, you must manually change the -r option until the size of the saved image matches the size of the input image. The -r specifies the DPI of the saved image. Since most screens have different DPIs, there is no single solution for everyone.

 im = imread('image.tif'); f = figure, imshow(im, 'Border', 'tight'); rectangle('Position', [100, 100, 10, 10]); print(f, '-r80', '-dtiff', 'image2.tif'); 

Use the code above, configure the -r option until it looks correct, and voilà!

+10
Feb 22 '09 at 21:55
source share

The reason the rectangle is not displayed in the saved image is because you do not change the im variable, which stores the image data. A rectangle is simply a plot object displayed above the graph. You must change the image data yourself.

Typically, images read in MATLAB are loaded as an N-by-M-by-3 matrix (i.e., an N-by-M pixel image with RGB (red-green-blue) values ​​for each pixel). Typically, image data is a uint8 data type, so RGB values ​​range from 0 to 255. If you want to change the RGB value for a given pixel, you must do the following:

 im = imread('test.jpg'); % Load a jpeg image im(1,1,1) = 255; % Change the red value for the first pixel im(1,1,2) = 0; % Change the green value for the first pixel im(1,1,3) = 0; % Change the blue value for the first pixel imwrite(im,'new.jpeg'); % Save modified image 

There are different ways to change more than one pixel at a time (i.e., a rectangular area), which will require learning how to index into multidimensional arrays . For more details on how various types of images are read in MATLAB (i.e. truecolor versus indexed ), I would check the documentation for imread .

+18
Feb 22 '09 at 19:23
source share

to the question at the top, there is a fairly simple solution provided by matlab:

 % you so far im = imread('image.tif'); imshow(im); rectangle('Position', [100, 100, 10, 10]); % now you use "getframe" and "frame2im" f = getframe(gca); im = frame2im(f); imwrite(im,'image2.tif'); 

which worked fine for me when I also drew a rectangle in the image and tried to save it. If you want to continue working with it, just add

 imread('image2.tif'); 

and keep working with him :)

Regards, Laura

+12
Nov 06
source share

after jacobko's answer. Setting paper properties and paper properties, as well as axis units and position properties, usually gives me the desired results without having to adjust the resolution. In this way,

 >> im = imread('image.tif'); >> f = figure, imshow(im); >> r=rectangle('Position',[100, 100,10,10]); >> set(r,'edgecolor','b') % change the color of the rectangle to blue >> set(f,'units','centimeters','position',[1 1 2.5 2.5]) % set the screen size and position >> set(f,'paperunits','centimeters','paperposition',[1 1 2.5 2.5]) % set size and position for printing >> set(gca,'units','normalized','position',[0 0 1 1]) % make sure axis fills entire figure >> print(f, '-r80','-dtiff','image2.tif') 

The output image, image2.tif, will now be 2.5 cm by 2.5 cm with a resouttion of 80dpi with no border around the axis.

+7
Feb 23 '09 at 17:33
source share

If you want to save im, you must first change its value. I am not familiar with the rectangle function, but you can do the following (brute force):

 im = imread('image.tif'); im(100:110,100)=0; im(100:110,110)=0; im(100,100:110)=0; im(110,100:110)=0; imshow(im); imwrite(im, 'image2.tif'); 

Please note that the above code is for grayscale image, if your image is an RGB image, you will need to do the following:

  im(100:110,100,:)=0; .... 
+3
Feb 22 '09 at 19:23
source share

You might be able to use getframe to capture the getframe image from the picture window. I think you could pass the cdata and colormap fields of the structure returned by getframe to imwrite as the image and its color map, respectively.

+2
Feb 22 '09 at 20:01
source share
 [f,p] = uigetfile('*.*'); I = imread([p,f]); imwrite(I,'img12.tif');% 

Any name we can provide to save the image

It will be automatically saved in your folder, and you can view any image.

+2
Aug 30 '16 at 11:41
source share
 close all; clear; clc; r = 240 ; c = 320; fig = figure('Visible', 'off'); imshow( zeros(r,c) ); hold on; plot([c-fix(c/2),c-fix(c/2)],[r-fix(r/2),r-fix(r/2)],'r*', 'MarkerSize', 10 ); % Sets position and size of figure on the screen set(fig, 'Units', 'pixels', 'position', [100 100 cr] ); % Sets axes to fill the figure space set(gca, 'Units', 'pixels', 'position', [0 0 c+1 r+1 ]); % Sets print properties; Looks like 1 pixel = (3/4)th of a point set(fig, 'paperunits', 'points', 'papersize', [fix((c-1)*(3/4))+1 fix((r-1)*(3/4))+1]); set(fig, 'paperunits', 'normalized', 'paperposition', [0 0 1 1]); print( fig, sprintf('-r%d', ceil(72*(4/3))), '-dpng', 'image.png' ); im = imread( 'image.png'); figure; imshow(im); 
+1
May 29 '09 at 22:40
source share



All Articles