简体   繁体   English

访问冲突写入位置 0x00000000

[英]Access violation writing location 0x00000000

Here is my code:这是我的代码:

ColorDetector.h颜色检测器.h

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

ColorDetector.cpp颜色检测器.cpp

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

ColorDetectorMain.cpp颜色检测器Main.cpp

#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;代码在这里中断*itout = 255; which in inside the while loop of ColorDetector.cpp.在 ColorDetector.cpp 的while循环中。

What am I doing here wrong?我在这里做错了什么?

You get this error because result is smaller than image and at some point you reach result.end() before reaching image.end() .因为你得到这个错误result小于image ,并在某些时候你达到result.end()到达之前image.end()

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

Did you mean this?你是这个意思吗?

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

the problem is this code:问题是这个代码:

    *++it;
    *++itout;

which should just be:这应该是:

    ++it;
    ++itout;

EDIT - 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 may be helpful.除非它被否决,否则我会留下答案,因为它可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM