C++ access violation exception in the function findContours()
I use visual studio 2017, and in opencv 3.3. I try to run the program below and get an error that I do not know what the reason might be.
The problem is in the function findContours().
The code is taken fromReal-Time Object Tracking Without Colour
Exception thrown at 0x00007FFE57B53FB8 (KernelBase.dll) in opencvTry.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <stdlib.h> #include <stdio.h> #include <iostream> #include <string>
using namespace std;
using namespace cv;
void searchOfMoov(Mat , Mat &);
string intToString(int);
std::string i=" ";
int theObject[2] = { 0,0 };
VideoCapture capture;
Mat frame1, frame2;
Mat grayimage1, grayimage2;
Mat diffrentimage;
Mat thresholdimage;
Rect objectBoundingRectangle = Rect(0, 0, 0, 0);
string intToString(int number) {
this function has a number input and string output
std::stringstream ss;
ss << number;
return ss.str();
}
void searchOfMoov(Mat thresholdImage, Mat &cameraFeed) {
bool objectDetected = false;
Mat temp;
thresholdImage.copyTo(temp);
//these two vectors needed for output of findContours
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
//find contours of filtered image using openCV findContours function
findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );// retrieves all contours
findContours(temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);// retrieves external contours
//if contours vector is not empty, we have found some objects
if (contours.size()>0)objectDetected = true;
else objectDetected = false;
if (objectDetected) {
the largest contour is found at the end of the contours vector
we will simply assume that the biggest contour is the object we are looking for.
vector< vector<Point> > largestContourVec;
largestContourVec.push_back(contours.at(contours.size() - 1));
make a bounding rectangle around the largest contour then find its centroid
this will be the object's final estimated position.
objectBoundingRectangle = boundingRect(largestContourVec.at(0));
int xpos = objectBoundingRectangle.x + objectBoundingRectangle.width / 2;
int ypos = objectBoundingRectangle.y + objectBoundingRectangle.height / 2;
update the objects positions by changing the 'theObject' array values
theObject[0] = xpos, theObject[1] = ypos;
}
make some temp x and y variables so we dont have to type out so much
int x = theObject[0];
int y = theObject[1];
draw some crosshairs on the object
circle(cameraFeed, Point(x, y), 20, Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x, y - 25), Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x, y + 25), Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x - 25, y), Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x + 25, y), Scalar(0, 255, 0), 2);
putText(cameraFeed, "Tracking object at (" + intToString(x) + "," + intToString(y) + ")", Point(x, y), 1, 1, Scalar(255, 0, 0), 2);
}
int main(){
some boolean variables for added functionality
bool objectDetected = false;
these two can be toggled by pressing 'd' or 't'
bool debugMode = false;
bool trackingEnabled = false;
pause and resume code
bool pause = false;
while (1) {
capture.open("17-12-20_074100.avi");
while (capture.get(CV_CAP_PROP_POS_FRAMES) < capture.get(CV_CAP_PROP_FRAME_COUNT) - 1) { //if video not end
capture.read(frame1);
cvtColor(frame1, grayimage1, COLOR_BGR2GRAY);
capture.read(frame2);
cvtColor(frame2, grayimage2, COLOR_BGR2GRAY ...
"I use visual studio 2017" -- did you build the opencv libs locally ? (the prebuild libs are for vs2015 ONLY
(it's not your code, your vs project is broken)
This is code I took at the link I attached. So the reason is because of Visual Studio version?
"So the reason is because of Visual Studio version?" -- yes. either use vs2015, or build opencv libs from src, using cmake.
then make sure to ONLY use debug libs with a debug project, and release libs with RELEASE.
Thank you for your reference