OpenCV c++ API VideoCapture not working with video files - c++

I'm currently using OpenCV(4.5.1-dev) c++ api to try to read frames from a mp4 .video file. I keep getting error "can not open video file". I compile open cv with
-DWITH_FFMPEG=OFF
as suggested in OpenCV readme.txt. The minimum reproducible code is
cv::VideoCapture cap;
cap.open(video_path, cv::CAP_V4L);
if(!cap.isOpened())
{
std::cerr << "trying to open video file at: " << video_path << std::endl;
CV_Error(cv::Error::StsError, "Can not open Video file");
}
for(int frameNum = 0; frameNum < cap.get(cv::CAP_PROP_FRAME_COUNT); frameNum++)
{
cv::Mat frame;
cap >> frame; // get the next frame from video
frames.push_back(frame);
}
where for video_path, I tried both absolute and relative path. Neither works. Any suggestions?

Related

I can't open 2 gige(basler) cameras at the same time with opencv

I'm using two GigE (basler aca2500-14gm) cameras on win10 opencv3.4, I connect the wires of the 2 cameras to the switch, and then connect it to my computer.but I can't open the camera and get the frames at the same time。
my code:
`
int main()
{
PylonInitialize();
VideoCapture cap(0);
VideoCapture cap1(2);
if (!cap.isOpened())
{
cout << "Camera 1 unsuccessfully opened" << endl;
}
if (!cap1.isOpened())
{
cout << "Camera 2 unsuccessfully opened" << endl;
}
bool stop = false;
while (!stop)
{
Mat frame;
Mat frame1;
cap >> frame;
cap1 >> frame1;
if (frame.empty() || frame1.empty())
{
break;
}
imshow("Open the camera 1", frame);
imshow("Open the camera 2", frame1);
if (waitKey(100) >= 0)
{
PylonTerminate();//
stop = true;
}
}
}`
by the way,when I tried to run the sample of basler SDK :Grab_MultipleCameras.cpp,
I can open the camera but the image in the window is grey.
is there anyone help me to solve this problem?
Thanks in advance.
When you run this sample of basler SDK it might happen that the second camera still could not be open but it just show you the window with defualt color (grey).
Another likely thing is that your are passing wrong device ID for VideoCapture to work, See this
OpenCv VideoCap documentation. Also from what I know if you are using GigE cameras it will be better to pass ip address of each camera to VideoCapture
So I would say only to try change one think in your code:
From
VideoCapture cap(0);
VideoCapture cap1(2);
To:
VideoCapture cap(/*camera Ip Address*/); //or try with different IDs
VideoCapture cap1(/*camera Ip Address*/);
Also take a look at this answer VideoCapture and GigE camera . It is stated there that when there is more than one camera you would be better with passing IP address.
Another thing to check will be if you can see both cameras in your device manager.
EDIT:
Hey I found nice documentation about working with Pylon SDK( from camera vendor) andOpenCV (it is probably older version of OpenCV but still can be useful)

Write to dummy video stream using OpenCV

I'm using OpenCV and v4l2loopback library to emulate video devices:
modprobe v4l2loopback devices=2
Then I check what devices I have:
root#blah:~$ v4l2-ctl --list-devices
Dummy video device (0x0000) (platform:v4l2loopback-000):
/dev/video1
Dummy video device (0x0001) (platform:v4l2loopback-001):
/dev/video2
XI100DUSB-SDI (usb-0000:00:14.0-9):
/dev/video0
video0 is my actual camera where I grab frames from, then I plan to process them via OpenCV and write it to video2 (which is a sink I believe).
Here is how I attempt to do so:
int width = 320;
int height = 240;
Mat frame(height, width, CVX_8UC3, Scalar(0, 0, 255));
cvtColor(frame, frame, CVX_BGR2YUV);
int fourcc = CVX_FOURCC('Y', 'U', 'Y', '2');
cout << "Trying to open video for write: " << FLAGS_out_video << endl;
VideoWriter outputVideo = VideoWriter(
FLAGS_out_video, fourcc, 30, frame.size());
if (!outputVideo.isOpened()) {
cerr << "Could not open the output video for write: " << FLAGS_out_video
<< endl;
}
As far as I know video output format should be YUYV (which is equal to YUY2 in OpenCV). Please correct me if I'm wrong. In my code I'm not writing into outputVideo anything yet, just trying to open it for write, but I keep getting outputVideo.isOpened()==false for some reason, no additional errors/info in the output:
root#blah:~$ main --uid='' --in_video='0' --out_video='/dev/video2'
Trying to open video for write: /dev/video2
Could not open the output video for write: /dev/video2
I'd appreciate any advice or help on how to debug/resolve this issue. Thank you in advance!

Mac OS X open cv write a video file. Error

I'm new to opencv and its development. I'm taking camera feed and that feed i convert into avi format's video file. When i try to open that file using VLC player. Nothing is shown to me. please find below the code i used. Any help is appreciated.( File is writing into file but i think its problem with file formats )
int main(int argc, char** argv){
VideoCapture vcap(0);
if(!vcap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
// VideoWriter video("/Users/venushka/Desktop/pre/ou.avi",vcap.get(CV_CAP_PROP_FOURCC),vcap.get(CV_CAP_PROP_FPS),
// cv::Size(vcap.get(CV_CAP_PROP_FRAME_WIDTH), vcap.get(CV_CAP_PROP_FRAME_HEIGHT)));
const int fps = 30.0;
vcap.set(CV_CAP_PROP_FPS, fps);
VideoWriter video("/Users/venushka/Desktop/pre/ou.avi", vcap.get(CV_CAP_PROP_FOURCC), fps,
cv::Size(vcap.get(CV_CAP_PROP_FRAME_WIDTH), vcap.get(CV_CAP_PROP_FRAME_HEIGHT)));
for(;;){
Mat frame;
vcap >> frame;
video.write(frame);
imshow( "Frame", frame );
char c = (char)waitKey(33);
if( c == 27 ) break;
}
return 0;
}
CV_CAP_PROP_FPS only works on videos as far as I know, from camera it will return 0.
Set your FPS manually as follows:
const int fps = 30.0;
vcap.set(CV_CAP_PROP_FPS, fps);
VideoWriter video("<path to avi>", vcap.get(CV_CAP_PROP_FOURCC), fps,
cv::Size(vcap.get(CV_CAP_PROP_FRAME_WIDTH), vcap.get(CV_CAP_PROP_FRAME_HEIGHT)));

"bad src image pointer" after read frame of video in OpenCV

VideoCapture cap;
cap.open("Path_to_directory\\%03d.jpg");
for (int i = 0; i < number_of_frames; ++i)
{
Mat frame;
cap >> frame;
//...
}
In the line "cap >> frame;" "bad src image pointer" is printed to console. None of the following frames of video could not be retrieved.
Is there a specific property of image should have to captured with VideoCapture then retrieving to a Mat?
Probably there is, because frames could be retrieved with code above. However after I manipulated them with Gimp, then I saved with overwriting them. Their some property should be changed while overwriting, but I don't know what are those.
Thanks.
Note: OpenCV version 2.4.9

Opening images one after the other

I have a series of images saved on my system according to their time stamps.
For example the images are named as:
20140305180348.jpg
20140305180349.jpg
I have 100 such images, I want to open them using OpenCV one after the other. I have tried using cvCapturefromFile() but using it I am able to open just a single image at a time. I want to stitch/join them so that I can make a video.
I am sorry I cannot post the code as I am not allowed to. How do I proceed?
In OpenCV, to write images to a video, you can use VideoWriter (and do it in a loop to read a sequence of images):
VideoWriter outputVideo; // Open the output
// ... set video properties like FPS
if (!outputVideo.isOpened())
{
cout << "Could not open the output video for write: " << source << endl;
return -1;
}
for(...)
{
// read your frame, e.g. to Mat img
// outputVideo.write(img); //save or
outputVideo << img;
}
cout << "Finished writing" << endl;
Check out here for more info.
In this tutorial is an example how to write a video. Just modify the for-loop in the end.
pseudocode:
open videocontainer
int i=0;
while(i<100){
Mat img = imread("path"+to_string(i)+".jpg");
outputvideo << img;
}
close videocontainer