[OpenCV] RAW rgb data to IplImage | 以rgb原始資料建立IplImage指標

Sometime we may only got rgb data from source, so we have to build valid IplImage pointer. This sample shows that how to change rgb data to IplImage.

有時我們只會收到rgb資料,所以要自己建立有效的IplImage,以下是將rgb資料轉成IplImage。




VARIABLE DEFINITION
變量定義
rgbtexture    raw rgb data get from the camera | 由視訊傳來的rgb原始資料
cpimg          IplImage to store the RAW rgb data | 用來存rgb原始資料的IplImage變量
arr                simple opencv data format | 基本的opencv的資料格式


According  to IplImage definition | 根據IplImage的定義

typedef struct _IplImage

{
    int  nSize;             /* sizeof(IplImage) */
    int  ID;                /* version (=0)*/
    int  nChannels;    /* Most of OpenCV functions support 1,2,3 or 4 channels */
    int  alphaChannel;      /* Ignored by OpenCV */
    int  depth;             /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S,                       IPL_DEPTH_16S,IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */

    char colorModel[4];     /* Ignored by OpenCV */
    char channelSeq[4];     /* ditto */
    int  dataOrder;         /* 0 - interleaved color channels, 1 - separate color channels. cvCreateImage can only create interleaved images */

    int  origin;            /* 0 - top-left origin,  1 - bottom-left origin (Windows bitmaps style). */
    int  align;             /* Alignment of image rows (4 or 8). OpenCV ignores it and uses widthStep instead.   */

    int  width;             /* Image width in pixels.*/
    int  height;            /* Image height in pixels.*/
    struct _IplROI *roi;    /* Image ROI. If NULL, the whole image is selected.*/
    struct _IplImage *maskROI;      /* Must be NULL. */
    void  *imageId;                 /* "           " */
    struct _IplTileInfo *tileInfo;  /* "           " */
    int  imageSize;         /* Image data size in bytes
                               (==image->height*image->widthStep
                               in case of interleaved data)*/

    char *imageData;        /* Pointer to aligned image data.         */
    int  widthStep;         /* Size of aligned image row in bytes.    */
    int  BorderMode[4];     /* Ignored by OpenCV.                     */
    int  BorderConst[4];    /* Ditto.                                 */
    char *imageDataOrigin;  /* Pointer to very origin of image data
              (not necessarily aligned) -needed for correct deallocation */
}
    IplImage;



If we want to create a IplImage, basically we need to create a image header by using CvCreateImage(), then set ImageData(pointer to save the rgb) as the rgb data. At this time,
the IplImage is almost done.

如果要建立IplImage指標,首先要用CvCreateImage()建立header,然後將rgb data放到imageData。


    IplImage *cpimg;

    unsigned char *rgbtexture;
    CvArr *arr;
   
    arr = rgbtexture;
   
    cpimg = cvCreateImage(cvSize(img->width, img->height),
        IPL_DEPTH_8U, 3);
    cpimg->imageSize = sizeof(arr);
    cpimg->imageData = (char *)arr;

    cvCvtColor(cpimg, cpimg, CV_RGB2BGR);
    cvResize(cpimg, img, CV_INTER_LINEAR);


    cvReleaseImage(&cpimg);





Because variable arr is a pointer. The data is saved in somewhere, so we better make a copy to ensure the image data is not changed or deleted by other. Up to here, we can use cvShowImage() to see the result. If the color of the image look strange, you can use cvCvtColor() to rearrange R,G,B in the array.
Keep in mind, the color pattern of opencv is BGR, not RGB.

因為arr是指標,所以最好複製一次以保護原始資料。如果cvShowImage()顯示錯誤的顏色,可以使用cvCvtColor()去改變R,G,B的排序。
OpenCV使用BGR而不是RGB的排序。

留言

此網誌的熱門文章

Monitoring System Spec.& IO Mapping

突如奇來的高科技電子零件