Cropping imagesc with a handler and rect
Mostrar comentarios más antiguos
Hello!
I'm currently working on a project which requires me to crop detected objects out of frames of images. X,Y coordinates are already obtained automatically.
The frame can only be displayed using imagesc instead of imshow.
I would like to crop but imcrop requires the frame to be saved as an image first. However, when I save the imagesc as an image file and reopen it for cropping, the X and Y coordinates of the objects to be cropped have changed.
%% Display frame with detected objects, only 2 images detected is sufficient
z=1 %frame number
bin_image{z}
handler = imagesc(bin_image{z}.frame);
hold on;
x1=bin_image{z}.x(1); %coordinates of object detected
x2=bin_image{z}.x(2);
x3=bin_image{z}.x(3);
x4=bin_image{z}.x(4);
y1=bin_image{z}.y(1);
y2=bin_image{z}.y(2);
y3=bin_image{z}.y(3);
y4=bin_image{z}.y(4);
rectangle('Position',[x1 y1 x2-x1 y2-y1],'EdgeColor','r');
rectangle('Position',[x3 y3 x4-x3 y4-y3],'EdgeColor','r');
saveas(handler,sprintf('Savedframe%d.png',z));
hold off;

Imagesc coordinates:
x: [205 216 223 234 0 0 0 0 0 0 0 0 0 0 0 0]
y: [91 108 91 108 0 0 0 0 0 0 0 0 0 0 0 0]
num_obj: 2
By manually crop and trial and error, i realised that the corrdinates of x and y of the two objects have changed when I want to crop out the two objects. How do I avoid the change in coordinates, because I would like to automate this process if given a few hundred frames to crop out from? Thank you!

%% Crop Rectangles
I = imread('SavedFrame1.png');
J = imcrop(I,[590 318 201 69]);
handler2 = imshow(J);
saveas(handler2,sprintf('SavedCrop1.png'));
2 comentarios
Image Analyst
el 23 de Feb. de 2019
"imcrop requires the frame to be saved as an image first" well, don't you HAVE images? If not, what do you have? Isn't bin_image{z}.frame an image? If not, exactly what is it?
WILSON XU WEIXUAN
el 23 de Feb. de 2019
Respuestas (1)
Image Analyst
el 23 de Feb. de 2019
0 votos
You should be saving the images bin_image{z}.frame and "J" directly with imwrite() instead of saving the figure windows with saveas().
Categorías
Más información sobre Display and Exploration en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

