Creating RGB Images

In color images, each pixel is characterized by several values (color channels). For example, RGB images have three color channels: red, green, and blue. The following fields in the IplImage structure specify color-related parameters of the image:

nChannels is the number of color channels in the image;
colorModel is a string that specifies the image's color model (for example, "RGB");
channelSeq is a string that specifies the sequence of channels in the image (for example, "BGR");

The following code example creates an RGB image. (Actually, the example application that you can launch on this page creates three images.)

To run the example from the command line, type this command:
>create -t4 -b200
(-b specifies the image brightness).

The source:

void create_rgb_one( int channel, bool domodal=true, int bright=255 ) {
   const int side = 256;
   IplROI roi = { channel, 0,0, side, side };
   IplImage* img = iplCreateImageHeader(
      3,                     // number of channels
      0,                     // alpha channel
      IPL_DEPTH_8U,          // data of byte type
      "RGB", "BGR",          // color model and channel seq
      IPL_DATA_ORDER_PIXEL,  // channel arrangement
      IPL_ORIGIN_BL,         // bottom left orientation
      IPL_ALIGN_DWORD,       // 4 bytes align
      side, side,            // image width and height
      &roi,                  // ROI
      NULL, NULL, NULL       // no mask ROI, image ID, not tiled
   );
   // allocate image data with filling
   iplAllocateImage( img, 1, 64 );

   if( NULL != img->imageData ) {
      /// loop to increase brightness
      for( int v=64; v<=bright; v+=2 ) {
         /// set square of channel selected
         iplSet( img, v );
         /// decrease area of roi for the next step
         ipInflateRoi( &roi, -1 );
      }
      /// protect local variable
      img->roi = NULL;
      /// display the image
      ipView( img, " RGB", domodal );
   }
   iplDeallocate( img, IPL_IMAGE_ALL );
}

* Legal Information © 1998-2000, Intel Corporation