You are not logged in.

#1 2009-04-14 14:22:15

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

C++ Templates on OpenCV IPLImages, some advise please

Hi all, I'm working on an engineering PhD involving computer vision.

My code-base involves lots of image manipulation, I simplify the routine stuff by using OpenCV's IPLImages, instead of my own image classes.

Thing is, many of my classes need to be flexible in the data-type being used. IPLImages can be of a variety of types, using templating as well, but the downshoot of that is that I'm not sure how to use templates for my own classes, and have resorted to function templates instead.

Here's what I'm currently using.

class foo {
private:
   template<class T>
    void bar2(IplImage* Image);
public:
    void bar(IplImage* Image);
};

template<class T>
foo::bar2(IplImage* Image){
    //actual code goes here, using type T
}

foo::bar(IplImage* Image) {
    //check the type using Image->depth, run the appropriate template
}

I would like, conceptually, to be able to be able to call foo::bar2 directly, WITHOUT having to specify what data-type to use. I know this is possible if one of the arguments is of the type being used, it'll automagically use the right template type, but since I'm using IPLImages instead of data arrays, that doesn't help my use-case, which means that every single one of my classes requires the re-directing foo::bar.

So, I hope I've been reasonably clear. Anyone has any suggestions?


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#2 2009-04-14 20:02:49

kakTuZ
Member
From: Hannover, Germany
Registered: 2007-10-20
Posts: 86

Re: C++ Templates on OpenCV IPLImages, some advise please

Hi,
if the type of the image is only accessible at runtime, templates wont help you much here.
But perhaps I have not understand the problem. How is the "type" of the image denoted? I have searched a bit, but only found that IplImage is a struct that
can hold image data and a lot of information about how to interprete these data.

To call a template function or use a template class, it is required to know the template arguments at compile time and i don't see, how this applies here.
My Best bet would be to create a small class hierarchie with the types you need and use the runtime polymorphism.

Offline

#3 2009-04-15 01:16:26

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: C++ Templates on OpenCV IPLImages, some advise please

The 'depth' variable within the IPLImage class has an integer value denoting the data type (I think its 8 for uint8, 16 for uint16, or something along those lines, there are #define s for that).

Yes, your second paragraph about the requirement for knowing the arguments at compile time answers my question, I think. Because I specify depth at run-time, its one of the parameters of my library. The current way I'm doing, it compiles for all possible types.

Runtime polymorphism is a bit above me for now, I'll go read up on it. I think it may increase rather than decrease my code complexity though, so may not use it. Thanks kakTuZ for your comments and help.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#4 2009-04-15 10:57:13

kakTuZ
Member
From: Hannover, Germany
Registered: 2007-10-20
Posts: 86

Re: C++ Templates on OpenCV IPLImages, some advise please

It is actually quite easy (See the wikipedia example: http://en.wikipedia.org/wiki/Polymorphi … ng#C.2B.2B).
In some cases it might be simple enough to have switch statements all over the code to delegate you calls depending on the image type, but when this needs to be done often or you have a lot of different types to handle, then it might be better to have a decent class hierarchy.

But there might be an alternative where you can combine templates with a decent class hierarchie.
I have found the types you mentioned (IPL_DEPTH_8U,IPL_DEPTH_8S,IPL_DEPTH_16U,IPL_DEPTH_16S,IPL_DEPTH_32S,IPL_DEPTH_32F,IPL_DEPTH_64F)

class MyImageBase { virtual ~MyImage(); virtual doSomething(); };

template<class Type>
class MyImage : extends MyImageBase {
    doSomething() {/*stuff done here depending on Type*/}
};

MyImageBase * makeImage(IplImage * image) {
    switch(image->depth) {
    case IPL_DEPTH_8U: return new MyImage<unsigned char>(image);
    case IPL_DEPTH_8s: return new MyImage<signed char>(image);
    ....
    case IPL_DEPTH_64F: return new MyImage<double>(image);
    }
    throw runtime_error("image type not supported");
}

This way you can use templates to only have one class to implement where the logic of handling the image goes. And all the different Image types have a common base wich will help you at runtime.

Offline

#5 2009-04-15 13:19:04

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: C++ Templates on OpenCV IPLImages, some advise please

Thanks kakTuZ for your continuining help,

That's actually almost exactly what I'm doing currently (without the virtual, though, pseudo-code in my OP), and it works. I was just looking for a solution that wouldn't involve me having to explicitly define the allowed image depths allowed. Which would mean that as I expand my algorithm to different data types (including my own defined ones) I wouldn't have to go back to each and every (currently about 10, and I expect another 20 or so) class that I've written using this technique.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

Board footer

Powered by FluxBB