Cropping imagesc with a handler and rect

2 vues (au cours des 30 derniers jours)
WILSON XU WEIXUAN
WILSON XU WEIXUAN le 23 Fév 2019
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 commentaires
Image Analyst
Image Analyst le 23 Fév 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
WILSON XU WEIXUAN le 23 Fév 2019
Oh sorry I just realised it is an image. My bad.
I have added the cropping code inside, but SavedCrop1 instead produced the same image as the Savedframe1.
Sorry but Am I still missing out on something?
SavedCrop1:SavedCrop1.png
Savedframe1:
Savedframe1.png
%% 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));
J = imcrop(bin_image{z}.frame,[x1 x2 x2-x1 y2-y1]);
handler2 = imshow(J);
saveas(handler2,sprintf('SavedCrop1.png'));
hold off;

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 23 Fév 2019
You should be saving the images bin_image{z}.frame and "J" directly with imwrite() instead of saving the figure windows with saveas().

Community Treasure Hunt

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

Start Hunting!

Translated by