Finding the 20th percentile pixel of an image using a histogram instead of std::sort I have a 2-dimensional matrix (an image) in which I need to find the 20th percentile value.  My first attempt was to sort the values and then index using std::floor(0.2*(srcSor.size())/100).

My code was originally straightforward like this:

#include &lt;opencv2/highgui.hpp&gt;
#include &lt;opencv2/imgproc.hpp&gt;

#include &lt;iostream&gt;

int main(int argc, char* argv[])
{   
   //Read the image .jpeg
   cv::Mat src=cv::imread(argv[1], CV_8UC1);
   std::vector&lt;double&gt; srcSor = src.reshape(0, 1); 
   std::sort (srcSor.begin(), srcSor.end(), std::greater&lt;double&gt;());  
   float A = srcSor[std::floor(0.2*(srcSor.size())/100)];
   //Then later I use A value..
   .
   .

   return 0;
}


I found that std::sort took a long time to sort a vector of 1.3m unsigned values.
So now I'm using an histogram to do the same thing; this takes less time to find that value of that certain index. The idea comes from the fact that the values of the matrix are unsigned int in the range 0-255:

#include &lt;opencv2/highgui.hpp&gt;
#include &lt;opencv2/imgproc.hpp&gt;

#include &lt;iostream&gt;

int main(int argc, char* argv[])
{   
   //Read the image .jpeg
   cv::Mat src=cv::imread(argv[1], CV_8UC1);

   std::vector&lt;unsigned&gt; histo(256,0.0);
   for(unsigned j=0;j&lt;src.rows;j++)
      for(unsigned i=0;i&lt;src.cols;i++)
          histo[(int)src.at&lt;uchar&gt;(j,i)]++;

   std::vector&lt;unsigned&gt; sumHH(256,0.0);
   sumHH[255]=histo[255];
   for(unsigned i=254;i&gt;=1;i--)
        sumHH[i]=sumHH[i+1]+histo[i];

   int A=0;
   int indx=std::floor(0.2*(src.rows*src.cols)/100);            
   for(unsigned i=1;i&lt;255;i++)
     if(indx&lt;sumHH[255-i])
     {
        A=255-i;
        break;  
     }
   //Then later I use A value..
   .
   .

   return 0;
}


But I think that my code is not really readable, can anyone suggest a better way to write my code and/or better optimized?
Well, index=std::floor(0.2*(srcSor.size())/100 is equivalent to the much simpler index = srcSor.size() / 500, ignoring some inaccuracy in representing 0.2.
Lose the fixed-size std::vectors. You don't depend on any of the benefits of using dynamic memory, so a simple array is sufficient and far more efficient.
Next, you really don't need the cumulative counts in sumHH at all. Just test whether you have reached the target immediately.
Don't cast unless you have to. Casting means overriding the type-system, and is thus error-prone.
Don't use obscure abbreviations.


Resulting code:

unsigned histogram[256] = {};
for (auto j = src.rows; j--;)
    for (auto i = src.cols; i--;)
        ++histogram[src.at&lt;uchar&gt;(j,i)];
int A = 255;
for (auto index = src.rows * src.cols / 500; index &gt; histogram[A]; --A)
    index -= histogram[A];