2

I read on http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html that a possible way to access the image data with c++ is:

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;

So I'm able to use something like:

IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
RgbImage  imgA(img);
imgA[i][j].b = 111;
imgA[i][j].g = 111;
imgA[i][j].r = 111;

When I use:

imgA[i][j].b

My question is: how cpp knows the channels of the image? I mean, how c++ populates the

img[i][j].b as blue channel
img[i][j].g as green channel and 
img[i][j].r as red channel?

Does it have a default constructor to the structs?!

1 Answer 1

3

You should have a look at how templates (Template metaprogramming) are working.

You have a constructor but the rgb channels are populated in the

 inline T* operator[](const int rowIndx)
 {
    return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));
 }

when you instantiate the Template with a RgbPixel structure your function will became:

 inline RgbPixel* operator[](const int rowIndx)
 {
    return ((RgbPixel *)(imgp->imageData + rowIndx*imgp->widthStep));
 }

Because the data in both (the image and the RgbPixel structure) is stored in a continuous block of memory you will get the correct informations.

Sign up to request clarification or add additional context in comments.

3 Comments

@CharlesB:I haven't finished editing the question. And my comment is very helpful: you could not explain something to somebody who dose not know what are you talking about.
Downvote removed after edit; in this case you can add a disclaimer like "currently adding some details" to your post so that it doesn't get downvoted
to moderator: my flag on this post is outdated now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.