Stuck with design & template

M

mast4as

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
 
M

mzdude

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.
 
I

Ian Collins

mast4as said:
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.
 
K

Krice

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.
 
F

Fabio Fracassi

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top