IplImageは
OpenCVで画像を扱うときの構造体です。
以下は定義です
定義
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; /* ditto */
struct _IplTileInfo *tileInfo; /* ditto */
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;
これは定義なのですが、すべて利用するわけではありません。
使いたいものは
int width; /* image width in pixels */
int height; /* image height in pixels */
char *imageData; /* pointer to aligned image data */
これらだけです。
widthとheightはそのまま、幅と高さです。
imageDataには画像データのポインタが格納されています。
それでは詳しく説明していきます。
メモリに格納され方
| 型 |
バイト数 |
一つのポインタの中身 |
実際の入り方(1pixel) |
| char |
1 |
0xCC (Cは256階調の色データ) |
0xBB,0xGG,0xRR |
メモリには↑の様に格納されています。
よって、ポインタを一つずらすことによって、次の色のデータにアクセスします。
1ピクセルずれるわけではないので注意です。
サンプル
それぞれの画素にアクセスする方法を記述しておきます。
char r,g,b;
IplImage *img; //既にここには何か格納されてると仮定する
for(int y=0;y<img->height;y++){
for(int x=0;x<img->width;x+=3){
//ポインタが1ずれると1バイト分ずれる事を忘れない。
b = *(img->imageData + mg->width*y + x);
g = *(img->imageData + mg->width*y + x +1);
r = *(img->imageData + mg->width*y + x +2);
}
}
コメントどうぞ
最終更新:2010年05月24日 01:45