template<class T> class Image
{ private: IlpImage *imgp; public: Image(IplImage* img = 0) { imgp = img; } inline T* operator[](const int rowIndx) { return( (T*)(imgp ->imageData + rowIndx * imgp ->widthStep)); } }; typedef struct { unsigned char b, g, r; }RgbPixel; typedef struct { float b, g, r; }RgbPixelFloat; typedef Image<RgbPixel> RgbImage; typedef Imgae<RgbPixelFloat> RgbImageFloat; typedef Image<unsigned char> BwImage; typedef Image<float> BwImageFloat;例:对3通道的字节型图像访问如下:
IplImage* img = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 3); RgbImage imgA(img); //假设访问第K通道、第i行、第j列的像素 imgA[i][j].b = 111; imgA[i][j].g = 111; imgA[i][j].r = 111;