17

I just started learning OpenCV with "Learning OpenCV Computer Vision with the OpenCV Library".

In the first example which demonstrates how to display a picture, it has a line

IplImage* img = cvLoadImage("name.type")

Although the book explains it, I still do not fully know what exactly IplImage* img does.

Does cvLoadImage loads the image to img which IplImage is pointing to? Can anyone explain this to me please? Thank you

FDinoff
  • 30,689
  • 5
  • 75
  • 96
n0bod1
  • 255
  • 1
  • 2
  • 13
  • 4
    If you're just starting to learn OpenCV, I **strongly** recommend using the C++ API. The C API (including `IplImage`) is deprecated (not to mention harder to use). You might want to start at OpenCV's [tutorials](http://docs.opencv.org/doc/tutorials/tutorials.html) instead. – Aurelius Jun 25 '13 at 22:39

1 Answers1

25
  • img is the name of the variable, might as well be blahblahblah;

  • IplImage is the type of the variable, it's just a struct that contains the image data itself plus some info (size, color depth, etc.) on the image;

    typedef struct _IplImage {
    int                  nSize;
    int                  ID;
    int                  nChannels;
    int                  alphaChannel;
    int                  depth;
    char                 colorModel[4];
    char                 channelSeq[4];
    int                  dataOrder;
    int                  origin;
    int                  align;
    int                  width;
    int                  height;
    struct _IplROI*      roi;
    struct _IplImage*    maskROI;
    void*                imageId;
    struct _IplTileInfo* tileInfo;
    int                  imageSize;
    char*                imageData;
    int                  widthStep;
    int                  BorderMode[4];
    int                  BorderConst[4];
    char*                imageDataOrigin;
    } IplImage;
    

For more info on IplImage: Other question about IplImage

  • cvLoadImage provides a pointer to an IplImage, which means it creates an IplImage when it loads it and returns you it's emplacement.

Do not forget to do cvReleaseImage(&img) when you are finished with it, if you do not want to have memory leaks.

Community
  • 1
  • 1
Smash
  • 3,722
  • 4
  • 34
  • 54
  • So IplImage is a data structure that stores informationon the variable "img"? If you don't mind, I would like to ask some more questions while you are here... Why is a pointer needed for IplImage? What does it mean when it says "A pointer to an allocated image data structure is then returned"? Thank you – n0bod1 Jun 25 '13 at 21:16
  • There is an image data AND a pointer to that is created with `IplImage* img = cvLoadImage("name.type")` call. The reason you declare it as such is that all cv... methods accept and return image pointers. The book you are learning from is based on old openCV API, which is written on C. Now openCV 2.4.5 latest stable release uses C++ API, where there is no need of pointers. Along with the book take a look at opencv 2.4.5 documentation, and learn easily with great tutorials there. In C++ API `Mat image = imload("");` declaration is without pointer, and methods require data, not its pointer. – baci Jun 25 '13 at 22:17