c++ - 访问冲突写入位置 0x00000000

标签 c++ visual-studio-2010 image opencv image-processing

这是我的代码:

ColorDetector.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

#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

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

运行这段代码时出现以下错误

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).

代码在 *itout = 255; 处中断,在 ColorDetector.cpp 的 while 循环中。

我在这里做错了什么?

最佳答案

你得到这个错误是因为 result 小于 image 并且在某些时候你在到达 之前到达 result.end() image.end().

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

你是这个意思吗?

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

关于c++ - 访问冲突写入位置 0x00000000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16150543/

相关文章:

c++ - C++ VS2017 中不允许使用 auto

c++ - Armadillo vector 的按位运算

c++ - 如何在 VS2010 中痛饮?

c++ - 如何在 OpenCV 中将图像与另一个图像相关联

c++ - 在您的 VS 解决方案中创建的 .libs 如何与另一个项目链接?

c++ - 时序图生成器

c# - 代码隐藏文件无法识别 aspx 控件

asp.net - 在 VB 中更改元素的 CSS 样式

html - CSS:通过不使用JS的包含图像定义元素的宽度

java - 调整服务器上图像的大小以适应各种设备