Tuesday, 25 March 2014

Unordered_multimap

Hash tables are structures that allow you to search elements using a key. In my recent publication, I am using the data structure that was named "Voxel Hashing" to test the efficiency of hash table while voxelizing/interpreting full-waveform LiDAR data for 3D polygonal mesh creation [1]. This approach is the one selected to be used on the open source software DASOS [2] since it does not store empty voxels and even though it was not the fastest in the processes of 3D polygonal mesh creation, it is able to find the value of a voxel at constant time O(n) using the Hash function. The C++ implementation of the unordered_multimap is used and this blog post explains how to use it. 

Unordered_multimap:

As mentioned before, it is used for quick search and it allows you to save more than one values with  the same key, while unordered_map doesn't. How does it work? Simply, for every key there is a bucket where all the values associated with that key are saved. 

So, unordered_multimap is in the same header file with unordered_map, so the header file that need to be included is:

#include < unordered_map >

To create a map you need to define the type of the two components of the map. For example:

std::unordered_multimap < unsigned int , std::string > myMap;

if you would like to give initial values in the map you can do it as follow:

// this gives the initial values to the constructor
std::unordered_multimap < unsigned int , std::string > myMap ({{100,"a"},{120,"b"}});

// while the following on initialises the map and then adds the values
std::unordered_multimap < unsigned int , std::string > myMap ={{100,"a"},{120,"b"}};

If you would like to add items you can do it using the command "emplace":

// add more elements
myMap.emplace(100,"c");
myMap.emplace(100,"d");
myMap.emplace(200,"e");

You may like to loop through all its elements as follow:

//loop through all its elements
std::cout << "These are all the elements saved into the map:\n";
for (auto& x: myMap)
{
   std::cout << "(" << x.first << " , " << x.second << ")\n";
}

And in my opinion the most important feature is to be able to loop through all the elements with the same key. Here is an example of how you may do it:

// print all the elements with Key = 100
 unsigned int key = 100;
 std::cout << "These are all the elements with key value: " << key << "\n";
 auto itsElements = myMap.equal_range(key);
 for (auto it = itsElements.first; it != itsElements.second; ++it)
 {
    std::cout << "(" << it->first << " , " << it->second << ")\n";
 }

By the end, another small useful feature is the ability to query the unordered_multimap and get the number of elements that exists with the same key. This is achieved using the command count() as follow:


// counts how many entries exist with the same key
std::cout << "There are " << myMap.count(key) << " entries with key equal to " << key << "\n";



To sum up here is the entire program, which defines an unordered_multimap with some initial values, adds 3 more elements, prints all the elements inside the map and finally prints all the elements with key 100.

#include < iostream >
#include < unordered_map >

int main(int /*argc*/, char **/*argv*/)
{
   // define and initialise map
   std::unordered_multimap < unsigned int , std::string > myMap ({{100,"a"},{120,"b"}});

   // add more elements
   myMap.emplace(100,"c");
   myMap.emplace(100,"d");
   myMap.emplace(200,"e");

   //loop through all its elements
   std::cout << "These are all the elements saved into the map:\n";
   for (auto& x: myMap)
   {
      std::cout << "(" << x.first << " , " << x.second << ")\n";
   }

   // print all the elements with Key = 100
   unsigned int key = 100;
   std::cout << "These are all the elements with key value: " << key << "\n";
   auto itsElements = myMap.equal_range(key);
   for (auto it = itsElements.first; it != itsElements.second; ++it)
   {
       std::cout << "(" << it->first << " , " << it->second << ")\n";
   }

   // counts how many entries exist with the same key
   std::cout << "There are " << myMap.count(key) << " entries with key equal to " << key << "\n";


   return 0;
}

And the output of the program is the following:

These are all the elements saved into the map:
(200 , e)
(100 , d)
(100 , c)
(100 , a)
(120 , b)
These are all the elements with key value: 100
(100 , d)
(100 , c)
(100 , a)
There are 3 entries with key equal to 100

References

[1] Miltiadou, M.; Campbell, N.D.F.; Cosker, D.; Grant, M.G. A Comparative Study about Data Structures Used for Efficient Management of Voxelised Full-Waveform Airborne LiDAR Data during 3D Polygonal Model Creation. Remote Sens. 202113, 559. https://doi.org/10.3390/rs13040559

[2] Miltiadou, M., Grant, M. G., Campbell, N. D., Warren, M., Clewley, D., & Hadjimitsis, D. G. (2019, June). Open source software DASOS: Efficient accumulation, analysis, and visualisation of full-waveform lidar. In Seventh International Conference on Remote Sensing and Geoinformation of the Environment (RSCy2019) (Vol. 11174, p. 111741M). International Society for Optics and Photonics. 

Wednesday, 15 January 2014

For each in C++, std::vector

Here it is an example code of using for each in c++ in three different ways. The code first initialises an array with values (1 2 4 5) and then it adds 1 to each one of its elements 3 times.
The output is : 4 5 7 8

//function that adds one to a given reference value
void addOne(int &n){n++;}

// function that prints all the elements of a given vector
void print(const std::vector &i_vec)
{
    for(const int &n: i_vec){std::cout << n << " ";}
    std::cout << "\n";
}

int main(void)
{
  //initialisation of an std::vector
  std::vector myvec{1,2,4,5};

  // first method of adding one to each of its elements
  for (int &n : myvec){n++;}
  // 2nd method
  std::for_each(myvec.begin(), myvec.end(),[](int &n){n++;});
  // 3rd method by calling the addOne function
  std::for_each(myvec.begin(), myvec.end(),addOne);

  // print the values of the array
  print(myvec);

  return 0;
}

Wednesday, 6 November 2013

Summed Area Tables (Integral Images), Explanation and Implementation in C++

In 1984, Crow proposed an image representation where each pixel value is replaced by the sum of all the pixels that belong to the rectangle defined by the lower left corner of the image and the pixel of our interest [1].
Even though more storage space may be required to save the image, the sum of every rectangle in the image can be calculated in constant time once the table is constructed. The summed area table can be constructed in linear time O(n), where n is the number of pixels in the image, since one iteration through the entire image is enough to replace the pixel values inside the area table.

Figure 1. This figure depicts the parameters of the following equation [2].

 
So, let’s assume the we have the above image and we would like to find the sum of the blue area defined by the pixels (x,y) and (x+lenX, y+lenY) included. Then the sum is given by: 
sum =  T(x+lenX, y+lenY) - T(x+lenX, y-1) - T(x-1,y+lenY) +  T(x-1, y-1)
      
Figure 2. Once the Integral Image is constructed, the sum of any rectangular area is calculated in constant time [2].


Where T(x,y) is the value in the table with coordinates (x,y).

Code Available here:
https://www.dropbox.com/s/83oynkf11rrjd21/SumTables.zip

The images are taken from the following article, which explains how Sum Area Tables, in other words Integral Images, are used in 3D (named Integral Volumes) to optimise reconstruction of polygon representations from voxelised data[2]: https://www.mdpi.com/2072-4292/13/4/559



Work Cited

[1] Crow, F.C. (1984, July). Summed-Area Tables for Texture Mapping. ACM, Computer Graphics, Volume 18, Number 3

[2] Miltiadou M, Campbell NDF, Cosker D, Grant MG. A Comparative Study about Data Structures Used for Efficient Management of Voxelised Full-Waveform Airborne LiDAR Data during 3D Polygonal Model Creation. Remote Sensing. 2021; 13(4):559. https://doi.org/10.3390/rs13040559

Friday, 18 October 2013

std::unordered_map example

Unordered map is a structure that allows you to save data in an easy to access format.

For example, let assume that we would like to create a telephone catalogue. If we use an std::vector then looping though all the contact to find a number is time expensive. Instead we can use an std::unordered_map and have a much quicker search of data.

So this is an example code using the unordered_map:

#include 
#include 


int main(void)
{
   // initialise the map which takes as input a string and an integer
   std::unordered_map < std::string,unsigned int > mymap;

   // create and insert a new contact
   std::pair < std::string, unsigned int > pair("Maria",300);
   mymap.insert(pair);

   // search for a contract
   std::string input = "Maria";

   std::unordered_map < std::string,unsigned int > ::const_iterator got = mymap.find(input);
   if(got == mymap.end())
   {
       std::cout << "Contact does not exist\n";
   }
   else
   {
       std::cout << got -> first << "'s number is " << got->second <<"\n";
   }

  return 0;
}


So if you run the above example, this is what you get:

Maria's number is 300


Please note that unsigned int is not a prober type to save telephone numbers, because telephone numbers if are treated as numbers then you can easily end up with an overflow.
In this example I just wanted to show that unordered map allows you to save two different types of variables. A better approach will have been to have two string instead of string and an unsigned int.

Wednesday, 9 October 2013

Merge Sort without Recursion.

MathJax TeX Test Page
Well, I needed a sorting algorithm in C++, so I decided to write my own. Merge sort is quick, O(nlogn), and stable. Recursion is also slow so I decided to write it without recursion:


  // array to be sorted
  int array[] = {12,4,10,2,3,2,8,7,-1,-4,14,8,9,2,11};
  unsigned int len = 15;
  // allocate memory for temporarly saved values
  std::vector tempValues;
  tempValues.resize(len);
  // start sorting 2 elements each time, then merge them with the two next to them etc
  for(int step=2; step/2 < len; step*=2)
  {
     for(unsigned int i=0; i < len; i+=step)
     {
        int endOfT2 = i+step;
        if(i+ step/2 >= len)
        {
           continue;
        }
        else if (i+step >= len)
        {
           endOfT2 = len;
        }
        // both sets have step/2 items.
        // t1 points to the first set of values
        int t1 = i;
        // t2 points to the second set of values
        int t2 = i+step/2;
        // here we save all the values that have been overridden from the first set
        unsigned int tempIndex=0;
        while(t1 < i+step/2 && t2 < endOfT2)
        {
           if(array[t1]>array[t2])
           {
              tempValues[tempIndex]=array[t1];
              t1++;
           }
           else
           {
              tempValues[tempIndex]=array[t2];
              t2++;
           }
           tempIndex++;
        }
        while(t1 < i+step/2)
        {
           tempValues[tempIndex]=array[t1];
           t1++;
           tempIndex++;
        }
        // write values back to the array
        for(unsigned int t=0; t < tempIndex; ++t)
        {
            array[i+t]=tempValues[t];
        }
     }
  }

  // print the sorted array
  for(unsigned int i=0; i < len; ++i)
  {
      std::cout << " " << array[i];
  }
  std::cout << "\n";

Thursday, 26 September 2013

Reading Binary Files into Structures - C++

Hello,

this tutorial aims to give an overview on how to read binary files into structures in C++. These are the methods used for reading the LAS files in DASOS [1], an open source softwaring for managing full-waveform LiDAR data. I found reading binary files pretty interesting and challenging at the same time, so I decided to write a short tutorial about it.

If you want to read a binary file, you should first know how the bytes are structured inside the file. For example the first 10bytes may represents a word, the next 4 bytes may be a float number, the next 6 bytes may be 3 short int numbers, etc. For that reason you should also know how many bytes each type is. If not then, you can use the sizeof(<type>) command and find out. 

Let's assume that we have a file with a word, a float number and 3 short ints as the above example, then a struct with these information should be defined.

typedef struct myStructure
{
   char word[10];           // 10 bytes                    
   float number;            //  4 bytes
   short int A;             //  2 bytes
   short int B;             //  2 bytes
   short int C;             //  2 bytes
}myStructure;

The above should be 20 bytes, but that is not guarantee. While I was writing my code I came across a case where my struct should have been 235 bytes, but sizeof(myStructure) returned 243. This occured because of the way C++ allocates memory for structures. In order to avoid it, you have to use #pragma and specify how your data should be packed. If not then your binary data will not match with the structure since you will try to match 235 bytes into a structure which is 243 and the results will be wrong. 

#pragma pack(push)
#pragma pack(1)
typedef struct myStructure
{
   char word[10];           // 10 bytes                    
   float number;            //  4 bytes
   short int A;             //  2 bytes
   short int B;             //  2 bytes
   short int C;             //  2 bytes
}myStructure;
#pragma pack(pop)

Once the structure is defined the next step is to open the file as follow:
file.open(filename.c_str(),std::ios::binary);
if(!file.is_open())
{
   std::cerr << "File noT found \n"
   exit(EXIT_FAILURE);
}

Then define a variable of type myStructure and read the data into the structure:

myStructure data;
file.read((char *) &data,sizeof(data));

and you are done! The data is now into the structure.

There were occasions where I couldn't read the data straight into the structure, because I didn't know the length of a few variable from the beginning. This problem was solved by first reading all the data into an array of char (each char is a byte) and then use memcpy to copy the data into structures or arrays.

 char allData [sizeOfAllData];
// read all the data from the binary file
file.read((char *) allData,sizeOfAllData);
// in this example we want to read ints
int partOfData = new (std::nothrow) int[numOfInts];
// testing if memory has been allocated for that data
if(partOfData==0) // memory  couldn't not been allocated
{
   std::cout << "Allocation of memory failed\n"
   exit(EXIT_FAILURE);
}
memcpy((void *)partOfData,(void *)allData,numOfInts*sizeof(int));

By the end once we get the information we need, we should close the file:

   file.close();

Please note that most of the code is written by heart, so there may be a few spelling mistakes.

I hope you find this tutorial useful. If you have any comments, corrections or questions please don't hesitate to contact me. =)


More information about the software here:
Miltiadou, M., Grant, M. G., Campbell, N. D., Warren, M., Clewley, D., & Hadjimitsis, D. G. (2019, June). Open source software DASOS: Efficient accumulation, analysis, and visualisation of full-waveform lidar. In Seventh International Conference on Remote Sensing and Geoinformation of the Environment (RSCy2019) (Vol. 11174, p. 111741M). International Society for Optics and Photonics.

Tuesday, 4 September 2012

Geometrical Correspondence Tools for Facial Animation Production

The project I most enjoyed is my undergraduate individual project with supervisor Dr Tilo Burghardt from University of Bristol. 


1. Motivation



A project carried out at Cardiff University captures the motion of a human’s face and produces a sequence of 48 spatio-temporal models per second using 3dMDFaceTM Dynamic System [1]. But those facial scans are really noisy and around 10TB are required for a 10mins video. In order to merge them into a single representation and improve their quality, semantically identical facial elements should be associated between the modes.

2. Related Work


1. Accurate registration based on symmetry plane around nose
[X.M Tang, J.S. Chen and Y.S Moon. Accurate 3D face registration based on the symmetry plane analysis of nose region. 2008]
2. Real-time Face Pose Estimation
[Michael D.Breinstein, Daniel Kuettel, Thibaut Weise, Luc Van Goolm Hanspeter Pfister. Real-time Face Pose Estimation from Single Range Images. [2008]
3. Constructing a realistic Face Model of An Individual for Expression Animation         
[Yu Zhang, Edmond C. Prakash and Eric Sung. Constructing a  realistic Face Model of An


3. Outline of the Application


That project is able to resolve the approximate position of key features from a facial points cloud.

It is robust in terms of:

1.         Scaling
2.         Rotation
3.         Translation
4.         Noise


It is achieved in four stages as shown below:
Figure 1: Outline of the Application

4. Plane Initialisation

Initialise a plane that is approximately aligned with the face. Find a few peripheral points and used then to find an amount of backwards-pointing vectors. The initial plane passes through the centre of gravity and its normal is the average of those vectors. 


Figure 2: Calculation of a backwards pointing vector

5. Iterative Algorithm


An iterative Algorithm with three degrees of freedom is used to find the bilateral symmetry axis of the face.


The iterative algorithm has 3 sub-iterations; each one serves a degree of freedom:
1.         Rotation of the plane around its normal.
2.         Shifting the plane left and right.
3.         Rotation of the plane around the symmetry axis.

During each iteration the mirror difference is calculated. 


Figure 3: Mirror Difference of the face, P is an array with sorted points
 that lie on the plane that is aligned with the face

During each sub-iteration, the mirror difference (A2, A3 and A4) is calculated for input values B2, B3 and B4. After each sub-iteration, the algorithm divides its observation field and repeats until the observation field become too small to be divided again.
Figure 4: How the  observation field is halved during each iteration
The sub-iterations run the one after the other. Once they all converge, the system runs them again and repeats until no changes are observed.


6. Posing a Template

By posing a template the plane is rotated in another degree of freedom and the actual facial part is detected. 

Figure 5: Comparison with pre-computed reference of range images,
using an Error Function


7. Detection of Key Points:

Once the face is aligned, the approximate position of key Points are detected using curvature analysis and outward projection of points. 

8. Evaluation:

The following image shows the evaluation of the program:

Figure 8:  Initial Invariance
Figure 9: Symmetry axis
Figure 10: Range Images
Figure 11: Key Points

9. Future Work


Once the features of interest are found:

1.         Polymesh sequences can be merged into a single representation
2.         Mesh quality can be improved and
3.         Their motion can be driven by another model.

The facial registration system is also a useful tool in expression analysis and face recognition systems.






10. Related Links 

Poster: https://www.dropbox.com/s/md6c3r7rv4rjhd7/undegraduatePoster.pdf
Full Thesis: https://www.dropbox.com/s/0znaz8x7msc77wj/udegraduateThesis.pdf