-1

I would like to read and store a BMP file in a QuadTree:

An example of the BMP file would be:

P1
4 4
1 0 1 1
0 1 0 0
1 1 0 0
1 1 0 0

and the structure of the QuadTree I have thought of is:

struct QuadTreeNode{
   int size;
   struct QuadTreeNode *children[4];
   int color;    //0 white, 1 black, -1 div
};

I am having trouble thinking of a way to build the QuadTree. What would a good way be to build the QuadTree as the file is read? Should I start with the leaves or with the main QuadTree?

Wryfyng
  • 31
  • 8
  • In my opinion, you should start be writing all the leaves and then start with the main Quadtree in such a way that when a leaf node appears in the main Quadtree you would search its data in the list of leaves written before – Pedro Pereira Mar 19 '18 at 15:06

2 Answers2

0

I would start defining some nice structure for storing images that is also able to map sub images into the original data;

e.g.

struct Img{
  int   width;
  int   height;
  int   rowstep; ///< in multiples of pixel data type
  int*  data; ///< ptr type may be different or void
};

Then i would read/convert the data from the file into some memory (parse the image value) and create a toplevel img:

Img toplevel {4,4, 32, dataptr};

4 subimges:

Img tl {toplevel.width/2,toplevel.height/2, toplevel.rowstep, toplevel.data};
Img tr {toplevel.width/2,toplevel.height/2, toplevel.rowstep, toplevel.data+toplevel.width/2};
Img bl {toplevel.width/2,toplevel.height/2, toplevel.rowstep, toplevel.data + toplevel.rowstep*toplevel.height/2};
Img br {toplevel.width/2,toplevel.height/2, toplevel.rowstep, toplevel.data + toplevel.rowstep*toplevel.height/2 + toplevel.width/2};

You can now recursivly split each img into 4 smaller as long as the width is greater 1 (recursive descent).
Then you have the color value for the leafs.
On the recursive ascension you can sum/mean the 4 values from the deeper levels (and throw away the sub images if you do not need them).

At the end you should have a complete quadtree.

PS:

One additional comment:

I would try not to use raw-Pointer in the Quadtree structure. Maybe use std::array<std::shared_ptr<QuadTreeNode>, 4> children;

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
0

What is the variable size for ?

Your structure should also contain a pointer to the parent node so that you can move free in the quadtree.

Your variable which holds the color should be replaced by a struct which contains 3 variables representing a color since a pixel is typically represented by 3 components: red, green and blue. If you are processing a binary image a bool or char will be sufficient.

In your structure you need an additional variable ones which have the position and the size of the quadtree.

Your structure could look something like this

struct QuadtreeNode{
    short x1;
    short y1;
    short x2;
    short y2;
    Color_Pixel pixel;        
    struct QuadtreeNode *parent;
    struct QuadtreeNode *quad[4];
};

struct Color_Pixel{
    uchar r;
    uchar g;
    uchar b;
};

Your question

Should I start with the leaves or with the main QuadTree?

You should start with the "main Quadtree" this first node represents the entire image, then you have to divide your image into further nodes or "leaves". In your destructor you have to move from bottom to the top in order to free your allocated ressources correctly, there you should start with the "leaves".

Daniel B
  • 91
  • 1
  • 5