Search code examples
c++opencvshowiplimage

OpenCv fill pixel of onechannel image and show it


I wanna fill a black and white image and show it.

I can't understand what's the metter with this code:

IplImage * imageOut;
int window = 100;

cvNamedWindow("mappa", CV_WINDOW_AUTOSIZE );
imageOut = cvCreateImage(cvSize(window,window),8,1);

    for(int i=0; i<window; i++){  
        for(int j=0; j<window;j++){

            ((unsigned char*)(imageOut->imageData + i*imageOut->widthStep))[j]= j;
        }
    }

    cvShowImage("mappa", imageOut );

Solution

  • The correct procedure would be:

    imageOut = cvCreateImage(cvSize(window,window),IPL_DEPTH_8U,1);
    int width = imageOut->width; 
    int height = imageOut->height;
    int bpp = imageOut->nChannels; 
    for (int i=0; i < width*height*bpp; i+=bpp) 
    {    
       imageOut->imageData[i] = i & 0xff;  // some casting might be needed here
    }