I want to read an image into MATLAB, draw a rectangle on it, and then save the image.
Also, I’m just learning MATLAB–please be gentle. It seems like it should be simple, but I can’t seem to do it.
im = imread('image.tif'); imshow(im); rectangle('Position', [100, 100, 10, 10]); imwrite(im, 'image2.tif');
Even though I can see the rectangle on the image, the saved image does not display the rectangle. How can I save the image and have the rectangle show up?
FWIW, I’ve already tried saveas(), but that gives me a HUGE image. Is there a way to use saveas() and make the saved image the correct size?
There’s actually a bug at The MathWorks site about this issue. Too bad they don’t spell out a real answer (as, IMHO, holding up a ruler to your monitor is not a real solution).
Using the
printcommand, you must manually change the-rparameter until the size of the saved image matches the size of the input image. The-rparameter specifies the DPI of the saved image. Since most screens have different DPIs, there’s no one-size-fits-all solution.Use the code above, tweak the
-rparameter until it looks right, and voilà!