MATLAB: How to add permanent outlines, polygons, rectangles, etc to the images with the Image Processing Toolbox 6.2 (R2008b)

Image Processing Toolbox

I have an image and I would like to use the IMPOLY function to outline a certain part of it. I then want to save the image, in its original format and resolution, with the polygon outline I created.
I tried using IMWRITE but it only saves the original image, not the line data that I added.
SAVEAS and GETFRAME are also not satisfactory as these methods can lose resolution based on the size of the image on my screen.

Best Answer

It is possible to use a combination of tools in the Image Processing Toolbox to permanently modify an image matrix to include line data.
The section of code below is an example:
%Open the image file
I = imread([matlabroot '\toolbox\images\imdemos\football.jpg']);
%Display the unmodified image.
imshow(I)
%Create a polygon around some portion of the image.
h = impoly(gca,[101.0000 165.0000; 212.0000 103.0000; 235.0000 137.0000; 119.0000 204.0000]);
%Create a logical mask matrix from the polygon
BW = h.createMask;
%Use the mask to create a perimeter
P = bwperim(BW,8);
%Separate the original image into Red Green and Blue matrices for
%manipulation.
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
% Modify the appropriate color matrices to create a polygon in whatever
% color you need. This one is red.
R(P) = 255;
G(P) = 0;
B(P) = 0;
%Assemble a new image.
Inew(:,:,1) = R;
Inew(:,:,2) = G;
Inew(:,:,3) = B;
%Display it
figure, imshow(Inew);
%Write it to file.
imwrite(Inew,'modFootball.jpg','jpeg');