Convert RGB IplImage to 3 Arrays

I need help with a C ++ / pointer. When I create an RGB IplImage and I want to access i, j, I use the following C ++ class, taken from: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv- intro / opencv-intro.html

template<class T> class Image
{
private:
    IplImage* imgp;

public:
    Image(IplImage* img=0) {imgp=img;}
    ~Image(){imgp=0;}
    void operator=(IplImage* img) {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 Image<RgbPixelFloat>  RgbImageFloat;
typedef Image<unsigned char>  BwImage;
typedef Image<float>          BwImageFloat;

I work with CUDA, so sometimes I have to put all the data in an array, I like to save each channel in its own array, it seems easier to process the data in this way. So I would usually do something like this:

IplImage *image = cvLoadImage("whatever.tif");
RgbImageFloat img(image);
for(int i = 0; i < exrIn->height; i++)
{
    for(int j = 0; j < exrIn->width; j++)
    {
        hostr[j*data->height+i] = img[i][j].r;
        hostg[j*data->height+i] = img[i][j].g;
        hostb[j*data->height+i] = img[i][j].b;
    }
}

Then I copied my data to the device, worked with it, returned it to the host, and then started the loop, again, through the array, assigning the data back to IplImage and saving my results.

, , , , . , ? - , :

float *hostr = &img[0][0].r
float *hostg = &img[0][0].b
float *hostb = &img[0][0].g

? !

EDIT:   . , . , . IplImage , , csl . , , , , IplImage, - "rgbrgbrgbrgb".

+3
2

-, ++, OpenCV 2.0, (IplImage* CvMat*) (Mat) . .. MATLAB-esque, .

IplImage* Mat, :

 IplImage *image = cvLoadImage("lena.bmp");
 Mat Lena(image);
 vector<Mat> Channels;
 split(Lena,Channels);
 namedWindow("LR",CV_WINDOW_AUTOSIZE);
 imshow("LR",Channels[0]);
 waitKey();

vector Channels.

OpenCV2.0 , . OpenCV :

x(1,1,1) x(1,1,2) x(1,1,3) x(1,2,1) x(1,2,2) x(1,2,3) ...

x(i,j,k) = an element in row i of column j in channel k

, OpenCV .. widthStep, . , csl , ( widthStep) .

:

2.0 , IplImage* Mat Lena = imread("Lena.bmp");.

+5

. , , .

, . . I.e., . , , , .

-, y * . , .

, , memcpy(), . , float , , memcpy() .

, ( , ):

float *dst = &hostg[0][0];
RgbPixelFloat *src = &img[0][0];
RgbPixelFloat *end = &img[HEIGHT][WIDTH] + 1;

// copy green channel of whole image
while ( src != end )  {
    *dst = src->g;
    ++dst;
    ++src;
}
+1

Source: https://habr.com/ru/post/1720114/


All Articles