/ *   
 
   *   P R O J E C T :   N y A R T o o l k i t C S 
 
   *   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
   *   T h i s   w o r k   i s   b a s e d   o n   t h e   o r i g i n a l   A R T o o l K i t   d e v e l o p e d   b y 
 
   *       H i r o k a z u   K a t o 
 
   *       M a r k   B i l l i n g h u r s t 
 
   *       H I T L a b ,   U n i v e r s i t y   o f   W a s h i n g t o n ,   S e a t t l e 
 
   *   h t t p : / / w w w . h i t l . w a s h i n g t o n . e d u / a r t o o l k i t / 
 
   * 
 
   *   T h e   N y A R T o o l k i t C S   i s   J a v a   e d i t i o n   A R T o o l K i t   c l a s s   l i b r a r y . 
 
   *   C o p y r i g h t   ( C ) 2 0 0 8 - 2 0 0 9   R y o   I i z u k a 
 
   * 
 
   *   T h i s   p r o g r a m   i s   f r e e   s o f t w a r e :   y o u   c a n   r e d i s t r i b u t e   i t   a n d / o r   m o d i f y 
 
   *   i t   u n d e r   t h e   t e r m s   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e   a s   p u b l i s h e d   b y 
 
   *   t h e   F r e e   S o f t w a r e   F o u n d a t i o n ,   e i t h e r   v e r s i o n   3   o f   t h e   L i c e n s e ,   o r 
 
   *   ( a t   y o u r   o p t i o n )   a n y   l a t e r   v e r s i o n . 
 
   *   
 
   *   T h i s   p r o g r a m   i s   d i s t r i b u t e d   i n   t h e   h o p e   t h a t   i t   w i l l   b e   u s e f u l , 
 
   *   b u t   W I T H O U T   A N Y   W A R R A N T Y ;   w i t h o u t   e v e n   t h e   i m p l i e d   w a r r a n t y   o f 
 
   *   M E R C H A N T A B I L I T Y   o r   F I T N E S S   F O R   A   P A R T I C U L A R   P U R P O S E .     S e e   t h e 
 
   *   G N U   G e n e r a l   P u b l i c   L i c e n s e   f o r   m o r e   d e t a i l s . 
 
   * 
 
   *   Y o u   s h o u l d   h a v e   r e c e i v e d   a   c o p y   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e 
 
   *   a l o n g   w i t h   t h i s   p r o g r a m .     I f   n o t ,   s e e   < h t t p : / / w w w . g n u . o r g / l i c e n s e s / > . 
 
   *   
 
   *   F o r   f u r t h e r   i n f o r m a t i o n   p l e a s e   c o n t a c t . 
 
   * 	 h t t p : / / n y a t l a . j p / n y a t o o l k i t / 
 
   * 	 < a i r m a i l ( a t ) e b o n y . p l a l a . o r . j p >   o r   < n y a t l a ( a t ) n y a t l a . j p > 
 
   *   
 
   * / 
 
 u s i n g   S y s t e m . D i a g n o s t i c s ; 
 
 n a m e s p a c e   j p . n y a t l a . n y a r t o o l k i t . c s . c o r e 
 
 { 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
         / * * 
 
           *   S0n0000o00{ @ l i n k   N y A R L a b e l i n g _ R l e } 0000(uD0_0wb_iQhVg0Y00
 
           *   iQW0_0wb_00]000000pe{ @ l i n k   # o n S q u a r e D e t e c t } x0wW0~0Y00
 
           *   }b000g0]000000pe{ @ l i n k   # o n S q u a r e D e t e c t } 0[ňY00_L0B00~0Y00
 
           * / 
 
         p u b l i c   c l a s s   N y A R S q u a r e C o n t o u r D e t e c t o r _ R l e   :   N y A R S q u a r e C o n t o u r D e t e c t o r 
 
         { 
 
                 / * *   l a b e l _ s t a c k k0000_n0P}g0ĄMzY00000* / 
 
                 p r o t e c t e d   c l a s s   L a b e l i n g   :   N y A R L a b e l i n g _ R l e 
 
                 { 
 
                         p u b l i c   N y A R R l e L a b e l F r a g m e n t I n f o P t r S t a c k   l a b e l _ s t a c k ; 
 
                         i n t   _ r i g h t ; 
 
                         i n t   _ b o t t o m ; 
 
 
 
 
 
                         p u b l i c   L a b e l i n g ( i n t   i _ w i d t h ,   i n t   i _ h e i g h t ) : b a s e ( i _ w i d t h ,   i _ h e i g h t ) 
 
                         { 
 
                                 l o n g   t   =   ( l o n g ) i _ w i d t h   *   i _ h e i g h t   *   2 0 4 8   /   ( 3 2 0   *   2 4 0 )   +   3 2 ; / / f u l l   H D   s u p p o r t 
 
                                 t h i s . l a b e l _ s t a c k   =   n e w   N y A R R l e L a b e l F r a g m e n t I n f o P t r S t a c k ( ( i n t ) t ) ; / / iQSj0 g'Y000pe
 
                                 t h i s . _ b o t t o m   =   i _ h e i g h t   -   1 ; 
 
                                 t h i s . _ r i g h t   =   i _ w i d t h   -   1 ; 
 
                                 r e t u r n ; 
 
                         } 
 
                         p u b l i c   o v e r r i d e   b o o l   l a b e l i n g ( I N y A R G r a y s c a l e R a s t e r   i _ r a s t e r ,   N y A R I n t R e c t   i _ a r e a ,   i n t   i _ t h ) 
 
                         { 
 
                                 / / MRRgS
 
                                 t h i s . l a b e l _ s t a c k . c l e a r ( ) ; 
 
                                 / / 000n0iQ
 
                                 b o o l   r e t   =   b a s e . l a b e l i n g ( i _ r a s t e r ,   i _ a r e a ,   i _ t h ) ; 
 
                                 / / 000
 
                                 t h i s . l a b e l _ s t a c k . s o r t B y A r e a ( ) ; 
 
                                 r e t u r n   r e t ; 
 
                         } 
 
                         p u b l i c   o v e r r i d e   b o o l   l a b e l i n g ( I N y A R G r a y s c a l e R a s t e r   i _ r a s t e r ,   i n t   i _ t h ) 
 
                         { 
 
                                 / / MRRgS
 
                                 t h i s . l a b e l _ s t a c k . c l e a r ( ) ; 
 
                                 / / 000n0iQ
 
                                 b o o l   r e t   =   b a s e . l a b e l i n g ( i _ r a s t e r ,   i _ t h ) ; 
 
                                 / / 000
 
                                 t h i s . l a b e l _ s t a c k . s o r t B y A r e a ( ) ; 
 
                                 r e t u r n   r e t ; 
 
                         } 
 
 
 
                         p r o t e c t e d   o v e r r i d e   v o i d   o n L a b e l F o u n d ( N y A R R l e L a b e l F r a g m e n t I n f o   i _ l a b e l ) 
 
                         { 
 
                                 / /   0000WL0;ubn0gk0cW0f0D00p0dY
 
                                 i f   ( i _ l a b e l . c l i p _ l   = =   0   | |   i _ l a b e l . c l i p _ r   = =   t h i s . _ r i g h t ) 
 
                                 { 
 
                                         r e t u r n ; 
 
                                 } 
 
                                 i f   ( i _ l a b e l . c l i p _ t   = =   0   | |   i _ l a b e l . c l i p _ b   = =   t h i s . _ b o t t o m ) 
 
                                 { 
 
                                         r e t u r n ; 
 
                                 } 
 
                                 t h i s . l a b e l _ s t a c k . p u s h ( i _ l a b e l ) ; 
 
                         } 
 
                 } 
 
 
 
                 p r o t e c t e d   L a b e l i n g   _ l a b e l i n g ; 
 
                 p r i v a t e   r e a d o n l y   N y A R L a b e l O v e r l a p C h e c k e r < N y A R R l e L a b e l F r a g m e n t I n f o >   _ o v e r l a p _ c h e c k e r   =   n e w   N y A R L a b e l O v e r l a p C h e c k e r < N y A R R l e L a b e l F r a g m e n t I n f o > ( 3 2 ) ; 
 
                 p r i v a t e   N y A R C o n t o u r P i c k u p   _ c p i c k u p   =   n e w   N y A R C o n t o u r P i c k u p ( ) ; 
 
                 p r i v a t e   r e a d o n l y   N y A R C o o r d 2 S q u a r e V e r t e x I n d e x e s   _ c o o r d 2 v e r t e x   =   n e w   N y A R C o o r d 2 S q u a r e V e r t e x I n d e x e s ( ) ; 
 
                 p r i v a t e   r e a d o n l y   N y A R I n t C o o r d i n a t e s   _ c o o r d ; 
 
                 / * * 
 
                   *   0000000g0Y00
 
                   *   eQR;uPn00000c[W0f000000000ubW0~0Y00
 
                   *   @ p a r a m   i _ s i z e 
 
                   *   eQR;uPn0000
 
                   * / 
 
                 p u b l i c   N y A R S q u a r e C o n t o u r D e t e c t o r _ R l e ( N y A R I n t S i z e   i _ s i z e ) 
 
                 { 
 
                         t h i s . s e t u p I m a g e D r i v e r ( i _ s i z e ) ; 
 
                         / / 00000n00000c[W0_0D0h0M0o0s e t A r e a R a n g e 0Oc0f0m00
 
                         t h i s . _ c o o r d   =   n e w   N y A R I n t C o o r d i n a t e s ( ( i _ s i z e . w   +   i _ s i z e . h )   *   2 ) ; 
 
                         r e t u r n ; 
 
                 } 
 
                 / * * 
 
                   *   ;uPQt000000n0R0fH0pe0R0fH004XTo00S0n0pe0
NfM0Y00S0h00
 
                   *   @ p a r a m   i _ s i z e 
 
                   *   @ 
 
                   * / 
 
                 p r o t e c t e d   v o i d   s e t u p I m a g e D r i v e r ( N y A R I n t S i z e   i _ s i z e ) 
 
                 { 
 
                         / / yr'`x
 
                         D e b u g . A s s e r t ( N y A R L a b e l i n g _ R l e . _ s f _ l a b e l _ a r r a y _ s a f e _ r e f e r e n c e ) ; 
 
                         t h i s . _ l a b e l i n g   =   n e w   L a b e l i n g ( i _ s i z e . w ,   i _ s i z e . h ) ; 
 
                         t h i s . _ c p i c k u p   =   n e w   N y A R C o n t o u r P i c k u p ( ) ; 
 
                 } 
 
 
 
                 p r i v a t e   r e a d o n l y   i n t [ ]   _ _ d e t e c t M a r k e r _ m k v e r t e x   =   n e w   i n t [ 4 ] ; 
 
                 / * * 
 
                   *   S0n0peo00000K00wb_0iQW0f00]000000pe{ @ l i n k   # o n S q u a r e D e t e c t } g0wW0~0Y00
 
                   *   @ p a r a m   i _ r a s t e r 
 
                   *   iQCQn0000;uP
 
                   *   eQRg0M00000n0;u }b__o00{ @ l i n k   N y A R L a b e l i n g _ R l e # l a b e l i n g ( I N y A R R a s t e r ,   i n t ) } h0TX0g0Y00
 
                   *   @ p a r a m   i _ a r e a 
 
                   *   iQY00{V0iQCQn0000n0QtPg0B00_L0B00~0Y00
 
                   *   @ p a r a m   i _ t h 
 
                   *   000h0$R[Y00weE\$P
 
                   *   @ 
 
                   * / 
 
                 p u b l i c   v o i d   d e t e c t M a r k e r ( I N y A R G r a y s c a l e R a s t e r   i _ r a s t e r ,   N y A R I n t R e c t   i _ a r e a ,   i n t   i _ t h ,   N y A R S q u a r e C o n t o u r D e t e c t o r . C b H a n d l e r   i _ c b ) 
 
                 { 
 
                         D e b u g . A s s e r t ( i _ a r e a . w   *   i _ a r e a . h   >   0 ) ; 
 
 
 
                         N y A R R l e L a b e l F r a g m e n t I n f o P t r S t a c k   f l a g m e n t   =   t h i s . _ l a b e l i n g . l a b e l _ s t a c k ; 
 
                         N y A R L a b e l O v e r l a p C h e c k e r < N y A R R l e L a b e l F r a g m e n t I n f o >   o v e r l a p   =   t h i s . _ o v e r l a p _ c h e c k e r ; 
 
                         / / 000n0ub000j00S0S0~0g0
 
                         i f   ( ! t h i s . _ l a b e l i n g . l a b e l i n g ( i _ r a s t e r ,   i _ a r e a ,   i _ t h ) ) 
 
                         { 
 
                                 r e t u r n ; 
 
                         } 
 
                         / /   000peL00 j00S0S0~0g0
 
                         i n t   l a b e l _ n u m   =   f l a g m e n t . g e t L e n g t h ( ) ; 
 
                         i f   ( l a b e l _ n u m   <   1 ) 
 
                         { 
 
                                 r e t u r n ; 
 
                         } 
 
 
 
                         / / 0000000S_
 
                         N y A R R l e L a b e l F r a g m e n t I n f o [ ]   l a b e l s   =   f l a g m e n t . g e t A r r a y ( ) ; 
 
 
 
                         N y A R I n t C o o r d i n a t e s   c o o r d   =   t h i s . _ c o o r d ; 
 
                         i n t [ ]   m k v e r t e x   =   t h i s . _ _ d e t e c t M a r k e r _ m k v e r t e x ; 
 
 
 
 
 
                         / / ͑j000000n0 g'Ype0-[
 
                         o v e r l a p . s e t M a x L a b e l s ( l a b e l _ n u m ) ; 
 
 
 
                         f o r   ( i n t   i   =   0 ;   i   <   l a b e l _ n u m ;   i + + ) 
 
                         { 
 
                                 N y A R R l e L a b e l F r a g m e n t I n f o   l a b e l _ p t   =   l a b e l s [ i ] ; 
 
                                 / /   ek0iQU00_0wb_h0n0͑j000x
 
                                 i f   ( ! o v e r l a p . c h e c k ( l a b e l _ p t ) ) 
 
                                 { 
 
                                         / /   ͑j0c0f0D000F0`00
 
                                         c o n t i n u e ; 
 
                                 } 
 
 
 
                                 / / *퐒0S_
 
                                 i f   ( ! t h i s . _ c p i c k u p . g e t C o n t o u r ( i _ r a s t e r ,   i _ a r e a ,   i _ t h ,   l a b e l _ p t . e n t r y _ x ,   l a b e l _ p t . c l i p _ t ,   c o o r d ) ) 
 
                                 { 
 
                                         c o n t i n u e ; 
 
                                 } 
 
                                 i n t   l a b e l _ a r e a   =   l a b e l _ p t . a r e a ; 
 
                                 / / *}00000W0f00wb_K0i0F0K00$R[0wb_j00p0m k v e r t e x k0S_
 
                                 i f   ( ! t h i s . _ c o o r d 2 v e r t e x . g e t V e r t e x I n d e x e s ( c o o r d ,   l a b e l _ a r e a ,   m k v e r t e x ) ) 
 
                                 { 
 
                                         / /   pn0S_L0Qegj0K0c0_0
 
                                         c o n t i n u e ; 
 
                                 } 
 
                                 / / wb_0zvW0_0S0h00000000peg0w
 
                                 i _ c b . d e t e c t M a r k e r C a l l b a c k ( c o o r d ,   m k v e r t e x ) ; 
 
 
 
                                 / /   iQnn0wb_n0^\W0_00000͑j000000k0RY000
 
                                 o v e r l a p . p u s h ( l a b e l _ p t ) ; 
 
 
 
                         } 
 
                         r e t u r n ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00000K00wb_0iQW0f00]000000pe{ @ l i n k   # o n S q u a r e D e t e c t } g0wW0~0Y00
 
                   *   A R T o o l K i t n0a r D e t e c t M a r k e r 2 0Wk0W0f0D0~0Y00
 
                   *   @ p a r a m   i _ r a s t e r 
 
                   *   iQCQn0000;uP
 
                   *   eQRg0M00000n0;u }b__o00{ @ l i n k   N y A R L a b e l i n g _ R l e # l a b e l i n g ( N y A R G r a y s c a l e R a s t e r ,   i n t ) } h0TX0g0Y00
 
                   *   @ p a r a m   i _ t h 
 
                   *   ;u }n0N$P$R[weE\$Pg0Y00S0n0$Po0000000h00*}Bfk0O00~0Y00
 
                   * / 
 
                 p u b l i c   v o i d   d e t e c t M a r k e r ( I N y A R G r a y s c a l e R a s t e r   i _ r a s t e r ,   i n t   i _ t h ,   N y A R S q u a r e C o n t o u r D e t e c t o r . C b H a n d l e r   i _ c b ) 
 
                 { 
 
                         N y A R R l e L a b e l F r a g m e n t I n f o P t r S t a c k   f l a g m e n t   =   t h i s . _ l a b e l i n g . l a b e l _ s t a c k ; 
 
                         N y A R L a b e l O v e r l a p C h e c k e r < N y A R R l e L a b e l F r a g m e n t I n f o >   o v e r l a p   =   t h i s . _ o v e r l a p _ c h e c k e r ; 
 
 
 
                         f l a g m e n t . c l e a r ( ) ; 
 
                         / / 000n0ub000j00S0S0~0g0
 
                         i f   ( ! t h i s . _ l a b e l i n g . l a b e l i n g ( i _ r a s t e r ,   i _ t h ) ) 
 
                         { 
 
                                 r e t u r n ; 
 
                         } 
 
                         i n t   l a b e l _ n u m   =   f l a g m e n t . g e t L e n g t h ( ) ; 
 
                         / /   000peL00 j00S0S0~0g0
 
                         i f   ( l a b e l _ n u m   <   1 ) 
 
                         { 
 
                                 r e t u r n ; 
 
                         } 
 
                         / / 0000000W0f0J0O0
 
                         f l a g m e n t . s o r t B y A r e a ( ) ; 
 
                         / / 0000000S_
 
                         N y A R R l e L a b e l F r a g m e n t I n f o [ ]   l a b e l s   =   f l a g m e n t . g e t A r r a y ( ) ; 
 
 
 
                         N y A R I n t C o o r d i n a t e s   c o o r d   =   t h i s . _ c o o r d ; 
 
                         i n t [ ]   m k v e r t e x   =   t h i s . _ _ d e t e c t M a r k e r _ m k v e r t e x ; 
 
 
 
 
 
                         / / ͑j000000n0 g'Ype0-[
 
                         o v e r l a p . s e t M a x L a b e l s ( l a b e l _ n u m ) ; 
 
 
 
                         f o r   ( i n t   i   =   0 ;   i   <   l a b e l _ n u m ;   i + + ) 
 
                         { 
 
                                 N y A R R l e L a b e l F r a g m e n t I n f o   l a b e l _ p t   =   l a b e l s [ i ] ; 
 
                                 i n t   l a b e l _ a r e a   =   l a b e l _ p t . a r e a ; 
 
 
 
                                 / /   ek0iQU00_0wb_h0n0͑j000x
 
                                 i f   ( ! o v e r l a p . c h e c k ( l a b e l _ p t ) ) 
 
                                 { 
 
                                         / /   ͑j0c0f0D000F0`00
 
                                         c o n t i n u e ; 
 
                                 } 
 
                                 / / *퐒0S_
 
                                 i f   ( ! t h i s . _ c p i c k u p . g e t C o n t o u r ( i _ r a s t e r ,   i _ t h ,   l a b e l _ p t . e n t r y _ x ,   l a b e l _ p t . c l i p _ t ,   c o o r d ) ) 
 
                                 { 
 
                                         c o n t i n u e ; 
 
                                 } 
 
                                 / / *}00000W0f00wb_K0i0F0K00$R[0wb_j00p0m k v e r t e x k0S_
 
                                 i f   ( ! t h i s . _ c o o r d 2 v e r t e x . g e t V e r t e x I n d e x e s ( c o o r d ,   l a b e l _ a r e a ,   m k v e r t e x ) ) 
 
                                 { 
 
                                         / /   pn0S_L0Qegj0K0c0_0
 
                                         c o n t i n u e ; 
 
                                 } 
 
                                 / / wb_0zvW0_0S0h00000000peg0w
 
                                 i _ c b . d e t e c t M a r k e r C a l l b a c k ( c o o r d ,   m k v e r t e x ) ; 
 
 
 
                                 / /   iQnn0wb_n0^\W0_00000͑j000000k0RY000
 
                                 o v e r l a p . p u s h ( l a b e l _ p t ) ; 
 
 
 
                         } 
 
                         r e t u r n ; 
 
                 } 
 
         } 
 
 
 
 } 