How can Gopro cam be connected using VideoCapture of opencv? - c++

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

Related

Cannot read video from file while can read from camera

My problem is that I cannot read from file I tried with .mp4 and .mov using cpp code on windows with library opencv3.4.0. I tried to read from camera it is working.
What could be the reason ?
cv::VideoCapture cap("001.mp4");
// Check if camera opened successfully
if (!cap.isOpened()) {
std::cout << "Error opening video stream or file" << std::endl;
break; //==>hits here
}
/// while below code part works correctly
cv::VideoCapture cap(0);
// Check if camera opened successfully
if (!cap.isOpened()) {
std::cout << "Error opening video stream or file" << std::endl;
break;
}
Copy and try below code, if it doesnt work either. It is completely about your opencv installation or directory mistake.
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(){
VideoCapture cap("videoname.mp4");
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
cap >> frame;
if (frame.empty())
break;
imshow( "Frame", frame );
char c=(char)waitKey(25);
if(c==27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}

opencv 3.2.0 can not acces camera 0

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

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

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

opencv VideoCapture is blocked in thread

I need some some help with the usage of opencv VideoCapture in another thread.
When I use the VideoCapture in the main thread, it is perfectly fine and it shows the video smoothly. But once I put the code in another thread and expect it do the same thing, it seems the VideoCapture does not work at all.
I made some attempts: if I initialize the VideoCapture with 0 (the default one) as parameter, it gets blocked. But if I don't initialize it
VideoCapture cap;
or use another number
VideoCapture cap(1);
it prints out error message and exits but does not get blocked.
Here is the code:
#include <iostream>
#include <thread>
#include <functional>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
class MyClass {
public:
// display the video
static void display(int i) {
VideoCapture cap(0);
if (!cap.isOpened()) {
cout << "cannot access webcame" << endl;
exit(-1);
}
Mat imgOriginal;
namedWindow("Original", WINDOW_AUTOSIZE);
while (true) {
bool success = cap.read(imgOriginal);
if (!success) {
cout << "fail to read video into mat" << endl;
break;
}
imshow("Original", imgOriginal);
if (waitKey(30) == 27) {
break;
}
}
}
};
int main()
{
//cout << "Hello World!" << endl;
thread myThread(bind(MyClass::display, 0));
myThread.join();
return 0;
}
Very appreciated if anyone can point out where I got it wrong. Thank you.