Access Violation Exception Thrown OpenCV - c++

Im working on a simple face detector using Visual Studio 2017 with OpenCV 3.2.0 and haarcascades.
Whenever i run this, i get an error saying there's an exception thrown right after i create the first point. Access violation error. Any help would be greatly appreciated as I do not see any error in the code.
#include <opencv2/opencv.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
CascadeClassifier face_cascade;
CascadeClassifier eye_cascade;
face_cascade.load("haarcascade_frontalface_alt.xml");
eye_cascade.load("haarcascade_eye_tree_eyeglasses.xml");
VideoCapture cap(0);
Mat frame;
while (cap.read(frame))
{
Mat frame_gray;
std::vector <Rect> faces;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray,faces, 1.1, 2,0,Size(30,30),Size(300,300));
for (size_t i = 0; i < faces.size();i++)
{
Point first(faces[i].x, faces[i].y);
Point second(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
rectangle(frame,second,first, cvScalar(255, 0, 0), 1, 8, 0);
}
imshow("Test", frame_gray);
waitKey(30);
}
return 0;
}

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?

After adding SurfFeatureDetector, program giving error

I am using opencv 3.3.0
my code is
#include <stdio.h>
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include <fstream>
#include <string>
#include <vector>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
int main(int argc, char** argv)
{
Mat img1;
img1 = imread("img/img1.jpg", IMREAD_GRAYSCALE);
if (img1.empty())
{
cout << "Could not open or find the image" << endl;
return -1;
}
Ptr<SurfFeatureDetector> detector = SurfFeatureDetector::create(400);
vector<KeyPoint> keypoints;
detector->detect(img1, keypoints);
imshow("img1", img1);
waitKey(0);
return 0;
}
I am new to C++ and opencv. The code work well without surf part.
error
OpenCV Error: Assertion failed (TlsSetValue(tlsKey, pData) == TRUE) in cv::TlsAbstraction::SetData, file C:\Users\Darshana\Documents\opencv\opencv-3.3.0\modules\core\src\system.cpp, line 1270
I have tried with Ptr<SURF> detector = SURF::create(400); as well.
UPDATE
I couldn't figure out a solution for this. Maybe a setup issue or library issue. So I just move to opencv 2.4.13 and now everything works fine. I had to change the above code to work with v2.4.13 though.
You haven't set parameter for SurfFeatureDetector. I think you should try this instead:
int minHessian = 400;
SurfFeatureDetector detector(minHessian);
vector<KeyPoint> keypoints;
detector.detect(img1, keypoints);

Opencv Saving Video

I am using cascade for detection object and I want to save my video after detection But I can save just a frame (I can save just the last frame)
And my code is here:
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <opencv2/objdetect/objdetect.hpp>
using namespace std;
using namespace cv;
int main()
{
CascadeClassifier nesne;
nesne.load("cascade.xml");
string filename = "a.avi";
VideoCapture capture(filename);
Mat frame;
Mat grires;
namedWindow("algilanan", 1);
while(true)
{
capture>>frame;
cvtColor(frame, grires, CV_BGR2GRAY); //resmi gri renk uzayına çevirir.
vector<Rect> nesvek;
nesne.detectMultiScale(grires, nesvek, 1.1, 3, 0, Size(30,30));
for(int i = 0; i < nesvek.size(); i++)
{
Point pt1(nesvek[i].x + nesvek[i].width, nesvek[i].y + nesvek[i].height);
Point pt2(nesvek[i].x, nesvek[i].y);
rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
}
int frame_width= capture.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height= capture.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("out.avi",CV_FOURCC('M','J','P','G'),10, Size(frame_width,frame_height),true);
video.write(frame);
imshow("algilanan", frame);
waitKey(33);
}
return 0;
}
The reason you can save a single frame is because you are re-initializing the VideoWriter on every iteration of the loop. You should initialize it outside the loop. Try this.
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <opencv2/objdetect/objdetect.hpp>
using namespace std;
using namespace cv;
int main()
{
CascadeClassifier nesne;
nesne.load("cascade.xml");
string filename = "a.avi";
VideoCapture capture(filename);
Mat frame;
Mat grires;
namedWindow("algilanan", 1);
int frame_width= capture.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height= capture.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("out.avi",CV_FOURCC('M','J','P','G'),10,Size(frame_width,frame_height),true);
while(true)
{
capture>>frame;
cvtColor(frame, grires, CV_BGR2GRAY); //resmi gri renk uzayına çevirir.
vector<Rect> nesvek;
nesne.detectMultiScale(grires, nesvek, 1.1, 3, 0, Size(30,30));
for(int i = 0; i < nesvek.size(); i++)
{
Point pt1(nesvek[i].x + nesvek[i].width, nesvek[i].y + nesvek[i].height);
Point pt2(nesvek[i].x, nesvek[i].y);
rectangle(frame, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);
}
video.write(frame);
imshow("algilanan", frame);
waitKey(33);
}
return 0;
}

How can I use fastNlMeansDenoisingColored() in C++?

I am new in learning OpenCV. While I was searching for good algorithm to remove noise I found this Function but I have no idea how to use it, so is this function available in OpenCV libraries? If yes, how can I use it.
void fastNlMeansDenoisingColored(InputArray src, OutputArray dst, float h=3, float hColor=3, int templateWindowSize=7, int searchWindowSize=21 )
This is from the opencv documentation. Therefore try using that function.
A snippet of code I found online may be able to help you:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main() {
Mat img = imread("a.jpg");
if (!img.data) {
cout << "No image found" << endl;
return -1;
}
// first copy the image
Mat img_gray = img.clone();
cvtColor(img, img_gray, CV_RGB2GRAY);
Mat img1;
cv::fastNlMeansDenoising(img_gray, img1, 3.0, 7, 21);
imshow("img1", img1);
waitKey();
return 0;
}
Hope it helps!

"strange" error using eclipse and opencv while following along in API, "undefined reference to symbol (long-string-of-jargon) in file.o"

not sure how to proceed... still wet behind the ears, looking for advice.
//"strange" error using eclipse and opencv while following along in API
./src/captureCV.o: undefined reference to symbol '_ZN2cv12GaussianBlurERKNS_11_InputArrayERKNS_12_OutputArrayENS_5Size_IiEEddi'
and here's the cpp:
/*
* captureCV.cpp
*/
#include "opencv2/opencv.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <string.h>
#include <stdio.h>
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
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
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
return 0;
}
not much to see just sample API material taken from openCV
build exits after executable is made.