c++ accesse violation while trying to open camera when debugging - c++

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include "opencv2/opencv.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
#include <Windows.h>
#pragma comment(lib, "vfw32.lib")
#pragma comment( lib, "comctl32.lib" )
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat edges;
VideoCapture cap;
try
{
cap.open(0); // open the default camera
}
catch (...)
{
cout << "error!!" << endl;
}
if (!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("edges", 1);
for (;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
edges = frame;
GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
imshow("edges", edges);
if (waitKey(30) >= 0) break;
}
return 0;
}
when i try to run this code in the command line it runs perfectly, but when i try to debug it in visual studio express 2013 it crashing at the line "cap.open(0);"
the errror is :
Unhandled exception at 0x0C028EFB in opencv_1.exe: 0xC0000005: Access violation executing location 0x0C028EFB.
i dont know what the problem is or how to fix it.
sorry for bad english

Related

OPENCV face detection Exception thrown: read access violation. this->cc.**_Ptr** was nullptr. error

I am using c++ opencv and want to detect faces therefore i wrote this code:
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include<iostream>
#using namespace cv;
#using namespace std;
void main() {
string path = "C:\\Users\\Gökhan\\Desktop\\Resources\\ test.png";
Mat img = imread(path);
CascadeClassifier faceCascade;
faceCascade.load("C:\\Users\\Gökhan\\Desktop\\Resources\\
haarcascade_frontalface_default.xml");
if (faceCascade.empty()) { cout << "XML file not loaded" << endl; }
vector<Rect> faces;
faceCascade.detectMultiScale(img, faces, 1.1, 20); //this lines gives the error
for (int i = 0; i < faces.size(); i++)
{
rectangle(img, faces[i].tl(), faces[i].br(), Scalar(255, 0, 255), 3);
}
imshow("Image", img);
waitKey(0);
}
However, it is saying that Unhandled exception at 0x00007FFFD09F8E6C in Project2.exe: Microsoft C++ exception: cv::Exception at memory location 0x0000003537D9F430. and when I click continue it says Exception thrown: read access violation.
this->cc._Ptr was nullptr.
I have not solve this problem. What should I do?

VideoFrames are only coming when esc,spacebar or enter keys are pressed OpenCV c++

I am kind of an intermediate in Computer Vison and fairly proficient in opencv python however coming to c++ i am facing problems in just selecting ROI from Video feed and displaying the cropped feed .My code looks like this.
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/tracking.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main() {
Mat frame1;
VideoCapture cap;
cap.open(0);
cap.read(frame1);
Rect2d roi = selectROI(frame1, true);
Mat Crop = frame1(roi);
while (1) {
cap.read(frame1);
Crop = frame1(roi);
if (Crop.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
imshow("roi", Crop);
int key=waitkey(0);
}
}
The code is compiling ,and the cropped window is seen however I am always in need to click enter,spacebar or esc to get the video feed.Weird?
So the correct version of the corrected code will look somewhat like this.Thanks for the help.
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/tracking.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main() {
Mat frame1;
VideoCapture cap;
cap.open(0);
cap.read(frame1);
Rect2d roi = selectROI(frame1, true);
Mat Crop = frame1(roi);
while (1) {
cap.read(frame1);
Crop = frame1(roi);
if (Crop.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
imshow("roi", Crop);
*int key=waitkey(1)*;
}
}

opencv program crashes only when debugger is attached

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include "opencv2/opencv.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <direct.h>
#pragma comment(lib, "vfw32.lib")
#pragma comment( lib, "comctl32.lib" )
using namespace cv;
using namespace std;
void PrintFullPath(char * partialPath)
{
char full[_MAX_PATH];
if (_fullpath(full, partialPath, _MAX_PATH) != NULL)
printf("Full path is: %s\n", full);
else
printf("Invalid path\n");
}
int main(int argc, char** argv)
{
PrintFullPath(".\\");
Mat edges;
VideoCapture cap;
try
{
cap.open(0); // open the default camera
}
catch (...)
{
cout << "error!!" << endl;
}
if (!cap.isOpened()) // check if we succeeded
return -1;
//Mat edges;
namedWindow("edges", 1);
for (;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
edges = frame;
GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
//Canny(edges, edges, 0, 30, 3);
//Sleep(2000);
imshow("edges", edges);
if (waitKey(30) >= 0) break;
}
return 0;
}
the program crashes ONLY when the debugger is attached, when i press f5 it crashes in line "cap.open(0)", but when I press cntrol f5 it runs perfectly.
the error i get is:
Unhandled exception at 0x09068EFB in opencv_1.exe: 0xC0000005: Access violation executing location 0x09068EFB.
im using visual studio 2013, windows 8.0, opencv 3.0.
if i run the exe file in the command line the prorgam runs perfectly.

Playing a video file in opencv c++

I'm trying to play a video file using the following code.
When run it only shows a black screen with the window name (Video), can anyone help me fix it.
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
using namespace cv;
int main( int argc, char** argv )
{
CvCapture* capture = cvCreateFileCapture( "1.avi" );
Mat frame= cvQueryFrame(capture);
imshow("Video", frame);
waitKey();
cvReleaseCapture(&capture);
}
Try this if you only want to play a video ::::::::::::::::::::::::::
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv2\core\core.hpp>
#include "opencv2/opencv.hpp"
int main(int argc, char** argv)
{
cvNamedWindow("Example3", CV_WINDOW_AUTOSIZE);
//CvCapture* capture = cvCreateFileCapture("20051210-w50s.flv");
CvCapture* capture = cvCreateFileCapture("1.wmv");
/* if(!capture)
{
std::cout <<"Video Not Opened\n";
return -1;
}*/
IplImage* frame = NULL;
while(1) {
frame = cvQueryFrame(capture);
//std::cout << "Inside loop\n";
if (!frame)
break;
cvShowImage("Example3", frame);
char c = cvWaitKey(33);
if (c == 27) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("Example3");
std::cout << "Hello!";
return 0;
}
Actually the code you posted won't even compile.
Just have a look at OpenCV documentation: Reading and Writing images and video
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
//Video Capture cap(path_to_video); // open the video file
if(!cap.isOpened()) // check if we succeeded
return -1;
namedWindow("Video",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("Video", frame);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}

OpenCV 2.4.3 camera capture not working on Ubuntu 12.04

I am trying to execute the following program using openCV 2.4.3 on Ubuntu 12.04 LTS.
But I get "camera not initialized as the output" Can anybody help me.
here is the code:
include <iostream>
include "opencv2/imgproc/imgproc.hpp"
include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(1);
if (!cap.isOpened())
{
cout <<"Failed to initialize camera\n";
return 1;
}
namedWindow("CameraCapture");
Mat frame;
while (1)
{
cap>> frame;
imshow("cameraCapture",frame);
if (waitKey(30)>0)break;
}
destroyAllWindows();
return 0;
}
Please help me!
Thanks,
Kushal
try the following...
#include "iostream"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
CvCapture *webcam = cvCaptureFromCAM(-1);
IplImage *img = NULL;
while(true)
{
img = cvQueryFrame(webcam);
cvShowImage("TEST",img);
cvWaitKey(20);
}
return 0;
}
did u check the default capture device? by default it is 0
VideoCapture cap(0);