why and how does this size() value change?

this has been bugging me for hours :(
i have marked some numbers in the code to follow.
1) the size is 226
2) the size is 233
3) the size is 390
then the size remains constand but for some reason, between 1) and 3) the size of this variable is changing and i cannot for the life of me understand how or why?
i can only guess it has something to do with std::map.

any ideas?
and thanks for taking a look.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

   std::vector <int> newIntensity;
   std::map <float, float> totalGreyValues;

   // total number of each grey value in cloud
   for(unsigned int i = 0; i < newIntensity.size(); i++)
      if(newIntensity[i] >= intensityMin && newIntensity[i] <= intensityMax)
         totalGreyValues[newIntensity[i]] = totalGreyValues[newIntensity[i]] + 1;

1)   std::cout << "total grey values size: " << totalGreyValues.size() << std::endl;

   std::cout << "total grey values: " << std::endl;
   for (unsigned int h = 0; h < 30; h ++) //totalGreyValues.size()
      std::cout << "   " << totalGreyValues[h];


2)   std::cout << "total grey values size: " << totalGreyValues.size() << std::endl;

   
   float percentage = 0;
   // probability of a pixel being of that grey value
   for (unsigned int i = 0; i < totalGreyValues.size(); i++) {
      percentage = (totalGreyValues[i] / inCld->size());
      percentageGreyValues.push_back(percentage);
   }
   
   std::cout << std::endl << "percentage size: " << percentageGreyValues.size() << std::endl;
3)   std::cout << "totalGreyValues size: " << totalGreyValues.size() << std::endl;
It looks like you have left out some code from between the lines.
Last edited on
your right, i did leave out some lines. i tried to only include the important parts.
here is a bigger picture.

the main focus (i think) is 'totalGreyValues.size()'



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85


   std::vector <int> newIntensity;
   std::vector <float> percentageGreyValues;
   std::vector <float> cdf;
   std::map <float, float> totalGreyValues;
   std::vector <float> equalizedHistogram;
   std::vector <float> equalizedRoundHistogram;
   
   int intensityMin = 100000;
   int intensityMax = 0;
   int cdfMin = 10000;
   int L = 256; //maximum grey value;
   float sum = 0;
   float one = 0;
   float roundFin = 0;


   std::cout << std::endl << "size of incld: " << inCld->size() << std::endl;
   std::cout << "original intensity: " << std::endl;
   for (unsigned int i = 0; i < 30; i++) {
      std::cout << "  " << inCld->points[i].intensity;
      inCld->points[i].intensity = floor(inCld->points[i].intensity + 0.5);
   }

   
   std::cout << std::endl << "rounded intensity: " << std::endl;
   for (unsigned int i = 0; i < 30; i++)
      std::cout << "  "  << inCld->points[i].intensity;

   // create new vector with only intensity values
   for (unsigned int i = 0; i < inCld->size(); i++)
      newIntensity.push_back (inCld->points[i].intensity);

   std::cout << std::endl << "intensity only vector: " << std::endl;
   for (unsigned int i = 0; i < 30; i++) // inCld->size()
      std::cout << "  " << newIntensity[i];

   // finds the min and max intensity values
   for (unsigned int i = 0; i < inCld->size(); i++)
      if (inCld->points[i].intensity < intensityMin)
         intensityMin = inCld->points[i].intensity;

   for (unsigned int i = 0; i < inCld->size(); i++)
      if (inCld->points[i].intensity > intensityMax)
         intensityMax = inCld->points[i].intensity;

   std::cout << std::endl << "min: " << intensityMin << std::endl;
   std::cout << "max: " << intensityMax << std::endl;

   // total number of each grey value in cloud
   for(unsigned int i = 0; i < newIntensity.size(); i++)
      if(newIntensity[i] >= intensityMin && newIntensity[i] <= intensityMax)
         totalGreyValues[newIntensity[i]] = totalGreyValues[newIntensity[i]] + 1;

1)   std::cout << "total grey values size: " << totalGreyValues.size() << std::endl;

   std::cout << "total grey values: " << std::endl;
   for (unsigned int h = 0; h < 30; h ++) //totalGreyValues.size()
      std::cout << "   " << totalGreyValues[h];

2)   std::cout << "total grey values size: " << totalGreyValues.size() << std::endl;

   float percentage = 0;
   // probability of a pixel being of that grey value
   for (unsigned int i = 0; i < greySize; i++) {  // totalGreyValues.size()
      percentage = (totalGreyValues[i] / inCld->size());
      percentageGreyValues.push_back(percentage);
   }
   
   std::cout << std::endl << "percentage size: " << percentageGreyValues.size() << std::endl;
3)   std::cout << "totalGreyValues size: " << totalGreyValues.size() << std::endl;


   std::cout << "percentage grey values: " << std::endl;
   for (unsigned int h = 0; h < 30; h ++)  // percentageGreyValues.size()
      std::cout << "   " << percentageGreyValues[h];


   for (unsigned int i = 0; i < greySize; i++)  // totalGreyValues.size()
      one = one + percentageGreyValues[i];
   
   std::cout << std::endl << "sum of probability: " << one << std::endl;

When you use operator[] to lookup a value with a key that is not in the map a new element is created and the size is increased by one. I think that could be the reason.
What Peter87 said, is exactly what is happening.

In the first case 1)
1
2
 if(newIntensity[i] >= intensityMin && newIntensity[i] <= intensityMax)
         totalGreyValues[newIntensity[i]] = totalGreyValues[newIntensity[i]] + 1;


Anytime the if evaluates to true, if that subscript newIntensity[i] does not exist in the map it is inserted.

2)

1
2
 for (unsigned int h = 0; h < 30; h ++) //totalGreyValues.size()
      std::cout << "   " << totalGreyValues[h];


From 0 to 29, if any of those subscripts are not already in the map they are inserted when you do your cout line.

3)
1
2
3
4
for (unsigned int i = 0; i < greySize; i++) {  // totalGreyValues.size()
      percentage = (totalGreyValues[i] / inCld->size());
      percentageGreyValues.push_back(percentage);
   }
Same thing here, 0 to greySize - 1 is inserted into the map if they don't already exist.
Topic archived. No new replies allowed.