2013-04-22 99 views
1

請看看下面的代碼訪問衝突寫入位置00000000

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;這在while循環ColorDetector.cpp內。

我在這裏做什麼錯了?請幫忙!

+0

也許'result.create(result.rows,result.cols,CV_8U);'不應該使用自己實例化? – 2013-04-22 15:17:45

回答

5

由於result小於image,並且在達到image.end()之前達到result.end(),所以得到此錯誤。

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

您的意思是?

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

太好了。謝謝 :) – 2013-04-23 06:03:42

3

的問題是這樣的代碼:

*++it; 
    *++itout; 

這應該僅僅是:

++it; 
    ++itout; 

編輯 - 看評論,看來,這是不正確的使用,但可能沒有導致你的問題。我會離開答案,除非它得到downvotes,因爲它可能是有幫助的。

+1

儘管編寫'++ it'肯定更有意義,但我懷疑它會導致nullpointer異常。 – 2013-04-22 15:14:54

+0

即使解引用確實沒有用,它也不會改變迭代器的增量方式,也不會解決OP的問題。 – syam 2013-04-22 15:15:15