You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1001 lines
30 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file mser.c
  2. ** @brief MSER - Definition
  3. ** @author Andrea Vedaldi
  4. **/
  5. /*
  6. Copyright (C) 2007-13 Andrea Vedaldi and Brian Fulkerson.
  7. All rights reserved.
  8. This file is part of the VLFeat library and is made available under
  9. the terms of the BSD license (see the COPYING file).
  10. */
  11. /**
  12. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  13. @page mser Maximally Stable Extremal Regions (MSER)
  14. @author Andrea Vedaldi
  15. @tableofcontents
  16. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  17. @ref mser.h implements the *Maximally Stable Extremal Regions* (MSER)
  18. local feature detector of @cite{matas03robust}. This detector extracts
  19. as features the the connected components of the level sets of the
  20. input intensity image. Among all such regions, the ones that are
  21. locally maximally stable are selected. MSERs are affine co-variant, as
  22. well as largely co-variant to generic diffeomorphic transformations.
  23. See @ref mser-starting for an introduction on how to use the detector
  24. from the C API. For further details refer to:
  25. - @subpage mser-fundamentals - MSER definition and parameters.
  26. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  27. @section mser-starting Getting started with the MSER detector
  28. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  29. Running the MSER filter usually involves the following steps:
  30. - Initialize the MSER filter by ::vl_mser_new(). The
  31. filter can be reused for images of the same size.
  32. - Compute the MSERs by ::vl_mser_process().
  33. - Optionally fit ellipsoids to the MSERs by ::vl_mser_ell_fit().
  34. - Retrieve the results by ::vl_mser_get_regions() (and optionally ::vl_mser_get_ell()).
  35. - Optionally retrieve filter statistics by ::vl_mser_get_stats().
  36. - Delete the MSER filter by ::vl_mser_delete().
  37. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  38. @page mser-fundamentals MSER fundamentals
  39. @tableofcontents
  40. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  41. The *extermal regions* of an image are the connected components of the
  42. level sets $S_l = \{ x : I(x) \leq l \}, l \in \real$ of the image
  43. $I(x)$. Consider a discretization of the intensity levels $l$
  44. consisting of $M$ samples $\mathcal{L}=\{0,\dots,M-1\}$. The extremal
  45. regions $R_l \subset S_l$ of the level sets $S_l, l \in \mathcal{L}$
  46. can be arranged in a tree, where a region $R_l$ is a children of a
  47. region $R_{l+1}$ if $R_l \subset R_{l+1}$. The following figures shows
  48. a 1D example where the regions are denoted by dark thick lines:
  49. @image html mser-tree.png "Connected components of the image level sets arranged in a tree."
  50. Note that, depending on the image, regions at different levels can be
  51. identical as sets:
  52. @image html mser-er-step.png "Connected components when the image contains step changes."
  53. A *stable extremal region* is an extremal region that does not change
  54. much as the index $l$ is varied. Here we use a criterion which is
  55. similar but not identical to the original paper. This definition is
  56. somewhat simpler both to understand and code.
  57. Let $B(R_l)=(R_l,R_{l+1},\dots,R_{l+\Delta})$ be the branch of the
  58. tree $R_l \subset R_{l+1} \subset \dots \subset R_{l + \Delta}$
  59. rooted at $R_l$. We associate to the branch the (in)stability score
  60. @f[
  61. v(R_l) = \frac{|R_{l+\Delta} - R_l|}{|R_l|}.
  62. @f]
  63. This score is a relative measure of how much $R_l$ changes as the
  64. index is increased from $l$ to $l+\Delta$, as illustrated in the
  65. following figure.
  66. @image html mser-er.png "Stability is measured by looking at how much a region changes with the intensity level."
  67. The score is low if the regions along the branch have similar area
  68. (and thus similar shape). We aim to select maximally stable
  69. branches; then a maximally stable region is just a representative
  70. region selected from a maximally stable branch (for simplicity we
  71. select $R_l$, but one could choose for example
  72. $R_{l+\Delta/2}$).
  73. Roughly speaking, a branch is maximally stable if it is a local
  74. minimum of the (in)stability score. More accurately, we start by
  75. assuming that all branches are maximally stable. Then we consider
  76. each branch $B(R_{l})$ and its parent branch
  77. $B(R_{l+1}):R_{l+1}\supset R_l$ (notice that, due to the
  78. discrete nature of the calculations, they might be geometrically
  79. identical) and we mark as unstable the less stable one, i.e.:
  80. - if $v(R_l)<v(R_{l+1})$, mark $R_{l+1}$ as unstable;
  81. - if $v(R_l)>v(R_{l+1})$, mark $R_{l}$ as unstable;
  82. - otherwise, do nothing.
  83. This criterion selects among nearby regions the ones that are more
  84. stable. We optionally refine the selection by running (starting
  85. from the bigger and going to the smaller regions) the following
  86. tests:
  87. - $a_- \leq |R_{l}|/|R_{\infty}| \leq a_+$: exclude MSERs too
  88. small or too big ($|R_{\infty}|$ is the area of the image).
  89. - $v(R_{l}) < v_+$: exclude MSERs too unstable.
  90. - For any MSER $R_l$, find the parent MSER $R_{l'}$ and check
  91. if
  92. $|R_{l'} - R_l|/|R_l'| < d_+$: remove duplicated MSERs.
  93. <table>
  94. <tr>
  95. <td>parameter</td>
  96. <td>alt. name</td>
  97. <td>standard value</td>
  98. <td>set by</td>
  99. </tr>
  100. <tr>
  101. <td>$\Delta$</td>
  102. <td>@c delta</td>
  103. <td>5</td>
  104. <td>::vl_mser_set_delta()</td>
  105. </tr>
  106. <tr>
  107. <td>$a_+$</td>
  108. <td>@c max_area</td>
  109. <td>0.75</td>
  110. <td>::vl_mser_set_max_area()</td>
  111. </tr>
  112. <tr>
  113. <td>$a_-$</td>
  114. <td>@c min_area</td>
  115. <td>3.0/$|R_\infty|$</td>
  116. <td>::vl_mser_set_min_area()</td>
  117. </tr>
  118. <tr>
  119. <td>$v_+$</td>
  120. <td>@c max_var</td>
  121. <td>0.25</td>
  122. <td>::vl_mser_set_max_variation()</td>
  123. </tr>
  124. <tr>
  125. <td>$d_+$</td>
  126. <td>@c min_diversity</td>
  127. <td>0.2</td>
  128. <td>::vl_mser_set_min_diversity()</td>
  129. </tr>
  130. </table>
  131. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  132. @section mser-vol Volumetric images
  133. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  134. The code supports images of arbitrary dimension. For instance, it
  135. is possible to find the MSER regions of volumetric images or time
  136. sequences. See ::vl_mser_new() for further details
  137. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  138. @section mser-ell Ellipsoids
  139. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  140. Usually extremal regions are returned as a set of ellipsoids
  141. fitted to the actual regions (which have arbitrary shape). The fit
  142. is done by calculating the mean and variance of the pixels
  143. composing the region:
  144. @f[
  145. \mu_l = \frac{1}{|R_l|}\sum_{x\in R_l}x,
  146. \qquad
  147. \Sigma_l = \frac{1}{|R_l|}\sum_{x\in R_l} (x-\mu_l)^\top(x-\mu_l)
  148. @f]
  149. Ellipsoids are fitted by ::vl_mser_ell_fit(). Notice that for a
  150. <em>n</em> dimensional image, the mean has <em>n</em> components
  151. and the variance has <em>n(n+1)/2</em> independent components. The
  152. total number of components is obtained by ::vl_mser_get_ell_dof()
  153. and the total number of fitted ellipsoids by
  154. ::vl_mser_get_ell_num(). A matrix with an ellipsoid per column is
  155. returned by ::vl_mser_get_ell(). The column is the stacking of the
  156. mean and of the independent components of the variance, in the
  157. order <em>(1,1),(1,2),..,(1,n), (2,2),(2,3)...</em>. In the
  158. calculations, the pixel coordinate $x=(x_1,...,x_n)$ use the
  159. standard index order and ranges.
  160. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  161. @section mser-algo Algorithm
  162. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  163. The algorithm is quite efficient. While some details may be
  164. tricky, the overall idea is easy to grasp.
  165. - Pixels are sorted by increasing intensity.
  166. - Pixels are added to a forest by increasing intensity. The forest has the
  167. following properties:
  168. - All the descendent of a certain pixels are subset of an extremal region.
  169. - All the extremal regions are the descendants of some pixels.
  170. - Extremal regions are extracted from the region tree and the extremal regions tree is
  171. calculated.
  172. - Stable regions are marked.
  173. - Duplicates and other bad regions are removed.
  174. @remark The extremal region tree which is calculated is a subset
  175. of the actual extremal region tree. In particular, it does not
  176. contain redundant entries extremal regions that coincide as
  177. sets. So, for example, in the calculated extremal region tree, the
  178. parent $R_q$ of an extremal region $R_{l}$ may or may
  179. <em>not</em> correspond to $R_{l+1}$, depending whether
  180. $q\leq l+1$ or not. These subtleties are important when
  181. calculating the stability tests.
  182. **/
  183. #include "mser.h"
  184. #include<stdlib.h>
  185. #include<string.h>
  186. #include<assert.h>
  187. /** -------------------------------------------------------------------
  188. ** @brief Advance N-dimensional subscript
  189. **
  190. ** The function increments by one the subscript @a subs indexing an
  191. ** array the @a ndims dimensions @a dims.
  192. **
  193. ** @param ndims number of dimensions.
  194. ** @param dims dimensions.
  195. ** @param subs subscript to advance.
  196. **/
  197. VL_INLINE void
  198. adv(int ndims, int const *dims, int *subs)
  199. {
  200. int d = 0 ;
  201. while(d < ndims) {
  202. if( ++subs[d] < dims[d] ) return ;
  203. subs[d++] = 0 ;
  204. }
  205. }
  206. /** -------------------------------------------------------------------
  207. ** @brief Climb the region forest to reach aa root
  208. **
  209. ** The function climbs the regions forest @a r starting from the node
  210. ** @a idx to the corresponding root.
  211. **
  212. ** To speed-up the operation, the function uses the
  213. ** VlMserReg::shortcut field to quickly jump to the root. After the
  214. ** root is reached, all the used shortcut are updated.
  215. **
  216. ** @param r regions' forest.
  217. ** @param idx stating node.
  218. ** @return index of the reached root.
  219. **/
  220. VL_INLINE vl_uint
  221. climb (VlMserReg* r, vl_uint idx)
  222. {
  223. vl_uint prev_idx = idx ;
  224. vl_uint next_idx ;
  225. vl_uint root_idx ;
  226. /* move towards root to find it */
  227. while (1) {
  228. /* next jump to the root */
  229. next_idx = r [idx] .shortcut ;
  230. /* recycle shortcut to remember how we came here */
  231. r [idx] .shortcut = prev_idx ;
  232. /* stop if the root is found */
  233. if( next_idx == idx ) break ;
  234. /* next guy */
  235. prev_idx = idx ;
  236. idx = next_idx ;
  237. }
  238. root_idx = idx ;
  239. /* move backward to update shortcuts */
  240. while (1) {
  241. /* get previously visited one */
  242. prev_idx = r [idx] .shortcut ;
  243. /* update shortcut to point to the new root */
  244. r [idx] .shortcut = root_idx ;
  245. /* stop if the first visited node is reached */
  246. if( prev_idx == idx ) break ;
  247. /* next guy */
  248. idx = prev_idx ;
  249. }
  250. return root_idx ;
  251. }
  252. /** -------------------------------------------------------------------
  253. ** @brief Create a new MSER filter
  254. **
  255. ** Initializes a new MSER filter for images of the specified
  256. ** dimensions. Images are @a ndims -dimensional arrays of dimensions
  257. ** @a dims.
  258. **
  259. ** @param ndims number of dimensions.
  260. ** @param dims dimensions.
  261. **/
  262. VL_EXPORT
  263. VlMserFilt*
  264. vl_mser_new (int ndims, int const* dims)
  265. {
  266. VlMserFilt* f ;
  267. int *strides, k ;
  268. f = (VlMserFilt*)vl_calloc (sizeof(VlMserFilt), 1) ;
  269. f-> ndims = ndims ;
  270. f-> dims = (int*)vl_malloc (sizeof(int) * ndims) ;
  271. f-> subs = (int*)vl_malloc (sizeof(int) * ndims) ;
  272. f-> dsubs = (int*)vl_malloc (sizeof(int) * ndims) ;
  273. f-> strides = (int*)vl_malloc (sizeof(int) * ndims) ;
  274. /* shortcuts */
  275. strides = f-> strides ;
  276. /* copy dims to f->dims */
  277. for(k = 0 ; k < ndims ; ++k) {
  278. f-> dims [k] = dims [k] ;
  279. }
  280. /* compute strides to move into the N-dimensional image array */
  281. strides [0] = 1 ;
  282. for(k = 1 ; k < ndims ; ++k) {
  283. strides [k] = strides [k-1] * dims [k-1] ;
  284. }
  285. /* total number of pixels */
  286. f-> nel = strides [ndims-1] * dims [ndims-1] ;
  287. /* dof of ellipsoids */
  288. f-> dof = ndims * (ndims + 1) / 2 + ndims ;
  289. /* more buffers */
  290. f-> perm = (vl_uint*)vl_malloc (sizeof(vl_uint) * f-> nel) ;
  291. f-> joins = (vl_uint*)vl_malloc (sizeof(vl_uint) * f-> nel) ;
  292. f-> r = (VlMserReg*)vl_malloc (sizeof(VlMserReg) * f-> nel) ;
  293. f-> er = 0 ;
  294. f-> rer = 0 ;
  295. f-> mer = 0 ;
  296. f-> rmer = 0 ;
  297. f-> ell = 0 ;
  298. f-> rell = 0 ;
  299. /* other parameters */
  300. f-> delta = 5 ;
  301. f-> max_area = 0.75 ;
  302. f-> min_area = 3.0 / f-> nel ;
  303. f-> max_variation = 0.25 ;
  304. f-> min_diversity = 0.2 ;
  305. return f ;
  306. }
  307. /** -------------------------------------------------------------------
  308. ** @brief Delete MSER filter
  309. **
  310. ** The function releases the MSER filter @a f and all its resources.
  311. **
  312. ** @param f MSER filter to be deleted.
  313. **/
  314. VL_EXPORT
  315. void
  316. vl_mser_delete (VlMserFilt* f)
  317. {
  318. if(f) {
  319. if(f-> acc ) vl_free( f-> acc ) ;
  320. if(f-> ell ) vl_free( f-> ell ) ;
  321. if(f-> er ) vl_free( f-> er ) ;
  322. if(f-> r ) vl_free( f-> r ) ;
  323. if(f-> joins ) vl_free( f-> joins ) ;
  324. if(f-> perm ) vl_free( f-> perm ) ;
  325. if(f-> strides) vl_free( f-> strides) ;
  326. if(f-> dsubs ) vl_free( f-> dsubs ) ;
  327. if(f-> subs ) vl_free( f-> subs ) ;
  328. if(f-> dims ) vl_free( f-> dims ) ;
  329. if(f-> mer ) vl_free( f-> mer ) ;
  330. vl_free (f) ;
  331. }
  332. }
  333. /** -------------------------------------------------------------------
  334. ** @brief Process image
  335. **
  336. ** The functions calculates the Maximally Stable Extremal Regions
  337. ** (MSERs) of image @a im using the MSER filter @a f.
  338. **
  339. ** The filter @a f must have been initialized to be compatible with
  340. ** the dimensions of @a im.
  341. **
  342. ** @param f MSER filter.
  343. ** @param im image data.
  344. **/
  345. VL_EXPORT
  346. void
  347. vl_mser_process (VlMserFilt* f, vl_mser_pix const* im)
  348. {
  349. /* shortcuts */
  350. vl_uint nel = f-> nel ;
  351. vl_uint *perm = f-> perm ;
  352. vl_uint *joins = f-> joins ;
  353. int ndims = f-> ndims ;
  354. int *dims = f-> dims ;
  355. int *subs = f-> subs ;
  356. int *dsubs = f-> dsubs ;
  357. int *strides = f-> strides ;
  358. VlMserReg *r = f-> r ;
  359. VlMserExtrReg *er = f-> er ;
  360. vl_uint *mer = f-> mer ;
  361. int delta = f-> delta ;
  362. int njoins = 0 ;
  363. int ner = 0 ;
  364. int nmer = 0 ;
  365. int nbig = 0 ;
  366. int nsmall = 0 ;
  367. int nbad = 0 ;
  368. int ndup = 0 ;
  369. int i, j, k ;
  370. /* delete any previosuly computed ellipsoid */
  371. f-> nell = 0 ;
  372. /* -----------------------------------------------------------------
  373. * Sort pixels by intensity
  374. * -------------------------------------------------------------- */
  375. {
  376. vl_uint buckets [ VL_MSER_PIX_MAXVAL ] ;
  377. /* clear buckets */
  378. memset (buckets, 0, sizeof(vl_uint) * VL_MSER_PIX_MAXVAL ) ;
  379. /* compute bucket size (how many pixels for each intensity
  380. value) */
  381. for(i = 0 ; i < (int) nel ; ++i) {
  382. vl_mser_pix v = im [i] ;
  383. ++ buckets [v] ;
  384. }
  385. /* cumulatively add bucket sizes */
  386. for(i = 1 ; i < VL_MSER_PIX_MAXVAL ; ++i) {
  387. buckets [i] += buckets [i-1] ;
  388. }
  389. /* empty buckets computing pixel ordering */
  390. for(i = nel ; i >= 1 ; ) {
  391. vl_mser_pix v = im [ --i ] ;
  392. vl_uint j = -- buckets [v] ;
  393. perm [j] = i ;
  394. }
  395. }
  396. /* initialize the forest with all void nodes */
  397. for(i = 0 ; i < (int) nel ; ++i) {
  398. r [i] .parent = VL_MSER_VOID_NODE ;
  399. }
  400. /* -----------------------------------------------------------------
  401. * Compute regions and count extremal regions
  402. * -------------------------------------------------------------- */
  403. /*
  404. In the following:
  405. idx : index of the current pixel
  406. val : intensity of the current pixel
  407. r_idx : index of the root of the current pixel
  408. n_idx : index of the neighbors of the current pixel
  409. nr_idx : index of the root of the neighbor of the current pixel
  410. */
  411. /* process each pixel by increasing intensity */
  412. for(i = 0 ; i < (int) nel ; ++i) {
  413. /* pop next node xi */
  414. vl_uint idx = perm [i] ;
  415. vl_mser_pix val = im [idx] ;
  416. vl_uint r_idx ;
  417. /* add the pixel to the forest as a root for now */
  418. r [idx] .parent = idx ;
  419. r [idx] .shortcut = idx ;
  420. r [idx] .area = 1 ;
  421. r [idx] .height = 1 ;
  422. r_idx = idx ;
  423. /* convert the index IDX into the subscript SUBS; also initialize
  424. DSUBS to (-1,-1,...,-1) */
  425. {
  426. vl_uint temp = idx ;
  427. for(k = ndims - 1 ; k >= 0 ; --k) {
  428. dsubs [k] = -1 ;
  429. subs [k] = temp / strides [k] ;
  430. temp = temp % strides [k] ;
  431. }
  432. }
  433. /* examine the neighbors of the current pixel */
  434. while (1) {
  435. vl_uint n_idx = 0 ;
  436. vl_bool good = 1 ;
  437. /*
  438. Compute the neighbor subscript as NSUBS+SUB, the
  439. corresponding neighbor index NINDEX and check that the
  440. neighbor is within the image domain.
  441. */
  442. for(k = 0 ; k < ndims && good ; ++k) {
  443. int temp = dsubs [k] + subs [k] ;
  444. good &= (0 <= temp) && (temp < dims [k]) ;
  445. n_idx += temp * strides [k] ;
  446. }
  447. /*
  448. The neighbor should be processed if the following conditions
  449. are met:
  450. 1. The neighbor is within image boundaries.
  451. 2. The neighbor is indeed different from the current node
  452. (the opposite happens when DSUB=(0,0,...,0)).
  453. 3. The neighbor is already in the forest, meaning that it has
  454. already been processed.
  455. */
  456. if (good &&
  457. n_idx != idx &&
  458. r [n_idx] .parent != VL_MSER_VOID_NODE ) {
  459. vl_mser_pix nr_val = 0 ;
  460. vl_uint nr_idx = 0 ;
  461. int hgt = r [ r_idx] .height ;
  462. int n_hgt = r [nr_idx] .height ;
  463. /*
  464. Now we join the two subtrees rooted at
  465. R_IDX = ROOT( IDX)
  466. NR_IDX = ROOT(N_IDX).
  467. Note that R_IDX = ROOT(IDX) might change as we process more
  468. neighbors, so we need keep updating it.
  469. */
  470. r_idx = climb(r, idx) ;
  471. nr_idx = climb(r, n_idx) ;
  472. /*
  473. At this point we have three possibilities:
  474. (A) ROOT(IDX) == ROOT(NR_IDX). In this case the two trees
  475. have already been joined and we do not do anything.
  476. (B) I(ROOT(IDX)) == I(ROOT(NR_IDX)). In this case the pixel
  477. IDX is extending an extremal region with the same
  478. intensity value. Since ROOT(NR_IDX) will NOT be an
  479. extremal region of the full image, ROOT(IDX) can be
  480. safely added as children of ROOT(NR_IDX) if this
  481. reduces the height according to the union rank
  482. heuristic.
  483. (C) I(ROOT(IDX)) > I(ROOT(NR_IDX)). In this case the pixel
  484. IDX is starting a new extremal region. Thus ROOT(NR_IDX)
  485. WILL be an extremal region of the final image and the
  486. only possibility is to add ROOT(NR_IDX) as children of
  487. ROOT(IDX), which becomes parent.
  488. */
  489. if( r_idx != nr_idx ) { /* skip if (A) */
  490. nr_val = im [nr_idx] ;
  491. if( nr_val == val && hgt < n_hgt ) {
  492. /* ROOT(IDX) becomes the child */
  493. r [r_idx] .parent = nr_idx ;
  494. r [r_idx] .shortcut = nr_idx ;
  495. r [nr_idx] .area += r [r_idx] .area ;
  496. r [nr_idx] .height = VL_MAX(n_hgt, hgt+1) ;
  497. joins [njoins++] = r_idx ;
  498. } else {
  499. /* cases ROOT(IDX) becomes the parent */
  500. r [nr_idx] .parent = r_idx ;
  501. r [nr_idx] .shortcut = r_idx ;
  502. r [r_idx] .area += r [nr_idx] .area ;
  503. r [r_idx] .height = VL_MAX(hgt, n_hgt + 1) ;
  504. joins [njoins++] = nr_idx ;
  505. /* count if extremal */
  506. if (nr_val != val) ++ ner ;
  507. } /* check b vs c */
  508. } /* check a vs b or c */
  509. } /* neighbor done */
  510. /* move to next neighbor */
  511. k = 0 ;
  512. while(++ dsubs [k] > 1) {
  513. dsubs [k++] = -1 ;
  514. if(k == ndims) goto done_all_neighbors ;
  515. }
  516. } /* next neighbor */
  517. done_all_neighbors : ;
  518. } /* next pixel */
  519. /* the last root is extremal too */
  520. ++ ner ;
  521. /* save back */
  522. f-> njoins = njoins ;
  523. f-> stats. num_extremal = ner ;
  524. /* -----------------------------------------------------------------
  525. * Extract extremal regions
  526. * -------------------------------------------------------------- */
  527. /*
  528. Extremal regions are extracted and stored into the array ER. The
  529. structure R is also updated so that .SHORTCUT indexes the
  530. corresponding extremal region if any (otherwise it is set to
  531. VOID).
  532. */
  533. /* make room */
  534. if (f-> rer < ner) {
  535. if (er) vl_free (er) ;
  536. f->er = er = (VlMserExtrReg*)vl_malloc (sizeof(VlMserExtrReg) * ner) ;
  537. f->rer = ner ;
  538. } ;
  539. /* save back */
  540. f-> nmer = ner ;
  541. /* count again */
  542. ner = 0 ;
  543. /* scan all regions Xi */
  544. for(i = 0 ; i < (int) nel ; ++i) {
  545. /* pop next node xi */
  546. vl_uint idx = perm [i] ;
  547. vl_mser_pix val = im [idx] ;
  548. vl_uint p_idx = r [idx] .parent ;
  549. vl_mser_pix p_val = im [p_idx] ;
  550. /* is extremal ? */
  551. vl_bool is_extr = (p_val > val) || idx == p_idx ;
  552. if( is_extr ) {
  553. /* if so, add it */
  554. er [ner] .index = idx ;
  555. er [ner] .parent = ner ;
  556. er [ner] .value = im [idx] ;
  557. er [ner] .area = r [idx] .area ;
  558. /* link this region to this extremal region */
  559. r [idx] .shortcut = ner ;
  560. /* increase count */
  561. ++ ner ;
  562. } else {
  563. /* link this region to void */
  564. r [idx] .shortcut = VL_MSER_VOID_NODE ;
  565. }
  566. }
  567. /* -----------------------------------------------------------------
  568. * Link extremal regions in a tree
  569. * -------------------------------------------------------------- */
  570. for(i = 0 ; i < ner ; ++i) {
  571. vl_uint idx = er [i] .index ;
  572. do {
  573. idx = r[idx] .parent ;
  574. } while (r[idx] .shortcut == VL_MSER_VOID_NODE) ;
  575. er[i] .parent = r[idx] .shortcut ;
  576. er[i] .shortcut = i ;
  577. }
  578. /* -----------------------------------------------------------------
  579. * Compute variability of +DELTA branches
  580. * -------------------------------------------------------------- */
  581. /* For each extremal region Xi of value VAL we look for the biggest
  582. * parent that has value not greater than VAL+DELTA. This is dubbed
  583. * `top parent'. */
  584. for(i = 0 ; i < ner ; ++i) {
  585. /* Xj is the current region the region and Xj are the parents */
  586. int top_val = er [i] .value + delta ;
  587. int top = er [i] .shortcut ;
  588. /* examine all parents */
  589. while (1) {
  590. int next = er [top] .parent ;
  591. int next_val = er [next] .value ;
  592. /* Break if:
  593. * - there is no node above the top or
  594. * - the next node is above the top value.
  595. */
  596. if (next == top || next_val > top_val) break ;
  597. /* so next could be the top */
  598. top = next ;
  599. }
  600. /* calculate branch variation */
  601. {
  602. int area = er [i ] .area ;
  603. int area_top = er [top] .area ;
  604. er [i] .variation = (float) (area_top - area) / area ;
  605. er [i] .max_stable = 1 ;
  606. }
  607. /* Optimization: since extremal regions are processed by
  608. * increasing intensity, all next extremal regions being processed
  609. * have value at least equal to the one of Xi. If any of them has
  610. * parent the parent of Xi (this comprises the parent itself), we
  611. * can safely skip most intermediate node along the branch and
  612. * skip directly to the top to start our search. */
  613. {
  614. int parent = er [i] .parent ;
  615. int curr = er [parent] .shortcut ;
  616. er [parent] .shortcut = VL_MAX (top, curr) ;
  617. }
  618. }
  619. /* -----------------------------------------------------------------
  620. * Select maximally stable branches
  621. * -------------------------------------------------------------- */
  622. nmer = ner ;
  623. for(i = 0 ; i < ner ; ++i) {
  624. vl_uint parent = er [i ] .parent ;
  625. vl_mser_pix val = er [i ] .value ;
  626. float var = er [i ] .variation ;
  627. vl_mser_pix p_val = er [parent] .value ;
  628. float p_var = er [parent] .variation ;
  629. vl_uint loser ;
  630. /*
  631. Notice that R_parent = R_{l+1} only if p_val = val + 1. If not,
  632. this and the parent region coincide and there is nothing to do.
  633. */
  634. if(p_val > val + 1) continue ;
  635. /* decide which one to keep and put that in loser */
  636. if(var < p_var) loser = parent ; else loser = i ;
  637. /* make loser NON maximally stable */
  638. if(er [loser] .max_stable) {
  639. -- nmer ;
  640. er [loser] .max_stable = 0 ;
  641. }
  642. }
  643. f-> stats. num_unstable = ner - nmer ;
  644. /* -----------------------------------------------------------------
  645. * Further filtering
  646. * -------------------------------------------------------------- */
  647. /* It is critical for correct duplicate detection to remove regions
  648. * from the bottom (smallest one first). */
  649. {
  650. float max_area = (float) f-> max_area * nel ;
  651. float min_area = (float) f-> min_area * nel ;
  652. float max_var = (float) f-> max_variation ;
  653. float min_div = (float) f-> min_diversity ;
  654. /* scan all extremal regions (intensity value order) */
  655. for(i = ner-1 ; i >= 0L ; --i) {
  656. /* process only maximally stable extremal regions */
  657. if (! er [i] .max_stable) continue ;
  658. if (er [i] .variation >= max_var ) { ++ nbad ; goto remove ; }
  659. if (er [i] .area > max_area) { ++ nbig ; goto remove ; }
  660. if (er [i] .area < min_area) { ++ nsmall ; goto remove ; }
  661. /*
  662. * Remove duplicates
  663. */
  664. if (min_div < 1.0) {
  665. vl_uint parent = er [i] .parent ;
  666. int area, p_area ;
  667. float div ;
  668. /* check all but the root mser */
  669. if((int) parent != i) {
  670. /* search for the maximally stable parent region */
  671. while(! er [parent] .max_stable) {
  672. vl_uint next = er [parent] .parent ;
  673. if(next == parent) break ;
  674. parent = next ;
  675. }
  676. /* Compare with the parent region; if the current and parent
  677. * regions are too similar, keep only the parent. */
  678. area = er [i] .area ;
  679. p_area = er [parent] .area ;
  680. div = (float) (p_area - area) / (float) p_area ;
  681. if (div < min_div) { ++ ndup ; goto remove ; }
  682. } /* remove dups end */
  683. }
  684. continue ;
  685. remove :
  686. er [i] .max_stable = 0 ;
  687. -- nmer ;
  688. } /* check next region */
  689. f-> stats .num_abs_unstable = nbad ;
  690. f-> stats .num_too_big = nbig ;
  691. f-> stats .num_too_small = nsmall ;
  692. f-> stats .num_duplicates = ndup ;
  693. }
  694. /* -----------------------------------------------------------------
  695. * Save the result
  696. * -------------------------------------------------------------- */
  697. /* make room */
  698. if (f-> rmer < nmer) {
  699. if (mer) vl_free (mer) ;
  700. f->mer = mer = (vl_uint*)vl_malloc( sizeof(vl_uint) * nmer) ;
  701. f->rmer = nmer ;
  702. }
  703. /* save back */
  704. f-> nmer = nmer ;
  705. j = 0 ;
  706. for (i = 0 ; i < ner ; ++i) {
  707. if (er [i] .max_stable) mer [j++] = er [i] .index ;
  708. }
  709. }
  710. /** -------------------------------------------------------------------
  711. ** @brief Fit ellipsoids
  712. **
  713. ** @param f MSER filter.
  714. **
  715. ** @sa @ref mser-ell
  716. **/
  717. VL_EXPORT
  718. void
  719. vl_mser_ell_fit (VlMserFilt* f)
  720. {
  721. /* shortcuts */
  722. int nel = f-> nel ;
  723. int dof = f-> dof ;
  724. int *dims = f-> dims ;
  725. int ndims = f-> ndims ;
  726. int *subs = f-> subs ;
  727. int njoins = f-> njoins ;
  728. vl_uint *joins = f-> joins ;
  729. VlMserReg *r = f-> r ;
  730. vl_uint *mer = f-> mer ;
  731. int nmer = f-> nmer ;
  732. vl_mser_acc *acc = f-> acc ;
  733. vl_mser_acc *ell = f-> ell ;
  734. int d, index, i, j ;
  735. /* already fit ? */
  736. if (f->nell == f->nmer) return ;
  737. /* make room */
  738. if (f->rell < f->nmer) {
  739. if (f->ell) vl_free (f->ell) ;
  740. f->ell = (float*)vl_malloc (sizeof(float) * f->nmer * f->dof) ;
  741. f->rell = f-> nmer ;
  742. }
  743. if (f->acc == 0) {
  744. f->acc = (float*)vl_malloc (sizeof(float) * f->nel) ;
  745. }
  746. acc = f-> acc ;
  747. ell = f-> ell ;
  748. /* -----------------------------------------------------------------
  749. * Integrate moments
  750. * -------------------------------------------------------------- */
  751. /* for each dof */
  752. for(d = 0 ; d < f->dof ; ++d) {
  753. /* start from the upper-left pixel (0,0,...,0) */
  754. memset (subs, 0, sizeof(int) * ndims) ;
  755. /* step 1: fill acc pretending that each region has only one pixel */
  756. if(d < ndims) {
  757. /* 1-order ................................................... */
  758. for(index = 0 ; index < nel ; ++ index) {
  759. acc [index] = subs [d] ;
  760. adv(ndims, dims, subs) ;
  761. }
  762. }
  763. else {
  764. /* 2-order ................................................... */
  765. /* map the dof d to a second order moment E[x_i x_j] */
  766. i = d - ndims ;
  767. j = 0 ;
  768. while(i > j) {
  769. i -= j + 1 ;
  770. j ++ ;
  771. }
  772. /* initialize acc with x_i * x_j */
  773. for(index = 0 ; index < nel ; ++ index){
  774. acc [index] = subs [i] * subs [j] ;
  775. adv(ndims, dims, subs) ;
  776. }
  777. }
  778. /* step 2: integrate */
  779. for(i = 0 ; i < njoins ; ++i) {
  780. vl_uint index = joins [i] ;
  781. vl_uint parent = r [index] .parent ;
  782. acc [parent] += acc [index] ;
  783. }
  784. /* step 3: save back to ellpises */
  785. for(i = 0 ; i < nmer ; ++i) {
  786. vl_uint idx = mer [i] ;
  787. ell [d + dof*i] = acc [idx] ;
  788. }
  789. } /* next dof */
  790. /* -----------------------------------------------------------------
  791. * Compute central moments
  792. * -------------------------------------------------------------- */
  793. for(index = 0 ; index < nmer ; ++index) {
  794. float *pt = ell + index * dof ;
  795. vl_uint idx = mer [index] ;
  796. float area = r [idx] .area ;
  797. for(d = 0 ; d < dof ; ++d) {
  798. pt [d] /= area ;
  799. if(d >= ndims) {
  800. /* remove squared mean from moment to get variance */
  801. i = d - ndims ;
  802. j = 0 ;
  803. while(i > j) {
  804. i -= j + 1 ;
  805. j ++ ;
  806. }
  807. pt [d] -= pt [i] * pt [j] ;
  808. }
  809. }
  810. }
  811. /* save back */
  812. f-> nell = nmer ;
  813. }