How to save a plotted pattern onto an image

Hi,
I am trying to save an image with a circle drawn on it. I tried with both imwrite and print, but then when I open the file again, the plotted circle is gone.
Image = imread('Eye_00001.jpg');
figure('visible', 'off')
imshow(Image)
% plot circle (CRx and CRy are center coordinates, CRr is radius)
hold on
th = 0:pi/50:2*pi;
xunit = CRr * cos(th) + CRx;
yunit = CRr * sin(th) + CRy;
plot(xunit, yunit, 'r');
% plot the circle center
scatter(CRx, CRy, 20, 'r+');
hold off
% save the image:
save_file_name = strcat(working_directory_name, 'Eye_res_00001.jpg');
imwrite(Image, res_filename)
close(figure)
The version with print changes in the lines:
save_file_name = strcat(working_directory_name, 'Eye_res_00001');
print(res_filename, '-djpeg')
Is there something I'm getting wrong?
Thanks everybody in advance

 Accepted Answer

Image = imread('Eye_00001.jpg');
figure('visible', 'off')
imshow(Image)
% plot circle (CRx and CRy are center coordinates, CRr is radius)
hold on
th = 0:pi/50:2*pi;
xunit = CRr * cos(th) + CRx;
yunit = CRr * sin(th) + CRy;
plot(xunit, yunit, 'r');
% plot the circle center
scatter(CRx, CRy, 20, 'r+');
hold off
F = getframe ;
% save the image:
save_file_name = strcat(working_directory_name, 'Eye_res_00001.jpg');
imwrite(F.cdata, res_filename)
close(figure)

8 Comments

Hi
I am sorry but I just tried the algorithm and nothing changed. Exactly the same problem as before
Attach your image and give the values of CRr, CRx and CRy. It will work actually. I suspect values related to circle.
In
imwrite(F.cdata, res_filename)
replace "res_filename" with "save_file_name".
hi, actually in my original code the two names are matching, so that is not the problem. I simplified the code I uploaded because it required some intermediate steps I though would be useless. I forgot to update that part
I = imread('Eye_00004.jpg');
Image = rgb2gray(I);
% Prompt user to select folder
dir_name = uigetdir(pwd,'Select working folder, must be the same for algorithm and Eye folder');
% Create paths to variables and folders
mkdir(fullfile(dir_name, 'Results'))
res_name = strcat(dir_name, '/Results/');
eye_file_name = sprintf('%s/Eye/Eye_', dir_name);
% select the file as 4th image of the Result folders
frame_index = 4;
% filter the image
f = fspecial('gaussian', [1 1], 1);
Image = imfilter(Image, f, 'symmetric');
% gets starting points for the recognitions (other functions)
sx = start_points(frame_index, 1); % 160
sy = start_points(frame_index, 2); % 390
% locate CR
[CRx, CRy, CRar] = locate_corneal_reflection_Beatrice(Image, sx, sy, cr_window_size);
% [CRx,CRy] = corneal reflection coordinate
% CRar = approximate radius of the corneal reflection
[CRx, CRy, CRar]
ans =
170.3393 376.5000 4.2220
% plot the image with the corneal reflex circle
imshow(Image)
hold on
th = 0:pi/50:2*pi;
xunit = CRr * cos(th) + CRx;
yunit = CRr * sin(th) + CRy;
plot(xunit, yunit, 'r');
scatter(CRx, CRy, 20, 'r+');
hold off
% for this example I show the image
F = getframe ;
% save the image:
res_filename = strcat(res_name, sprintf('Eye_res_%5.5d.jpg', frame_index));
imwrite(F.cdata, res_filename)
This is the original image
This is the image plotted with the circle (which doesn't get saved)
Hi,
I tried re-running the algorithm on the computer lab and now the getframe function works just fine. I do not know why in my PC still doesn't work. Probably I should re-install the program
Okay..hope you got your problem solved....
i am facing the same problem. i tried this code but still not able to save the image with circle over it. kindly help

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!