Debugging 无效的空指针异常

Debugging 无效的空指针异常,debugging,Debugging,我在这段代码中遇到一个错误: #include <stdio.h> #include <cv.h> #include <highgui.h> using namespace std; int main( int argc, char** argv ) { //load color img specified by first argument //IplImage *img = cvLoadImage( argv[1]); IplImage

我在这段代码中遇到一个错误:

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

using namespace std;

 int main( int argc, char** argv )

 {
 //load color img specified by first argument
 //IplImage *img = cvLoadImage( argv[1]);
  IplImage *img = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR );


 IplImage *red = cvCreateImage(cvSize(img->width, img->height ),img->depth,img->nChannels);

 IplImage *green = cvCreateImage(cvSize(img->width, img->height ),img-  >depth,img>nChannels);

 IplImage *blue = cvCreateImage(cvSize(img->width, img->height ),img->depth,img->nChannels);



// setup the pointer to access img data 
uchar *pImg   = ( uchar* )img->imageData;

// setup pointer to write data 
uchar *pRed   = ( uchar* )red->imageData;
uchar *pGreen = ( uchar* )green->imageData;
uchar *pBlue  = ( uchar* )blue->imageData;    



int i, j, rED, gREEN, bLUE, byte;
  for( i = 0 ; i < img->height ; i++ ) 
{

  for( j = 0 ; j < img->width ; j++ ) 
{
 rED = pImg[i*img->widthStep + j*img->nChannels + 2];

 gREEN = pImg[i*img->widthStep + j*img->nChannels + 1];
 bLUE = pImg[i*img->widthStep + j*img->nChannels + 0];
 // RED

 pRed[i*img->widthStep + j*img->nChannels + 2] = rED;
 // GREEN
 pGreen[i*img->widthStep + j*img->nChannels + 1] = gREEN;

 // BLUE
 pBlue[i*img->widthStep + j*img->nChannels + 0] = bLUE;
 }
}
// save images

cvSaveImage( argv[2], red );
cvSaveImage( argv[3], green );
cvSaveImage( argv[4], blue );

return 0;
}

黄色箭头指向::CrtDbgBreak()

可能不是该代码段中的唯一问题,但这一行有一个输入错误:

IplImage*green=cvCreateImage(cvSize(img->width,img->height),img->depth,img>nChannels)

您正在通过
img>nChannels
,而不是
img->nChannels

#ifdef _DEBUG
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message,
const wchar_t *file, unsigned int line)

   {    // report error and die
    if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
    {
        ::_CrtDbgBreak();
    }
}