OpenCV - in OSX is failing to capture the camera - c++

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

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

Xcode7.3.1-Why the image can not be found and ended with exit code:255

#include <iostream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace std;
using namespace cv;
int main()
{
char filename[] = "IMG_0851";
Mat image; //create an image matrix
I want to analysis the image in grayscale
image = cvLoadImage(filename,CV_LOAD_IMAGE_GRAYSCALE); //load image in grayscale directly
if (image.empty())
{
cout << "Image could not be found" << endl;
return -1;
}
imshow("Image in RGB", image);
cvWaitKey(0); //wait for a keystroke in the window
return 0;
}
Build succeeded, however, the image cannot be found & program ended exit code: 255.

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

Unhandled exception at Canny edge detector

I want to try Canny edge detector, but when I try to start I receive an Unhandled exception:
Unhandled exception at 0x00007FF97F6C8B9C in canny_project.exe: Microsoft C++ exception: cv::Exception at memory location 0x0000002485D89860
Below is the code that I implemented In VS2012.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main(int, char**)
{
namedWindow("Edges", CV_WINDOW_NORMAL);
CvCapture* capture = cvCaptureFromCAM(-1);
cv::Mat frame; cv::Mat out; cv::Mat out2;
while (1) {
frame = cvQueryFrame(capture);
GaussianBlur(frame, out, Size(5, 5), 0, 0);
cvtColor(out, out2, CV_BGR2GRAY); // produces out2, a one-channel image (CV_8UC1)
Canny(out2, out2, 100, 200, 3); // the result goes to out2 again,but since it is still one channel it is fine
if (!frame.data) break;
imshow("Edges", out2);
char c = cvWaitKey(33);
if (c == 'c') break;
}
return 0;
}
Thanks in advance
The problem is probably you are using cvCaptureFromCAM wrong.
cvCaptureFromCAM(0) // not -1
Why do you use OpenCV with C-Code? Use VideoCapture instead CvCapture.
Please try this instead and tell me whether images are shown or not and try different device numbers too:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main(int, char**)
{
cv::namedWindow("Capture");
int deviceNum = 0; // please try different device numbers too like -1, 1, 2, ...
cv:VideoCapture capture(deviceNum);
cv::Mat frame;
if(!capture.isOpened())
{
std::cout << "Could not open device " << deviceNum << std::endl;
return 0;
}
while (true)
{
capture >> frame; // = cvQueryFrame(capture);
//if (!frame.data) break;
if(frame.empty())
{
std::cout << "could not capture a legal frame" << std::endl;
continue;
//break;
}
cv::imshow("Capture", frame);
char c = cv::waitKey(33);
if (c == 'c') break;
}
std::cout << "press any key to exit" << std::endl;
cv::waitKey(0); // wait until key pressed
return 0;
}
cvCaptureFromCAM(-1) has wrong argument, use 0, if you have just one camera connected. In addition, in C API, when you finished working with video, release CvCapture structure with cvReleaseCapture(), or use Ptr<CvCapture> that calls cvReleaseCapture() automatically in the destructor. Have a try, please, this example, to see if you access your camera properly.