opencv 3.2.0 can not acces camera 0 - c++

Compiled opencv 3.2.0 to mingw - codeblocks in windows 10 creators
i can write programs in opencv, they run, e.g. Lena.cpp.
but when i need the camera, the program run, making the window but it doesn't show the camera(0) of the laptop.
Here's the code:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
VideoCapture stream1(0); //0 is the id of video device.0 if you have only one camera.
if (!stream1.isOpened()) { //check if video device has been initialised
cout << "cannot open camera";
}
//unconditional loop
while (true) {
Mat cameraFrame;
stream1.read(cameraFrame);
imshow("cam", cameraFrame);
if (waitKey(30) >= 0)
break;
}
return 0;
}

Related

How can Gopro cam be connected using VideoCapture of opencv?

Visual Studio and opencv are using versions 2019 and 4.4.0
GoPRO HERO 7 BLACK is connected to PC using USB-C.
I try to open the cam using VideoCapture(), but it is not opened.
Below is the code are using.
#include <opencv2\opencv.hpp>
#include <opencv2\highgui.hpp>
#include <opencv2\core.hpp>
#include <opencv2\imgproc.h>>
using namespace cv;
using namespace std;
int main(void) {
VideoCpature webcam_01(0);
if(!webcam_01.isOpened()) {
cout << "Cam opened error" << endl;
return -1; }
while(1) {
Mat frame;
webcam_01.read(frame);
if(frame.empty()) {
cout << "frame empty" << endl;
break;
}
imshow("img", frame);
}
return 0;
}

OpenCV - in OSX is failing to capture the camera

Why in OSX my code is not showing any camera capture at all?
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
VideoCapture stream1(0); //0 or 1 or 2 same..
if (!stream1.isOpened()) {
cout << "cannot open camera";
}
while (true) {
Mat cameraFrame;
stream1.read(cameraFrame);
imshow("cam", cameraFrame);
if (waitKey(30) >= 0)
break;
}
return 0;
}
[Solved] Problem was solved by replacing the value
VideoCapture stream1(2);

How to set camera fps in opencv?

I'm using a webcam supporting 1280 x 720 # 60 fps.
My computer environment is intel i5-4690K and Windows7, Visual studio 2015, opencv 3.1
When I run the webcam in Kinovea(0.85.15, https://www.kinovea.org/), the camera run at the 1280 x 720 # 60fps.
But, In Visual studio with Opencv, it isn't work # 60 fps.
It just work only 12~15 fps.
My code for checking the camera fps is below.
#include <stack>
#include <iostream>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/video.hpp>
#include "opencv2/imgcodecs.hpp"
#include <time.h>
using namespace cv;
using namespace std;
int keyboard;
int main(int argc, char** argv)
{
VideoCapture cap(0); //capture the video from web cam
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
while ((char)keyboard != 'q' && (char)keyboard != 27)
{
Mat imgOriginal;
Mat ROOI;
clock_t a = clock();
bool bSuccess = cap.read(imgOriginal);
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
printf("Captue Time : %f\n", double(clock() - a) / double(CLOCKS_PER_SEC));
imshow("Original", imgOriginal);
if (waitKey(1) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
In above code. I check the "Capture Time" and it was usually records 0.07s ~ 0.09s.
So, I attempt to VideoCapture::set(CV_CAP_PROP_FPS, 60), but it isn't work.
(When I get the FPS using the code VideoCapture::get(CV_CAP_PROP_FPS), it return value 0.)
How can I control the webcam FPS?
Thanks.
When I modify my code like below, it works # 60 fps.
#include <stack>
#include <iostream>
#include <math.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/video.hpp>
#include "opencv2/imgcodecs.hpp"
#include <time.h>
using namespace cv;
using namespace std;
int keyboard;
int main(int argc, char** argv)
{
VideoCapture cap(0); //capture the video from web cam
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
while ((char)keyboard != 'q' && (char)keyboard != 27)
{
Mat imgOriginal;
Mat ROOI;
clock_t a = clock();
bool bSuccess = cap.read(imgOriginal);
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
printf("Captue Time : %f\n", double(clock() - a) / double(CLOCKS_PER_SEC));
imshow("Original", imgOriginal);
if (waitKey(1) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
The key for camera working # 60 fps is
cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M', 'J', 'P', 'G'));
My camera works #60 fps in MJPG mode. So I add above code, it works fine!
You could try setting the camera's frame rate outside of OpenCV, e.g. on Linux you can control UVC cameras (Logitech, etc.) using libwebcam, and uvcdynctrl in particular.

Failed to output video file by OpenCV on MacOS with C++

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(){
VideoCapture vcap("0");
if(!vcap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
int frame_width= vcap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height= vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("~/out.avi", CV_FOURCC('J','P','E','G'), 10, Size(frame_width,frame_height),true);
for(;;){
Mat frame;
vcap >> frame;
video << frame;
imshow( "Frame", frame );
char c = (char)waitKey(33);
if( c == 'q' ) break;
}
return 0;
}
I have tried the above code, the program can correctly show the camera image through imshow(), but after terminate the program, no video output file has written to the disk. I use MacOS Xcode and OpenCV 2.4.13 as development platform. The same work and output the video file completely on Windows Visual Studio. Please help.

cvCaptureFromFile- Opening Video from specific path - Raspberry Pi

I have OpenCV-2.4.9 installed in Raspberry Pi. Right now I am trying to load a video from specific path and for that I tried with both C and C++ API
C API: cvCaptureFromFile(path);
C++ API: VideoCapture cap; cap.open(path)
I am getting error and it says could not open file.
It works well in Windows and Linux, but not in Raspberry Pi. Am I missing something?
C++ code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(){
VideoCapture cap("C:/Users/nava/Videos/file.mp4");
if (!cap.isOpened()){
cout << "Error opening video stream" << endl;
return -1;
}
while (1){
Mat Frame;
if (!cap.read(Frame)){
cout << "No Frame available" << endl;
waitKey();
}
imshow("output", Frame);
if (waitKey(33) == 27) break;
}
}
C Code:
#include "highgui.h"
int main(int argc, char** argv)
{
cvNamedWindow("video",CV_WINDOW_AUTOSIZE);
CvCapture* capture = cvCreateFileCapture("/home/pi/Desktop/test.mp4");
IplImage* frame;
while(1)
{
frame = cvQueryFrame(capture);
if(!frame) break;
cvShowImage("video", frame);
char c = cvWaitKey(33);
if(c == 27) break;
}
}
You have to install UV4L driver.Follow this tutorial :
http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=14