Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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);
}

}

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

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