MATLAB Answers

drawing on an image in loop

32 views (last 30 days)
Philip
Philip on 2 Jul 2012
How can I draw on an from loop iterations, without 'imread' and 'imwrite'?
EDIT: Inside the loop, I am doing some processing to determine the pixel coordinates of certain features. Once I have these coordinates, I am using them to draw a square on the image at these locations. I would like the loop to draw the first square on the first iteration, and also draw the second square on the second iteration and so on... until I have 100 squares. Is there a way to update the image like this without having to load and save the image each time?
At the moment, I am using:
for i=1:100
img = imread('img.png');
% !! DO SOME PROCESSING HERE !!
imwrite(output,'img.png');
end
However, the use of 'imread' and 'imwrite' takes a long time because the images are large.
Can the loop be altered to create an updated image, and then use 'imwrite' after the loop to save the result?

  0 Comments

Sign in to comment.

Answers (2)

Sean de Wolski
Sean de Wolski on 2 Jul 2012
Edited: Sean de Wolski on 2 Jul 2012
You are literally overwriting the same file each time? If so, then you absolutely don't need to save it on each iteration.
img = imread('img.png');
for ii = 1:100;
img = do_stuff_with_image(img); %stuff will be done 100x
end
imwrite(img,'img.png');

  4 Comments

Show 1 older comment
Image Analyst
Image Analyst on 2 Jul 2012
A little, but not totally. You can use "hold on" and then plot() or rectangle() to draw the box on the image. But what I don't understand is that you're reading in an image and processing it and then finally drawing the box on it, but right away you're on to the next image so the box is gone and you won't see it. What good is that? Do you want to see all 100 images and all 100 boxes on screeen simultaneously or just one at a time? By the way, see the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Philip
Philip on 2 Jul 2012
Thanks for your help. I am overlaying the squares on top of an already existing image. At the end of the loop I want to be able to see the image with all 100 squares overlayed.
By using 'imread' and 'imwrite' inside the loop, I can achieve the following:
1st iteration:
Read image (containing no squares yet)
Do processing to overlay square on image at specific location
imwrite to save the result (image with one square)
2nd iteration:
Read image (now contains 1 square, overlayed)
Do processing to draw a different square at another location
imwrite to save the result (image with two squares)
and so on...
But this is very slow, and I was wondering if there was a more efficient way
Ryan
Ryan on 2 Jul 2012
If you are using imwrite() to save the image file with a square marked on it then I'm assuming you have a matrix with the square on it already, so why not just redifine the 'starting image' at the end of the loop. For example:
I = imread('image.png');
for m = 1:100
% Draw your rectangles and do your processing on I
I = ProcessedImage;
end
imwrite(I,'image.png')

Sign in to comment.


Image Analyst
Image Analyst on 2 Jul 2012
Edited: Image Analyst on 2 Jul 2012
Philip: Why do you need to read and write it every iteration? Just read it once, enter the loop and "burn" the box into the image by writing into the image array directly (i.e. don't use plot() or rectangle() because those just place boxes in the overlay), then call cla and imshow(). Finally when the loop exits, call imwrite. This will be much faster and accomplish the same thing.

  0 Comments

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by