"Vlfeat" O (n) sorting algorithm--counting sort

Source: Internet
Author: User

Today want to find a good implementation of the ER algorithm on the Internet, no specific information, helpless can only see Vlfeat mser source code, see if can tinker to achieve an ER.

So, see a section of the feeling is very magical, so put down the code, run to write a blog, which is the paragraph

1 /* -----------------------------------------------------------------2 * Sort pixels by intensity3    * -------------------------------------------------------------- */4 5   {6 vl_uint buckets [vl_mser_pix_maxval];7 8     /*Clear Buckets*/9memset (Buckets,0,sizeof(vl_uint) *vl_mser_pix_maxval);Ten  One     /*Compute bucket size (how many pixels for each intensity A value)*/ -      for(i =0; I < (int) Nel; ++i) { -Vl_mser_pix v =im [i]; the++buckets [v]; -     } -  -     /*cumulatively Add bucket sizes*/ +      for(i =1; i < Vl_mser_pix_maxval; ++i) { -buckets [i] + = buckets [i1] ; +     } A  at     /*empty buckets computing pixel ordering*/ -      for(i = nel; I >=1 ; ) { -Vl_mser_pix v = im [--i]; -Vl_uint j =--buckets [v]; -Perm [j] =i; -     } in}

I look at the notes to say the sort, I think this is why even sort also to build wheels, why not directly with a quick row of what, then carefully read the code, only to find otherwise, the complexity is O (n).

The purpose of this code is to put a picture of the pixel gray values in ascending order, where the use of the smart pixel value is within 0-255 this feature, specifically opened up a 256-length array, the number of pixels to record each grayscale value, that is, this paragraph:

1 /* Compute bucket size (how many pixels for each intensity2       */3      for0 ; I < (int) nel; + +i) {4       Vl_mser_pix v = im [i]; 5       + + buckets [v]; 6     }

This statistic is then converted to the number of pixels smaller than the grayscale value:

1     /*  */2for     1 ; i < vl_mser_pix_maxval; + +i) {3       buckets [i] + = buckets [-1]; 4     }

Pixels smaller than pixels are buckets[m], then M is ranked between Buckets[m-1] to Buckets[m]. Each appeared a m,buckets[m]--,m in the buckets[m] place.

1     /*  */2for     1  ;) {3       Vl_mser_pix v = im [--i]; 4       Vl_uint j =-- buckets [v]; 5       Perm [j] = i; 6     }

Baidu later found that this is called the counting sort. This sort does not need to be compared and can be done in O (n+k) time. n is the number of arrays, and K is the range of values for the array. In general, this algorithm is only suitable for small k cases.

"Vlfeat" O (n) sorting algorithm--counting sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.