Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stuck with design & template

Reply
Thread Tools

Stuck with design & template

 
 
mast4as
Guest
Posts: n/a
 
      09-10-2009
Hi everyone

I am trying to design some code but can't seem to find the best way of
doing this. Basically I am trying to create an image class. The class
has 4 member variables, the number of channels (how many planes define
the image, for example if it's a B&W image m_numChannels = 1 & if it's
an RGB image then m_numChannels = 3). There's also its width, height
and finally a pointer to the data (stored as float). So the class
looks like this:

class Image
{
public:
uint m_numChannels;
uint m_width;
uint m_height;
float *m_data;
Image(const int &w, const int &h, const int nchannels) :
m_width(w), m_height(h), m_numChannels(nchannels)
{
m_data = new float [nchannels * w * h];
}
~Image()
{
if (m_data) delete [] m_data;
}
/*
Vector<> Interpolate(const float &u, const float &v)
{
}
*/
};

Now what I would like to do is to create functions within the class
that process each pixel of the image. If the image was an RGB image
and always had 3 channels I could use a class Color defined as a set
of 3 floats and have each process function could return a Color.

for (i = 0; i < image->m_height; ++i)
for (j = 0; j < image->m_width; ++j)
Color pixel = image->Process(i, j);

The problem is that an image can have an arbitrary number of channels
so we don't to limit us to returning color. Ideally I would like to
return a Vector who size is the same as the number of channels in the
image. Something like that:
template<uint size>
class Vector
{
public:
float m_data[size];
...
}

for (i = 0; i < image->m_height; ++i)
for (j = 0; j < image->m_width; ++j)
Vector<3> pixel = image->Process(i, j);

That seems like a good idea the problem is that I don't know how to
write the Process method in my image class such as it knows how to
return a Vector which size equal to the number of channels in the
image ? I wonder if I have to turn the image class in a template ?

Would anyone have some experience with a similar problem and could
please give me some advices ?

Thanks a lot -

-coralie
 
Reply With Quote
 
 
 
 
mzdude
Guest
Posts: n/a
 
      09-10-2009
On Sep 10, 1:05*pm, mast4as <(E-Mail Removed)> wrote:
> Hi everyone
>
> I am trying to design some code but can't seem to find the best way of
> doing this. Basically I am trying to create an image class. The class
> has 4 member variables, the number of channels (how many planes define
> the image, for example if it's a B&W image m_numChannels = 1 & if it's
> an RGB image then m_numChannels = 3). There's also its width, height
> and finally a pointer to the data (stored as float). So the class
> looks like this:
>
> class Image
> {
> public:
> * uint m_numChannels;
> * uint m_width;
> * uint m_height;
> * float *m_data;
> * Image(const int &w, const int &h, const int nchannels) :
> * * m_width(w), m_height(h), m_numChannels(nchannels)
> * {
> * * m_data = new float [nchannels * w * h];
> * }
> * ~Image()
> * {
> * * if (m_data) delete [] m_data;
> * }
> * /*
> * Vector<> Interpolate(const float &u, const float &v)
> * {
> * }
> * */
>
> };
>
> Now what I would like to do is to create functions within the class
> that process each pixel of the image. If the image was an RGB image
> and always had 3 channels I could use a class Color defined as a set
> of 3 floats and have each process function could return a Color.
>
> for (i = 0; i < image->m_height; ++i)
> * for (j = 0; j < image->m_width; ++j)
> * * Color pixel = image->Process(i, j);
>
> The problem is that an image can have an arbitrary number of channels
> so we don't to limit us to returning color. Ideally I would like to
> return a Vector *who size is the same as the number of channels in the
> image. Something like that:
> template<uint size>
> class Vector
> {
> public:
> * float m_data[size];
> * ...
>
> }
>
> for (i = 0; i < image->m_height; ++i)
> * for (j = 0; j < image->m_width; ++j)
> * * Vector<3> pixel = image->Process(i, j);
>
> That seems like a good idea the problem is that I don't know how to
> write the Process method in my image class such as it knows how to
> return a Vector which size equal to the number of channels in the
> image ? I wonder if I have to turn the image class in a template ?
>
> Would anyone have some experience with a similar problem and could
> please give me some advices ?
>


typedef std::vector<float> ImageWidth; // pixels wide
typedef std::vector<ImageWidth> ImageHeight; // pixel high
typedef std::vector<ImageHeight> ImageDepth; // pixel depth, 1 = b/
w 3 = rgb

ImageDepth GetRgb()
{
ImageDepth id(3,ImageHeight(40,ImageWidth(56)));
return id;
}

ImageDepth GetBw()
{
ImageDepth id(1,ImageHeight(20,ImageWidth(100)));
return id;
}

int main()
{
ImageDepth id = GetRgb();
float f = id[0][0][0]; // get red at 0,0
f = id[1][0][0]; // get green at 0,0

id = GetBw();

return 0;
}

Having done image processing in my youth, I would
question using float as the storage type, but
it's your code.

I would also drop the raw pointers in favor of
vector.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-11-2009
mast4as wrote:
> Hi everyone
>
> I am trying to design some code but can't seem to find the best way of
> doing this. Basically I am trying to create an image class. The class
> has 4 member variables, the number of channels (how many planes define
> the image, for example if it's a B&W image m_numChannels = 1 & if it's
> an RGB image then m_numChannels = 3). There's also its width, height
> and finally a pointer to the data (stored as float). So the class
> looks like this:
>
> class Image
> {
> public:
> uint m_numChannels;
> uint m_width;
> uint m_height;
> float *m_data;
> Image(const int &w, const int &h, const int nchannels) :
> m_width(w), m_height(h), m_numChannels(nchannels)


Why are you using const int& for these parameters? What's wrong with int?

You are also mixing signed and unsigned types.

--
Ian Collins
 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      09-11-2009
On 10 syys, 20:05, mast4as <(E-Mail Removed)> wrote:
> Would anyone have some experience with a similar problem and could
> please give me some advices ?


I would make a class with one layer of image data, then
make the image from a list of layers. I would also make
a pixel class for adjusting pixels.
 
Reply With Quote
 
Fabio Fracassi
Guest
Posts: n/a
 
      09-11-2009
On 10 Sep., 19:05, mast4as <(E-Mail Removed)> wrote:
> Hi everyone
>
> I am trying to design some code but can't seem to find the best way of
> doing this. Basically I am trying to create an image class. The class
> has 4 member variables, the number of channels (how many planes define
> the image, for example if it's a B&W image m_numChannels = 1 & if it's
> an RGB image then m_numChannels = 3). There's also its width, height
> and finally a pointer to the data (stored as float).


Have you evaluated existing image processing libraries? There are some
very sophisticated libraries out there which solve this exact problem
(and probably quite some others which you haven't even considered
yet.)

Take a look at boost GIL (http://www.boost.org/doc/libs/1_40_0/libs/
gil/doc/index.html look at the Tutorial and Design Pages) and VIGRA
(http://hci.iwr.uni-heidelberg.de/vigra/).

If nothing else it will give you the answers you need to design your
own class.

HTH

Fabio
 
Reply With Quote
 
mast4as
Guest
Posts: n/a
 
      09-14-2009
Thanks everyone for your help.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
program design - template design pattern Adam Akhtar Ruby 5 03-27-2009 07:54 AM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Design issue : "self type" as a default template argument (recursive template arguments) IR C++ 3 11-22-2006 08:38 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments