Stuck with design & template

Discussion in 'C++' started by mast4as, Sep 10, 2009.

  1. mast4as

    mast4as Guest

    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
     
    mast4as, Sep 10, 2009
    #1
    1. Advertising

  2. mast4as

    mzdude Guest

    On Sep 10, 1:05 pm, 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)
    >   {
    >     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.
     
    mzdude, Sep 10, 2009
    #2
    1. Advertising

  3. mast4as

    Ian Collins Guest

    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
     
    Ian Collins, Sep 11, 2009
    #3
  4. mast4as

    Krice Guest

    On 10 syys, 20:05, mast4as <> 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.
     
    Krice, Sep 11, 2009
    #4
  5. On 10 Sep., 19:05, 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).


    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
     
    Fabio Fracassi, Sep 11, 2009
    #5
  6. mast4as

    mast4as Guest

    Thanks everyone for your help.
     
    mast4as, Sep 14, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    478
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    538
    tom_usenet
    Jul 24, 2003
  3. IR
    Replies:
    3
    Views:
    421
  4. Andy B.

    solution design - I'm stuck

    Andy B., Apr 15, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    246
    Andy B.
    Apr 15, 2009
  5. Adam Akhtar
    Replies:
    5
    Views:
    337
    Robert Klemme
    Mar 27, 2009
Loading...

Share This Page