29217

Access violation writing location 0x00000000

Question:

Please have a look at the following code

<strong>ColorDetector.h</strong>

#pragma #include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> using namespace std; using namespace cv; class ColorDetector { public: ColorDetector(void); ~ColorDetector(void); //Set the Color Distance void SetColorDistanceThreshold(int); //Get the Color distance int GetColorDistanceThreshold(); //Detect Colours of the image Mat Process(Mat &image); //Set the Target color using 3 channels void SetTargetColor(unsigned char red, unsigned char green, unsigned char blue); //Set the Target color using Vec3b //This will hold the whole color value at once void SetTargetColor(Vec3b color); //Returns the target color Vec3b GetTargetColor(); //Gets the distancefrom the target color int GetDistance(const Vec3b &)const; private: int minDist; //Minimum acceptable distance Mat result; //The resulting image Vec3b target; //The target colour };

<strong>ColorDetector.cpp</strong>

#include "ColorDetector.h" ColorDetector::ColorDetector(void) { //Set the RGB Values target[0] = 0; target[1] = 0; target[2] = 0; } ColorDetector::~ColorDetector(void) { } void ColorDetector::SetColorDistanceThreshold(int distance) { if(distance>0) { minDist = distance; } else { minDist = 0; } } int ColorDetector::GetColorDistanceThreshold() { return minDist; } void ColorDetector::SetTargetColor(unsigned char red, unsigned char green, unsigned char blue) { //BGR Order target[0] = blue; target[1] = green; target[2] = red; } void ColorDetector::SetTargetColor(Vec3b color) { target = color; } Vec3b ColorDetector::GetTargetColor() { return target; } int ColorDetector::GetDistance(const Vec3b &color)const { int distance = abs(color[0]-target[0]) + abs(color[1]-target[1]) + abs(color[2]-target[2]); return distance; } Mat ColorDetector::Process(Mat &image) { result.create(result.rows,result.cols,CV_8U); //Loop Mat_<Vec3b>::const_iterator it = image.begin<Vec3b>(); Mat_<Vec3b>::const_iterator itend = image.end<Vec3b>(); Mat_<uchar>::iterator itout = result.begin<uchar>(); while( it != itend) { //Compute distance from target color if(GetDistance(*it)<minDist) { *itout = 255; } else { *itout = 0; } *++it; *++itout; } return result; }

<strong>ColorDetectorMain.cpp</strong>

#include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include "ColorDetector.h" using namespace std; int main() { ColorDetector cd; Mat image = imread("C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg"); try { if(!image.data) { throw 1; } } catch(int i) { cout << "Unable to read the image" << endl; } cd.SetColorDistanceThreshold(100); cd.SetTargetColor(130,190,230); namedWindow("Result"); imshow("Result",cd.Process(image)); waitKey(0); }

I get the following error when I run this code

First-chance exception at 0x013a16a5 in OpenCv.exe: 0xC0000005: Access violation writing location 0x00000000. Unhandled exception at 0x013a16a5 in OpenCv.exe: 0xC0000005: Access violation writing location 0x00000000. The program '[4768] OpenCv.exe: Native' has exited with code -1073741819 (0xc0000005).

The code breaks here *itout = 255; which in inside the while loop of ColorDetector.cpp.

What am I doing here wrong? Please help!

Answer1:

You get this error because result is smaller than image and at some point you reach result.end() before reaching image.end().

result.create(result.rows,result.cols,CV_8U);

Did you mean this?

result.create(image.rows,image.cols,CV_8U);

Answer2:

the problem is this code:

*++it; *++itout;

which should just be:

++it; ++itout;

<strong>EDIT</strong> - see comments, it appears that this is not correct usage, but probably didn't cause your problem. I'll leave the answer unless it gets downvotes because it <em>may</em> be helpful.

Recommend

  • how can I sort properly with preg_match?
  • find a point on a line closest to a third point javascript
  • Validating instances of inner-classes
  • Is it a oop good design?
  • Fix position mouse cursor
  • MySQL calculating distance (simple solution)
  • SQL Server geography latitude longitude error
  • Assigning the contents of a file to an variable
  • Find records within Geofence (circle) using MySQL Spatial fields
  • mysql SELECT best of each category in a single table
  • springboot + webpack dev server, does not change localhost bundle file after rebuilding
  • how to calculate distance between two points in google maps?
  • Thrust filter by key value
  • Load 24 bit TGA
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • why my app based on boost::asio didn't accept new connection
  • Opaque reference instead of PImpl. Is it possible?
  • Delphi: Where is the shortcut that started the application? [duplicate]
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • How do I mock an exported typescript function in a jasmine test?
  • Invalid Date on validation Date of js
  • Sonar maven jacoco code coverage for Multimodule project
  • C: Incompatible pointer type initializing
  • Regex thinks I'm nesting, but I'm not
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • output of program is not same as passed argument
  • Fill an image in a square container while keeping aspect ratio
  • Shallow update not allowed (git > 1.9)
  • Javascript + PHP Encryption with pidCrypt
  • jquery mobile loadPage not working
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to include full .NET prerequisite for Wix Burn installer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Qt: Run a script BEFORE make
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?