Is it possible to crop raw video before extracting all the frames using OpenCV and C++?
Is it possible to crop raw video before extracting all the frames using OpenCV and C++?
I want to crop a certain area of my raw video before extracting the frames. I don't want to use external cropping video software because I need the video to be raw for processing. Is it possible to crop a certain area before extraction ? I read about ROI for images, is it possible for video too ?
Here's my extraction code that's already working :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);
int main(int argc, char **argv)
string s;
VideoCapture cap("test_video.mov");
if (!cap.isOpened())
cout << "Cannot open the video file" << endl;
return -1;
double fps = cap.get(CV_CAP_PROP_FPS);
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_NORMAL);
resizeWindow("MyVideo", 600, 600);
while (1)
Mat frame;
Mat Gray_frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
cout << "Cannot read the frame from video file" << endl;
break;
s = int2str(c);
//cout<<("%dn",frame )<<endl;
c++;
imshow("MyVideo", frame);
imwrite("frame" + s + ".jpg", frame);
if (waitKey(30) == 27)
cout << "esc key is pressed by user" << endl;
break;
return 0;
string int2str(int &i)
string s;
stringstream ss(s);
ss << i;
return ss.str();
Still need to crop the video, any advice ?
The video has a long duration with high fps, there will be thousands extracted images and setting the ROI after the extraction isn't really practical for me, it's too slow. I want to know if there's a way to crop the video first.
– Hiro
11 hours ago
As far as I know there is no way to crop video without reading. You can do it with external program before doing this with OpenCV. If it is too slow are you sure that this is the bottelneck of your solution?
– wdudzik
7 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What's wrong with setting the ROI after extraction?
– hkchengrex
11 hours ago