Ask Your Question

How to save a cropped object from all the frames in a folder?

asked 2018-10-10 03:25:14 -0500

meera_c gravatar image

updated 2018-10-10 03:26:03 -0500

I have a function to crop a particular object(defined by its x and y corsinates and roi width and height). I want to do it in a video, that is, crop this particular object from all frames of the video and save it in a folder. I am not able to save all of it, my code overwrites the image. Can someone please tell me what I can change here?

int cropObject(double Xvalue, double Yvalue, cv::Mat frame)
if (frame.empty())
    std::cout << "!!!  failed to open target image" << std::endl;
    return -1;

int offset_x = Xvalue;
int offset_y = Yvalue;

cv::Rect roi;
roi.x = offset_x;
roi.y = offset_y;
roi.width = 50;//frame.size().width - (offset_x*2);
roi.height = 50;//frame.size().height - (offset_y*2); 

/* Crop the original image to the defined ROI */

int filenumber = 0;

cv::Mat crop = frame(roi);
if (!crop.empty())
    cv::imwrite("cropped_object.png", crop);


And this function is called each time for a new frame

edit retag flag offensive close merge delete



make sure, your filename is unique, e.g. by adding a (random ?) number to it.

berak gravatar imageberak ( 2018-10-10 04:59:23 -0500 )edit

Thanks, got it resolved by adding unique ID as u suggested

meera_c gravatar imagemeera_c ( 2018-10-10 05:25:06 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-10 04:56:55 -0500

holger gravatar image

updated 2018-10-10 05:00:33 -0500

Well i am trying to be polite most of times - but i had to laugh a bit XD LOL

Well just make sure the file does not exists or make sure you have an unique file name - maybe use the frame number or a random generated value (UUID or something like this). Your question is not really related to opencv ;-)

Anyway, have a nice day.

edit flag offensive delete link more

Question Tools


Asked: 2018-10-10 03:25:14 -0500

Seen: 96 times

Last updated: Oct 10 '18