Za pretvorbo paraboliˇ cne slike vpiˇsemo v lupino Matlaba:
unroll(’krogelna_slika.jpg’,’k’,g,d,focus);, kjer je g polmer
zr-cala, d razdalja med srediˇsˇ cem krogelnega zrcala in kamero ter focus ˇ zariˇsˇ cna
razdalja kamere.
4.5. OBJEKTIV S KROGELNIM ZRCALOM 25
Slika 4.5: Fotografija, posneta s krogelnim zrcalom [4].
Slika 4.6: Razˇ sirjena fotografija 4.5 s programom unroll [4].
Poglavje 5
Sklepne ugotovitve
Za inverzno transformacijo so paralelni algoritmi smiselni. Cas izvajanja ˇ na CUDA pri 96 procesnih enotah (Nvidia Optimus 540M) je za 10-krat hitrejˇsi, kot pa algoritem, ki deluje v Matlabu na istem raˇ cunalniku, Core i7.
Konkretno, za transformacijo brez upoˇstevanja zrcala je CUDA program na sliki Marsket0.jpg velikosti 2448x2448 (obrezana slika na podlagi Hughove transformacije), potreboval 0,19 sekund, proogram s programsko zanko 0,62 sekunde, zgoraj opisan Matlabov program pa 2,27 sekunde 5.1. Matlabovim programom se priˇsteje ˇse 10 % k ˇ casu izvajanja, ko se upoˇsteva oblika zrcala.
Z uporabo grafiˇ cnih procesnih enot za preslikavo lahko preslikavo v valjˇ cno panoramo ob ustrezni strojni podpori izvajamo tudi v realnem ˇ casu, kar programsko pri visoki loˇ cljivosti ˇse ni mogoˇ ce.
Realizacija ˇ cas (s) Nvidia Optimus 540M 0,19 Programska zanka 0,62
Matlab 2,27
Slika 5.1: Realizacije in ˇ casi izvajanja.
27
Slike
2.1 Fotografija robota Π s krogelnim zrcalom [4]. . . . 3 2.2 Kamera 0-360 ◦ [6]. . . . 4 2.3 “Fisheye” kamera [7]. . . . 5 2.4 Pretvorba hiperboliˇ cne v valjˇ cno sliko [7]. . . . 6 2.5 Pretvorba paraboliˇ cne v valjˇ cno sliko [7]. . . . 8 2.6 Shema kamere s krogelnim zrcalom [9]. . . . 10 3.1 Shema za pretvorbo vsesmerne fotografije v panoramo . . . . . 14 3.2 Interpolacija z bilinearnim filtrom [1]. . . . 15 3.3 Detekcija krogov s Houghovo transformacijo. . . . 16 4.1 Fotografija, posneta s 0-360 ◦ zrcalom [7]. . . . 21 4.2 Razˇsirjena fotografija 4.1 s programom unroll [7]. . . . . 21 4.3 Fotografija, posneta s hiperboliˇ cnim zrcalom [11]. . . . 22 4.4 Razˇsirjena fotografija 4.3 s programom unroll [11]. . . . 22 4.5 Fotografija, posneta s krogelnim zrcalom [4]. . . . 25 4.6 Razˇsirjena fotografija 4.5 s programom unroll [4]. . . . . 25 5.1 Realizacije in ˇ casi izvajanja. . . . 27
29
Literatura
[1] Y. Ba¸stanlar, “Parameter extraction and image enhancement for cata-dioptric omnidirectional cameras,” Dostopno na: http://www.ii.metu.
edu.tr/ ∼ yalinb/publications/Bastanlar-Thesis.pdf, 2012.
[2] Y. Yagi, “Omnidirectional sensing and its applications.” v IEICE Tran-sactions on Information and Systems, jul 1995, stran 568 – 579.
[3] A. Simon, R. Smith, in R. Pawlicki, “Omnistereo for panoramic vir-tual environment display systems,” v Virtual Reality, 2004. Proceedings.
IEEE, marec 2004, stran 67 – 279.
[4] “Pi robot,” Dostopno na: http://www.pirobot.org/blog/0001/, 2012.
[5] T. Svoboda, “Central panoramic cameras: Geometry and design,” 1997.
[6] “Some omnidirectional shots durin flying,” Dostopno na:
http://wwwlasmea.univ-bpclermont.fr/Personnel/Maxime.Lhuillier/
OmniPara.html, 2012.
[7] A. Torii in R. Klette, “Panoramic and 3d computer vision,” v ArtsIT, LNICST, 2012, stran 9 – 16.
[8] H. Bakstein in T. Pajdla, “Panoramic mosaicing with a 180 deg; fi-eld of view lens,” v Omnidirectional Vision, 2002. Proceedings. Third Workshop on, 2002, stran 60 – 67.
31
[9] A. Ohte, O. Tsuzuki, in K. Mori, “A practical spherical mirror omni-directional camera,” v Robotic Sensors: Robotic and Sensor Enviro-nments, 2005. International Workshop on, 30 2005-oct. 1 2005, stran 8 – 13.
[10] D. Kerbyson in T. Atherton, “Circle detection using hough transform filters,” v Image Processing and its Applications, 1995., Fifth Internati-onal Conference on, jul 1995, stran 370 –374.
[11] “Catadioptric cameras for 360 degree imaging,” Dostopno na: http:
//www1.cs.columbia.edu/CAVE/projects/cat cam 360/, 2012.
Poglavje 6 Priloga
Priloˇ zeni so programi, ki smo jih razvili za potrebe te diplomske naloge. Prvi je program, napisan za knjiˇ znico CUDA, opisan v poglavju 3.
Priloga k 3.1:
33
Program unroll cuda
Program poˇ zenemo kot: unroll vhodna_datoteka.jpg izhodna_datoteka.jpg.
Pri tem je vhodna_datoteka.jpg vhodna datoteka tipa jpg in izhodna_datoteka.jpg izhodna datoteka tipa jpg.
Program prebere vhodno vsesmerno sliko iz datoteke, s Hughovo trans-formacijo poiˇ sˇ ce center in izpiˇ se panoramsko sliko vhodne slike v izhodno datoteko.
Razdeljen je v tri datoteke:
unroll.cpp
Glavni program s funkcijo main():
1 #i n c l u d e ” u n r o l l k e r n e l s . h ” 2 #i n c l u d e <o p e n c v / c v . h>
3 #i n c l u d e <o p e n c v / h i g h g u i . h>
4 #i n c l u d e <i o m a n i p>
5 #i n c l u d e <i o s t r e a m>
6 #i n c l u d e <math . h>
7 #i n c l u d e <s y s / t i m e b . h>
8 #i n c l u d e <t i m e . h>
9 10
11 #d e f i n e DEBUG TIME
12
13 i n t n o t E n t i r e l y (i n t x , i n t y , i n t r , i n t w i d t h , i n t h e i g h t ) { 14 i f ( ( x−r<0) | | ( x+r>w i d t h ) | | ( y−r<0) | | ( y+r>h e i g h t ) ) r e t u r n 1 ; 15 e l s e r e t u r n 0 ;
16 }
17
18 i n t main (i n t a r g c , c h a r∗∗ a r g v )
19 {
20 i f ( a r g c < 3 )
21 {
22 s t d : : c o u t << ” U p o r a b i : ”<< a r g v [ 0 ] << ” <i n p u t>” <<” <o u t p u t>” <<
23 << s t d : : e n d l ;
24 e x i t ( 1 ) ;
25 }
26
35
27 I p l I m a g e∗ i n p u t i m a g e = NULL ;
28 i n p u t i m a g e = c v L o a d I m a g e ( a r g v [ 1 ] , CV LOAD IMAGE UNCHANGED ) ; 29 i f( ! i n p u t i m a g e )
30 {
31 s t d : : c o u t << ”ERROR: F a i l e d t o l o a d i n p u t i m a g e ” << s t d : : e n d l ;
32 r e t u r n −1;
33 }
34
35 I p l I m a g e∗ g r a y = c v C r e a t e I m a g e ( c v G e t S i z e ( i n p u t i m a g e ) , 8 , 1 ) ; 36 CvMemStorage∗ s t o r a g e = c v C r e a t e M e m S t o r a g e ( 0 ) ;
37 c v C v t C o l o r ( i n p u t i m a g e , g r a y , CV BGR2GRAY ) ; 38 c v S m o o t h ( g r a y , g r a y , CV GAUSSIAN , 9 , 9 ) ; 39 CvSeq∗ c i r c l e s = c v H o u g h C i r c l e s ( g r a y , s t o r a g e ,
40 CV HOUGH GRADIENT, 2 , g r a y−>h e i g h t / 8 ,
41 2 0 0 , 1 0 0 , g r a y−>h e i g h t / 8 , ( g r a y−>w i d t h>
42 g r a y−>h e i g h t ?
43 g r a y−>h e i g h t :
44 g r a y−>w i d t h ) ) ;
45
46 i n t w i d t h = i n p u t i m a g e−>w i d t h ; 47 i n t h e i g h t = i n p u t i m a g e−>h e i g h t ; 48
49 // u g o t o v i n a j v e c j i k r o g
50 i n t maxx =0; i n t maxy =0; i n t maxr =0; i n t i ;
51
52 f o r ( i = 0 ; i < c i r c l e s−>t o t a l ; i ++)
53 {
54 f l o a t∗ p = (f l o a t∗) c v G e t S e q E l e m ( c i r c l e s , i ) ; 55
56 i n t x=cvRound ( p [ 0 ] ) ; i n t y=cvRound ( p [ 1 ] ) ; i n t r=cvRound ( p [ 2 ] ) ; 57
58 i f ( r>maxr ) {
59 maxx=x ; maxy=y ; maxr=r ;
60 }
61
62 /∗ c v C i r c l e ( s i m g , c v P o i n t ( cvRound ( p [ 0 ] ) , cvRound ( p [ 1 ] ) ) ,
63 3 , CV RGB ( 0 , 2 5 5 , 0 ) , −1, 8 , 0 ) ;
64 c v C i r c l e ( s i m g , c v P o i n t ( cvRound ( p [ 0 ] ) , cvRound ( p [ 1 ] ) ) ,
65 cvRound ( p [ 2 ] ) , CV RGB ( 2 5 5 , 0 , 0 ) , 3 , 8 , 0 ) ;
66 ∗/
67 }
68
69 // Ce k r o g n i v c e l o t i na s l i k i , v z e m i k a r g e o m e t r i j s k o s r e d i s c e s l i k e . . . 70 i f ( n o t E n t i r e l y ( maxx , maxy , maxr , g r a y−>w i d t h , g r a y−>h e i g h t ) ) {
71 maxx=cvRound ( g r a y−>w i d t h / 2 ) ; 72 maxy=cvRound ( g r a y−>h e i g h t / 2 ) ;
73 maxr=( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ?
74 cvRound ( g r a y−>h e i g h t / 2 ) : cvRound ( g r a y−>w i d t h / 2 ) ) ;
75 }
76
77 // Ce j e k r o g m a n j s i od 80% n a j m a n j s e z u n a n j e d i m e n z i j e s l i k e , 78 // v z e m i g e o m e t r i s k o s r e d i s c e s l i k e
79
80 i f ( maxr<0 . 8∗( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ?
81 cvRound ( g r a y−>h e i g h t / 2 ) : cvRound ( g r a y−>w i d t h / 2 ) ) ){
82 maxx=cvRound ( g r a y−>w i d t h / 2 ) ; 83 maxy=cvRound ( g r a y−>h e i g h t / 2 ) ;
84 maxr=( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ?
85 cvRound ( g r a y−>h e i g h t / 2 ) : cvRound ( g r a y−>w i d t h / 2 ) ) ;
86 }
87
88 i n t r =0;
89 i n t n e w h e i g h t=r ; 90
37
91 i f ( h e i g h t > 2 ∗ maxy ) n e w h e i g h t = maxy ; 92 e l s e n e w h e i g h t = h e i g h t−maxy ;
93
94 r=maxr−(maxr % 8 ) ;
95 n e w h e i g h t=r +(16−r % 1 6 ) ;
96 i n t n e w w i d t h= CUDART PI F∗r +(16−(i n t) ( CUDART PI F∗r ) % 1 6 ) ; 97
98 p r i n t f ( ”%d %d %d\n ” , n e w h e i g h t , n e w w i d t h , r ) ; 99
100 c v S e t I m a g e R O I ( i n p u t i m a g e , c v R e c t ( maxx−r , maxy−r , 2∗r , 2∗r ) ) ; 101 I p l I m a g e∗ img = c v C r e a t e I m a g e ( c v S i z e ( 2∗r , 2∗r ) , i n p u t i m a g e−>d e p t h ,
102 i n p u t i m a g e−>n C h a n n e l s ) ;
103 c v C o p y ( i n p u t i m a g e , img , NULL ) ; 104 c v R e s e t I m a g e R O I ( i n p u t i m a g e ) ;
105 /∗
106 cvNamedWindow ( ” c i r c l e s ” , 1 ) ; 107 c v S h o w I m a g e ( ” c i r c l e s ” , img ) ;
108 c v S a v e I m a g e ( ” c i r c l e o u t p u t . j p g ” , img ) ;
109 cv Wai tKe y ( 0 ) ;
110 ∗/
111
112 i n t b p p = i n p u t i m a g e−>n C h a n n e l s ; 113
114 # i f d e f DEBUG
115 s t d : : c o u t << ”>> Width : ”<< w i d t h << s t d : : e n d l <<
116 ”>> H e i g h t : ” << h e i g h t << s t d : : e n d l <<
117 ”>>Bpp : ”<< b p p << s t d : : e n d l ; 118
119 s t d : : c o u t << s t d : : e n d l << ”>>> D e b u g g i n g O r i g i n a l d a t a : ”<< s t d : : e n d l ; 120 f o r (i n t i =0; i < w i d t h∗h e i g h t∗b p p ; i+=b p p )
121 {
122 i f ( ! ( i % ( w i d t h∗b p p ) ) ) 123 s t d : : c o u t << s t d : : e n d l ; 124
125 s t d : : c o u t << s t d : : d e c << ”R : ”<< (i n t) i n p u t i m a g e−>i m a g e D a t a [ i ] <<
126 ” G: ” << (i n t) i n p u t i m a g e−>i m a g e D a t a [ i +1] <<
127 ” B : ” << (i n t) i n p u t i m a g e−>i m a g e D a t a [ i +2] << ” ” ;
128 }
129 s t d : : c o u t << s t d : : e n d l << s t d : : e n d l ; 130 #e n d i f
131
132 f l o a t∗ c p u i m a g e = new f l o a t[ w i d t h ∗ h e i g h t ∗ 4 ] ; 133 i f ( ! c p u i m a g e )
134 {
135 s t d : : c o u t << ”ERROR: F a i l e d t o a l l o c a t e memory ”<< s t d : : e n d l ;
136 r e t u r n −1;
137 }
138
139 f o r (i n t i = 0 ; i < 2∗r ∗ 2∗r ; i ++)
140 {
141 c p u i m a g e [ i ∗ 4 + 0 ] = (u n s i g n e d c h a r) img−>i m a g e D a t a [ i ∗ b p p + 0 ] / 2 5 5 . f ; 142 c p u i m a g e [ i ∗ 4 + 1 ] = (u n s i g n e d c h a r) img−>i m a g e D a t a [ i ∗ b p p + 1 ] / 2 5 5 . f ; 143 c p u i m a g e [ i ∗ 4 + 2 ] = (u n s i g n e d c h a r) img−>i m a g e D a t a [ i ∗ b p p + 2 ] / 2 5 5 . f ;
144 }
145
146 f l o a t∗ c p u o u t i m a g e = new f l o a t[ n e w w i d t h ∗ n e w h e i g h t ∗ 4 ] ; 147 i f ( ! c p u o u t i m a g e )
148 {
149 s t d : : c o u t << ”ERROR: F a i l e d t o a l l o c a t e memory ”<< s t d : : e n d l ;
150 r e t u r n −1;
151 }
152
153 # i f d e f DEBUG TIME
154 // S t a r t c l o c k
155 s t r u c t t i m e b s t a r t t i m e s t ; 156 f t i m e (& s t a r t t i m e s t ) ; 157 #e n d i f
158
159 f l o a t∗ g p u i m a g e = NULL ;
160 c u d a E r r o r t c u d a e r r = c u d a M a l l o c ( (v o i d ∗∗)(& g p u i m a g e ) ,
161 ( 2∗r ∗ 2∗r ∗ 4 ) ∗ s i z e o f(f l o a t) ) ;
162 i f ( c u d a e r r != c u d a S u c c e s s )
163 {
164 s t d : : c o u t << ”ERROR: F a i l e d c u d a M a l l o c ”<< s t d : : e n d l ;
165 r e t u r n −1;
166 }
167
168 c u d a e r r = cudaMemcpy ( g p u i m a g e , c p u i m a g e , ( 2∗r ∗ 2∗r ∗ 4 ) ∗ s i z e o f(f l o a t) ,
169 c u d a M e m c p y H o s t T o D e v i c e ) ;
170 i f ( c u d a e r r != c u d a S u c c e s s )
171 {
172 s t d : : c o u t << ”ERROR: F a i l e d cudaMemcpy ” << s t d : : e n d l ;
173 r e t u r n −1;
174 }
175
176 f l o a t∗ g p u o u t i m a g e = NULL ;
177 c u d a E r r o r t c u d a e r r 1 = c u d a M a l l o c ( (v o i d ∗ ∗)
178 (& g p u o u t i m a g e ) ,
179 ( n e w w i d t h ∗ n e w h e i g h t ∗ 4 )
180 ∗ s i z e o f(f l o a t) ) ;
181 i f ( c u d a e r r 1 != c u d a S u c c e s s )
182 {
183 s t d : : c o u t << ”ERROR: F a i l e d c u d a M a l l o c ”<< s t d : : e n d l ;
184 r e t u r n −1;
185 }
186
187 dim3 b l o c k ( 1 6 , 1 6 ) ;
188 dim3 g r i d ( (i n t) c e i l (d o u b l e( ( n e w w i d t h ∗ n e w h e i g h t ) / 2 5 6 . 0 ) ) ) ; 189
190 c u d a t r a n s f o r m ( g p u i m a g e , g p u o u t i m a g e , 2∗r , 2∗r , 191 n e w w i d t h , n e w h e i g h t , g r i d , b l o c k ) ; 192
193 cudaMemcpy ( c p u o u t i m a g e , g p u o u t i m a g e , ( n e w w i d t h ∗ n e w h e i g h t ∗ 4 ) 194 ∗ s i z e o f(f l o a t) , c u d a M e m c p y D e v i c e T o H o s t ) ;
195 i f ( c u d a e r r != c u d a S u c c e s s )
196 {
197 s t d : : c o u t << ”ERROR: F a i l e d cudaMemcpy ” << s t d : : e n d l ;
198 r e t u r n −1;
199 }
200
201 # i f d e f DEBUG TIME
202 // S t o p c l o c k
203 s t r u c t t i m e b s t o p t i m e s t ; 204 f t i m e (& s t o p t i m e s t ) ;
205 d o u b l e e l a p s e d = ( (d o u b l e) s t o p t i m e s t . t i m e +
206 ( (d o u b l e) s t o p t i m e s t . m i l l i t m ∗ 0 . 0 0 1 ) ) −
207 ( (d o u b l e) s t a r t t i m e s t . t i m e + ( (d o u b l e) s t a r t t i m e s t . m i l l i t m ∗ 0 . 0 0 1 ) ) ; 208
209 s t d : : c o u t << ”∗ Time e l a p s e d : ” << s t d : : s e t p r e c i s i o n ( 5 ) <<
210 << e l a p s e d << ” s e c ”<< s t d : : e n d l ;
211 #e n d i f 212
213 c h a r∗ b u f f = new c h a r[ n e w w i d t h ∗ n e w h e i g h t ∗ b p p ] ; 214 i f ( ! b u f f )
215 {
216 s t d : : c o u t << ”ERROR: F a i l e d t o a l l o c a t e memory ”<< s t d : : e n d l ;
217 r e t u r n −1;
218 }
39
219 220
221 f o r (i n t i = 0 ; i < ( n e w w i d t h ∗ n e w h e i g h t ) ; i ++)
222 {
223 b u f f [ i ∗ b p p + 0 ] = (c h a r) f l o o r ( c p u o u t i m a g e [ i ∗ 4 + 0 ] ∗ 2 5 5 . f ) ; 224 b u f f [ i ∗ b p p + 1 ] = (c h a r) f l o o r ( c p u o u t i m a g e [ i ∗ 4 + 1 ] ∗ 2 5 5 . f ) ; 225 b u f f [ i ∗ b p p + 2 ] = (c h a r) f l o o r ( c p u o u t i m a g e [ i ∗ 4 + 2 ] ∗ 2 5 5 . f ) ;
226 }
227
228 # i f d e f DEBUG
229 s t d : : c o u t << s t d : : e n d l << ”>>> D e b u g g i n g O u t p u t d a t a : ”<< s t d : : e n d l ; 230 f o r (i n t i =0; i < w i d t h∗h e i g h t∗b p p ; i+=b p p )
231 {
232 i f ( ! ( i % ( w i d t h∗b p p ) ) ) 233 s t d : : c o u t << s t d : : e n d l ; 234
235 s t d : : c o u t << s t d : : d e c << ”R : ”<< (i n t) b u f f [ i ] <<
236 ” G: ” << (i n t) b u f f [ i +1]<<
237 ” B : ” << (i n t) b u f f [ i +2]<< ” ” ;
238 }
239 s t d : : c o u t << s t d : : e n d l << s t d : : e n d l ; 240 #e n d i f
241
242 I p l I m a g e∗ o u t i m a g e = c v C r e a t e I m a g e ( c v S i z e ( n e w w i d t h , n e w h e i g h t ) ,
243 i n p u t i m a g e−>d e p t h , b p p ) ;
244 i f ( ! o u t i m a g e )
245 {
246 s t d : : c o u t << ”ERROR: F a i l e d c v C r e a t e I m a g e ”<< s t d : : e n d l ;
247 r e t u r n −1;
248 }
249
250 o u t i m a g e−>i m a g e D a t a = b u f f ; 251
252 i f ( ! c v S a v e I m a g e ( a r g v [ 2 ] , o u t i m a g e ) )
253 {
254 s t d : : c o u t << ”ERROR: F a i l e d c v S a v e I m a g e ”<< s t d : : e n d l ;
255 }
256
257 c u d a e r r = c u d a F r e e ( g p u i m a g e ) ; 258 i f ( c u d a e r r != c u d a S u c c e s s )
259 {
260 s t d : : c o u t << ”ERROR: F a i l e d c u d a F r e e ”<< s t d : : e n d l ;
261 r e t u r n −1;
262 }
263
264 c u d a e r r 1 = c u d a F r e e ( g p u o u t i m a g e ) ; 265 i f ( c u d a e r r 1 != c u d a S u c c e s s )
266 {
267 s t d : : c o u t << ”ERROR: F a i l e d c u d a F r e e ”<< s t d : : e n d l ;
268 r e t u r n −1;
269 }
270
271 c v R e l e a s e I m a g e (& i n p u t i m a g e ) ; 272 c v R e l e a s e I m a g e (& o u t i m a g e ) ; 273
274
275 d e l e t e[ ] c p u i m a g e ; 276 d e l e t e[ ] b u f f ; 277
278 r e t u r n 0 ;
279 }
unroll kernels.h
Zaglavna datoteka:
1
2 #i n c l u d e <c u d a r u n t i m e a p i . h>
3 #i n c l u d e <c u d a . h>
4 #i n c l u d e <m a t h c o n s t a n t s . h>
5 #i n c l u d e <c u t i l m a t h . h>
6
7 e x t e r n ”C” v o i d c u d a t r a n s f o r m (f l o a t∗ imagem , f l o a t∗ o u t i m a g e , i n t w i d t h ,
8 i n t h e i g h t , i n t n e w w i d t h , i n t n e w h e i g h t ,
9 dim3 b l o c k s , dim3 b l o c k s i z e ) ;
unroll kernels.cu
Funkcije, ki se izvajajo na grafiˇ cni kartici:
1 #i n c l u d e ” i n t e r n a l / c u t i l m a t h b u g f i x e s . h ” 2 #d e f i n e WEIGHTS b s p l i n e w e i g h t s
3 #i n c l u d e ” i n t e r n a l / b s p l i n e k e r n e l . c u ” 4 #i n c l u d e <m a t h c o n s t a n t s . h>
5
6 d e v i c e v o i d f i l t e r ( f l o a t 4∗ img , f l o a t 4∗ o u t i m a g e , i n t w i d t h , i n t h e i g h t ,
7 d o u b l e x , d o u b l e y ,i n t i )
8 {
9 f l o a t d x=r e m a i n d e r ( x , 1 ) ;
10 f l o a t d y=r e m a i n d e r ( y , 1 ) ;
11 i n t o l d i = f l o o r ( y−0 . 5 )∗( w i d t h )+ f l o o r ( x−0 . 5 ) ;
12
13 i n t b p p = 4 ;
14 i n t p x = (i n t) x ; // f l o o r o f x
15 i n t p y = (i n t) y−0 . 5 ; // f l o o r o f y
16 c o n s t i n t s t r i d e = w i d t h ;
17
18 // C a l c u l a t e t h e w e i g h t s f o r e a c h p i x e l
19 d o u b l e f x = d x ;
20 d o u b l e f y = d y ;
21 d o u b l e f x 1 = 1 . 0 f − f x ;
22 d o u b l e f y 1 = 1 . 0 f − f y ;
23
24 d o u b l e w1 = 0 . 0 1 + f x 1 ∗ f y 1 ∗ 0 . 9 f ;
25 d o u b l e w2 = 0 . 0 1 + f x ∗ f y 1 ∗ 0 . 9 f ;
26 d o u b l e w3 = 0 . 0 1 + f x 1 ∗ f y ∗ 0 . 9 f ;
27 d o u b l e w4 = 0 . 0 1 + f x ∗ f y ∗ 0 . 9 f ;
28
29 f l o a t r = ( img [ o l d i ] . x ∗ w1 +
30 img [ o l d i + 1 ] . x ∗ w2 +
31 img [ o l d i + s t r i d e ] . x ∗ w3 +
32 img [ o l d i + s t r i d e + 1 ] . x ∗ w4 ) ;
33 f l o a t g = ( img [ o l d i ] . y ∗ w1 +
34 img [ o l d i + 1 ] . y ∗ w2 +
35 img [ o l d i + s t r i d e ] . y ∗ w3 +
36 img [ o l d i + s t r i d e + 1 ] . y ∗ w4 ) ;
37 f l o a t b = ( img [ o l d i ] . z ∗ w1 +
38 img [ o l d i + 1 ] . z ∗ w2 +
39 img [ o l d i + s t r i d e ] . z ∗ w3 +
40 img [ o l d i + s t r i d e + 1 ] . z ∗ w4 ) ;
41 o u t i m a g e [ i ] = m a k e f l o a t 4 ( r , g , b , 0 ) ;
42 }
41
43
44 g l o b a l v o i d t r a n s f o r m ( f l o a t 4∗ imagem , f l o a t 4∗ o u t i m a g e , i n t w i d t h , 45 i n t h e i g h t , i n t n e w w i d t h , i n t n e w h e i g h t )
46 {
47 c o n s t i n t i = b l o c k I d x . x ∗ ( b l o c k D i m . x ∗ b l o c k D i m . y ) + 48 b l o c k D i m . x ∗ t h r e a d I d x . y + t h r e a d I d x . x ;
49
50 i f( i < n e w w i d t h ∗ n e w h e i g h t )
51 {
52
53 // i n t x = b l o c k I d x . x∗b l o c k D i m . x + t h r e a d I d x . x ; 54 // i n t y = b l o c k I d x . y∗b l o c k D i m . y + t h r e a d I d x . y ; 55 i n t x = r e m a i n d e r ( (d o u b l e) i , (d o u b l e) n e w w i d t h ) ;
56 i n t y = f l o o r f ( (d o u b l e) ( i / n e w w i d t h ) ) ;
57 d o u b l e x 1 = −((d o u b l e) ( x )∗2∗CUDART PI F / (d o u b l e) n e w w i d t h ) ;
58 d o u b l e y 1 = ( (d o u b l e) ( y ) / (d o u b l e) n e w h e i g h t )−1;
59 d o u b l e o l d x 1 = y 1∗c o s f ( x 1 ) ;
60 d o u b l e o l d y 1 = y 1∗s i n f ( x 1 ) ;
61 d o u b l e o l d x = ( ( o l d x 1 + 1 )∗(d o u b l e) w i d t h / 2 ) ;
62 d o u b l e o l d y = ( ( o l d y 1 + 1 )∗(d o u b l e) h e i g h t / 2 ) ;
63 i n t o l d i = f l o o r f ( o l d y )∗w i d t h+ f l o o r f ( o l d x ) ;
64 // f l o a t r = imagem [ o l d i ] . x ;
65 // f l o a t g = imagem [ o l d i ] . y ;
66 // f l o a t b = imagem [ o l d i ] . z ;
67 // o u t i m a g e [ i ] = m a k e f l o a t 4 ( r , g , b , 0 ) ;
68 f i l t e r ( imagem , o u t i m a g e , w i d t h , h e i g h t , o l d x , o l d y , i ) ;
69 }
70 }
71
72 e x t e r n ”C” v o i d c u d a t r a n s f o r m (f l o a t∗ imagem , f l o a t∗ o u t i m a g e , i n t w i d t h , 73 i n t h e i g h t ,i n t n e w w i d t h , i n t n e w h e i g h t , dim3 b l o c k s , dim3 b l o c k s i z e )
74 {
75 t r a n s f o r m <<< b l o c k s , b l o c k s i z e >>> ( ( f l o a t 4∗) imagem , 76 ( f l o a t 4∗) o u t i m a g e , w i d t h , h e i g h t , n e w w i d t h , n e w h e i g h t ) ;
77 }
Program unroll center
Program poˇ zenemo kot: unroll_center vhodna_datoteka.jpg izhodna_datoteka.jpg.
Pri tem je vhodna_datoteka.jpg vhodna datoteka tipa jpg in izhodna_datoteka.jpg izhodna datoteka tipa jpg.
Program doloˇ ci center in premer kroga za program v Matlabu. Matlabov program, opisan v nadaljevanju ga kliˇ ce. Program mora biti v istem imeniku, kot Matlabovi programi.
unroll center.cpp
1 #i n c l u d e <c v . h>
2 #i n c l u d e <h i g h g u i . h>
3 #i n c l u d e <math . h>
4
5 i n t n o t E n t i r e l y (i n t x , i n t y , i n t r , i n t width , i n t h e i g h t ) {
6 i f ( ( x−r<0) | | ( x+r>w i d t h ) | | ( y−r<0) | | ( y+r>h e i g h t ) ) return 1 ;
7 e l s e return 0 ;
8 }
9
10 i n t main (i n t a r g c , char∗∗ a r g v )
11 {
12 I p l I m a g e∗ img = cvLoadImage ( a r g v [ 1 ] , 1 ) ; ;
13 I p l I m a g e∗ g r a y = c v C r e a t e I m a g e ( c v G e t S i z e ( img ) , 8 , 1 ) ; 14 CvMemStorage∗ s t o r a g e = c v C r e a t e M e m S t o r a g e ( 0 ) ; 15 c v C v t C o l o r ( img , g r a y , CV BGR2GRAY ) ;
16 cvSmooth ( g r a y , g r a y , CV GAUSSIAN , 9 , 9 ) ; 17 CvSeq∗ c i r c l e s = c v H o u g h C i r c l e s ( g r a y , s t o r a g e , 18 CV HOUGH GRADIENT, 2 , g r a y−>h e i g h t / 4 , 2 0 0 , 1 0 0 ) ;
19 i n t i ;
20
21 // u g o t o v i n a j v e ? j i k r o g
22 i n t maxx=0;
23 i n t maxy=0;
24 i n t maxr =0;
25 f o r ( i = 0 ; i < c i r c l e s−>t o t a l ; i ++)
26 {
27 f l o a t∗ p = (f l o a t∗) cvGetSeqElem ( c i r c l e s , i ) ; 28
29 i n t x=cvRound ( p [ 0 ] ) ; 30 i n t y=cvRound ( p [ 1 ] ) ;
43
31 i n t r=cvRound ( p [ 2 ] ) ; 32
33 i f ( r>maxr ) {
34 maxx=x ;
35 maxy=y ;
36 maxr=r ;
37 }
38
39 /∗c v C i r c l e ( img , c v P o i n t ( cvRound ( p [ 0 ] ) , cvRound ( p [ 1 ] ) ) ,
40 3 , CV RGB ( 0 , 2 5 5 , 0 ) , −1 , 8 , 0 ) ;
41 c v C i r c l e ( img , c v P o i n t ( cvRound ( p [ 0 ] ) , cvRound ( p [ 1 ] ) ) ,
42 cvRound ( p [ 2 ] ) , CV RGB ( 2 5 5 , 0 , 0 ) , 3 , 8 , 0 ) ;∗/
43
44 }
45 // Ce k r o g n i v c e l o t i na s l i k i , v z e m i k a r g e o m e t r i j s k o
46 // s r e d i s c e s l i k e . . .
47 i f ( n o t E n t i r e l y ( maxx , maxy , maxr , g r a y−>width , g r a y−>h e i g h t ) ) { 48 maxx=cvRound ( g r a y−>w i d t h / 2 ) ;
49 maxy=cvRound ( g r a y−>h e i g h t / 2 ) ;
50 maxr=( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ?
51 cvRound ( g r a y−>h e i g h t / 2 ) : cvRound ( g r a y−>w i d t h / 2 ) ) ;
52 }
53
54 // Ce j e k r o g m a n j s i od 80% n a j m a n s e z u n a n j e d i m e n z i j e 55 // s l i k e , vzemimo g e o m e t r i s k o s r e d i s c e s l i k e
56
57 i f ( maxr<0 . 8∗( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ?
58 cvRound ( g r a y−>h e i g h t / 2 ) : cvRound ( g r a y−>w i d t h / 2 ) ) ){
59 maxx=cvRound ( g r a y−>w i d t h / 2 ) ; 60 maxy=cvRound ( g r a y−>h e i g h t / 2 ) ;
61 maxr=( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ?
62 cvRound ( g r a y−>h e i g h t / 2 ) : cvRound ( g r a y−>w i d t h / 2 ) ) ;
63 }
64
65 // cvNamedWindow ( ” c i r c l e s ” , 1 ) ; 66 // c v S h o w I m a g e ( ” c i r c l e s ” , img ) ;
67 // c v S a v e I m a g e ( ” c i r c l e o u t p u t . j p g ” , img ) ; 68 // c v W a i t K e y ( 0 ) ;
69 p r i n t f ( ”%d %d %d\n ” , maxx , maxy , maxr ) ;
70 return 0 ;
71 }
Program unroll: programska zanka
Program poˇ zenemo kot: unroll vhodna_datoteka.jpg izhodna_datoteka.jpg. Pri tem je vhodna_datoteka.jpg vhodna datoteka tipa jpg in izhodna_datoteka.jpg iz-hodna datoteka tipa jpg.
Program prebere vhodno vsesmerno sliko v vhodni datoteki, s Hughovo transformacijo poiˇ sˇ ce center in izpiˇ se panoramsko sliko vhodne slike v izhodno datoteko.
unroll.cc
1 #i n c l u d e <o p e n c v / c v . h>
2 #i n c l u d e <o p e n c v / h i g h g u i . h>
3 #i n c l u d e <i o m a n i p>
4 #i n c l u d e <i o s t r e a m>
5 #i n c l u d e <math . h>
6 #i n c l u d e <s y s / t i m e b . h>
7 #i n c l u d e <t i m e . h>
8 9
10 #d e f i n e DEBUG TIME 11
12 void f i l t e r (f l o a t∗ img , f l o a t∗ o u t i m a g e , i n t width , i n t h e i g h t , double x ,
13 double y ,i n t i )
14 {
15 f l o a t dx=r e m a i n d e r ( x , 1 ) ; 16 f l o a t dy=r e m a i n d e r ( y , 1 ) ;
17 i n t o l d i = f l o o r ( y−0 . 5 )∗( w i d t h )+ f l o o r ( x−0 . 5 ) ; 18
19 i n t bpp = 4 ;
20 i n t px = (i n t) x ; // f l o o r o f x 21 i n t py = (i n t) y−0 . 5 ; // f l o o r o f y 22 const i n t s t r i d e = w i d t h ;
23
24 // C a l c u l a t e t h e w e i g h t s f o r e a c h p i x e l
25 double f x = dx ;
26 double f y = dy ;
27 double f x 1 = 1 . 0 f − f x ; 28 double f y 1 = 1 . 0 f − f y ; 29
30 double w1 = 0 . 0 1 + f x 1 ∗ f y 1 ∗ 0 . 9 f ;
45
31 double w2 = 0 . 0 1 + f x ∗ f y 1 ∗ 0 . 9 f ; 32 double w3 = 0 . 0 1 + f x 1 ∗ f y ∗ 0 . 9 f ; 33 double w4 = 0 . 0 1 + f x ∗ f y ∗ 0 . 9 f ; 34
35 o u t i m a g e [ i∗4 ] = ( img [ o l d i∗4+0] ∗ w1 +
36 img [ ( o l d i +1)∗4+0] ∗ w2 +
37 img [ ( o l d i+ s t r i d e )∗4 + 0 ] ∗ w3 +
38 img [ ( o l d i+ s t r i d e +1)∗4+0] ∗ w4 ) ;
39 o u t i m a g e [ i∗4+1] = ( img [ o l d i∗4+1] ∗ w1 +
40 img [ ( o l d i +1)∗4+1] ∗ w2 +
41 img [ ( o l d i+ s t r i d e )∗4 + 1 ] ∗ w3 +
42 img [ ( o l d i+ s t r i d e +1)∗4+1]∗ w4 ) ;
43 o u t i m a g e [ i∗4+2] = ( img [ o l d i∗4+2] ∗ w1 +
44 img [ ( o l d i +1)∗4+2] ∗ w2 +
45 img [ ( o l d i+ s t r i d e )∗4 + 2 ] ∗ w3 +
46 img [ ( o l d i+ s t r i d e +1)∗4+2]∗ w4 ) ;
47 }
48
49 void t r a n s f o r m (f l o a t∗ imagem , f l o a t∗ o u t i m a g e , i n t width , i n t h e i g h t ,
50 i n t newwidth , i n t n e w h e i g h t )
51 {
52 f o r (i n t y =0; y<n e w h e i g h t ; y++){
53 f o r (i n t x =0; x<newwidth ; x++){
54 // i n t x = r e m a i n d e r ( ( d o u b l e ) i , ( d o u b l e ) n e w w i d t h ) ; 55 // i n t y = f l o o r f ( ( d o u b l e ) ( i / n e w w i d t h ) ) ;
56 i n t i = f l o o r f ( y )∗newwidth+ f l o o r f ( x ) ;
57 double x1 = −((double) ( x )∗2∗M PI / (double) newwidth ) ; 58 double y1 = ( (double) ( y ) / (double) n e w h e i g h t )−1;
59 double o l d x 1 = y1∗c o s f ( x1 ) ; 60 double o l d y 1 = y1∗s i n f ( x1 ) ;
61 double o l d x = ( ( o l d x 1 + 1 )∗(double) w i d t h / 2 ) ; 62 double o l d y = ( ( o l d y 1 + 1 )∗(double) h e i g h t / 2 ) ; 63 i n t o l d i = f a b s ( f l o o r f ( o l d y )∗w i d t h+ f l o o r f ( o l d x ) ) ; 64 // f l o a t r = imagem [ o l d i ] . x ;
65 // f l o a t g = imagem [ o l d i ] . y ; 66 // f l o a t b = imagem [ o l d i ] . z ;
67 // o u t i m a g e [ i ] = m a k e f l o a t 4 ( r , g , b , 0 ) ;
68 f i l t e r ( imagem , o u t i m a g e , width , h e i g h t , o l d x , o l d y , i ) ;
69 }
70 }
71 }
72
73 i n t n o t E n t i r e l y (i n t x , i n t y , i n t r , i n t width , i n t h e i g h t ) { 74 i f ( ( x−r<0) | | ( x+r>w i d t h ) | | ( y−r<0) | | ( y+r>h e i g h t ) ) return 1 ; 75 e l s e return 0 ;
76 }
77
78 i n t main (i n t a r g c , char∗∗ a r g v )
79 {
80 i f ( a r g c < 3 )
81 {
82 s t d : : c o u t << ” U p o r a b i : ” << a r g v [ 0 ] << ” <i n p u t>” << ” <o u t p u t>” << s t d : : e n d l ; 83 e x i t ( 1 ) ;
84 }
85
86 I p l I m a g e∗ i n p u t i m a g e = NULL ;
87 i n p u t i m a g e = cvLoadImage ( a r g v [ 1 ] , CV LOAD IMAGE UNCHANGED ) ; 88 i f( ! i n p u t i m a g e )
89 {
90 s t d : : c o u t << ”ERROR: F a i l e d t o l o a d i n p u t i m a g e ” << s t d : : e n d l ;
91 return −1;
92 }
93
94 I p l I m a g e∗ g r a y = c v C r e a t e I m a g e ( c v G e t S i z e ( i n p u t i m a g e ) , 8 , 1 ) ;
47
95 CvMemStorage∗ s t o r a g e = c v C r e a t e M e m S t o r a g e ( 0 ) ; 96 c v C v t C o l o r ( i n p u t i m a g e , g r a y , CV BGR2GRAY ) ; 97 cvSmooth ( g r a y , g r a y , CV GAUSSIAN , 9 , 9 ) ; 98 CvSeq∗ c i r c l e s = c v H o u g h C i r c l e s ( g r a y , s t o r a g e ,
99 CV HOUGH GRADIENT, 2 , g r a y−>h e i g h t / 8 , 2 0 0 ,
100 1 0 0 , g r a y−>h e i g h t / 8 ,
101 ( g r a y−>width> g r a y−>h e i g h t ?
102 g r a y−>h e i g h t : g r a y−>w i d t h ) ) ;
103
104 i n t w i d t h = i n p u t i m a g e−>w i d t h ; 105 i n t h e i g h t = i n p u t i m a g e−>h e i g h t ; 106
107 // u g o t o v i n a j v e ? j i k r o g
108 i n t maxx=0; i n t maxy=0; i n t maxr =0; i n t i ;
109
110 f o r ( i = 0 ; i < c i r c l e s−>t o t a l ; i ++)
111 {
112 f l o a t∗ p = (f l o a t∗) cvGetSeqElem ( c i r c l e s , i ) ; 113
114 i n t x=cvRound ( p [ 0 ] ) ; i n t y=cvRound ( p [ 1 ] ) ; i n t r=cvRound ( p [ 2 ] ) ; 115
116 i f ( r>maxr ) {
117 maxx=x ; maxy=y ; maxr=r ;
118 }
119
120 c v C i r c l e ( i n p u t i m a g e , c v P o i n t ( cvRound ( p [ 0 ] ) , cvRound ( p [ 1 ] ) ) ,
121 3 , CV RGB ( 0 , 2 5 5 , 0 ) , −1 , 8 , 0 ) ;
122 c v C i r c l e ( i n p u t i m a g e , c v P o i n t ( cvRound ( p [ 0 ] ) , cvRound ( p [ 1 ] ) ) , 123 cvRound ( p [ 2 ] ) , CV RGB ( 2 5 5 , 0 , 0 ) , 3 , 8 , 0 ) ;
124
125 }
126
127 // Ce k r o g n i v c e l o t i na s l i k i , v z e m i k a r g e o m e t r i j s k o s r e d i s c e s l i k e . . . 128 i f ( n o t E n t i r e l y ( maxx , maxy , maxr , g r a y−>width , g r a y−>h e i g h t ) ) {
129 maxx=cvRound ( g r a y−>w i d t h / 2 ) ; 130 maxy=cvRound ( g r a y−>h e i g h t / 2 ) ;
131 maxr=( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ? cvRound ( g r a y−>h e i g h t / 2 ) : 132 cvRound ( g r a y−>w i d t h / 2 ) ) ;
133 }
134
135 // Ce j e k r o g m a n j s i od 80% n a j m a n j s e z u n a n j e d i m e n z i j e s l i k e , 136 // v z e m i g e o m e t r i s k o s r e d i s c e s l i k e
137
138 i f ( maxr<0 . 8∗( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ? cvRound ( g r a y−>h e i g h t / 2 ) :
139 cvRound ( g r a y−>w i d t h / 2 ) ) ){
140 maxx=cvRound ( g r a y−>w i d t h / 2 ) ; 141 maxy=cvRound ( g r a y−>h e i g h t / 2 ) ;
142 maxr=( ( g r a y−>w i d t h > g r a y−>h e i g h t ) ? cvRound ( g r a y−>h e i g h t / 2 ) : 143 cvRound ( g r a y−>w i d t h / 2 ) ) ;
144 }
145
146 i n t r =0;
147 i n t n e w h e i g h t=r ; 148
149 i f ( h e i g h t > 2 ∗ maxy ) n e w h e i g h t = maxy ; 150 e l s e n e w h e i g h t = h e i g h t−maxy ;
151
152 r=maxr−(maxr % 8 ) ; 153 n e w h e i g h t=r +(16−r % 1 6 ) ;
154 i n t newwidth= M PI∗r +(16−(i n t) ( M PI∗r ) % 1 6 ) ; ; 155
156 p r i n t f ( ”%d %d %d\n ” , n e w h e i g h t , newwidth , r ) ; 157
158 cvSetImageROI ( i n p u t i m a g e , c v R e c t ( maxx−r , maxy−r , 2∗r , 2∗r ) ) ;
159 I p l I m a g e∗ img = c v C r e a t e I m a g e ( c v S i z e ( 2∗r , 2∗r ) , i n p u t i m a g e−>depth ,
160 i n p u t i m a g e−>n C h a n n e l s ) ;
161 cvCopy ( i n p u t i m a g e , img , NULL ) ; 162 cvResetImageROI ( i n p u t i m a g e ) ; 163
164 cvNamedWindow ( ” c i r c l e s ” , 1 ) ; 165 cvShowImage ( ” c i r c l e s ” , img ) ;
166 c v S a v e I m a g e ( ” c i r c l e o u t p u t . j p g ” , img ) ; 167 cvWaitKey ( 0 ) ;
168 169
170 i n t bpp = i n p u t i m a g e−>n C h a n n e l s ; 171
172 #i f d e f DEBUG
173 s t d : : c o u t << ”>> Width : ” << w i d t h << s t d : : e n d l <<
174 ”>> H e i g h t : ” << h e i g h t << s t d : : e n d l <<
175 ”>>Bpp : ” << bpp<< s t d : : e n d l ; 176
177 s t d : : c o u t << s t d : : e n d l<< ”>>> D e b u g g i n g O r i g i n a l d a t a : ”<< s t d : : e n d l ; 178 f o r (i n t i =0; i < w i d t h∗h e i g h t∗bpp ; i+=bpp )
179 {
180 i f ( ! ( i % ( w i d t h∗bpp ) ) ) 181 s t d : : c o u t<< s t d : : e n d l ; 182
183 s t d : : c o u t << s t d : : d e c << ”R : ”<< (i n t) i n p u t i m a g e−>imageData [ i ] <<
184 ” G: ”<< (i n t) i n p u t i m a g e−>imageData [ i +1]<<
185 ” B : ”<< (i n t) i n p u t i m a g e−>imageData [ i +2]<< ” ” ;
186 }
187 s t d : : c o u t << s t d : : e n d l<< s t d : : e n d l ; 188 #e n d i f
189
190 f l o a t∗ c p u i m a g e =new f l o a t[ w i d t h ∗ h e i g h t ∗ 4 ] ; 191 i f ( ! c p u i m a g e )
192 {
193 s t d : : c o u t << ”ERROR: F a i l e d t o a l l o c a t e memory” << s t d : : e n d l ;
194 return −1;
195 }
196
197 f o r (i n t i = 0 ; i < 2∗r ∗ 2∗r ; i ++)
198 {
199 c p u i m a g e [ i ∗ 4 + 0 ] = (unsigned char) img−>imageData [ i ∗ bpp + 0 ] / 2 5 5 . f ; 200 c p u i m a g e [ i ∗ 4 + 1 ] = (unsigned char) img−>imageData [ i ∗ bpp + 1 ] / 2 5 5 . f ; 201 c p u i m a g e [ i ∗ 4 + 2 ] = (unsigned char) img−>imageData [ i ∗ bpp + 2 ] / 2 5 5 . f ; 202 c p u i m a g e [ i ∗ 4 + 3 ] = (unsigned char) img−>imageData [ i ∗ bpp + 2 ] / 2 5 5 . f ;
203 }
204
205 f l o a t∗ c p u o u t i m a g e =new f l o a t[ newwidth ∗ n e w h e i g h t ∗ 4 ] ; 206 i f ( ! c p u o u t i m a g e )
207 {
208 s t d : : c o u t << ”ERROR: F a i l e d t o a l l o c a t e memory” << s t d : : e n d l ;
209 return −1;
210 }
211
212 #i f d e f DEBUG TIME 213 // S t a r t c l o c k
214 s t r u c t t i m e b s t a r t t i m e s t ; 215 f t i m e (& s t a r t t i m e s t ) ; 216 #e n d i f
217 218
219 t r a n s f o r m ( c p u i m a g e , c p u o u t i m a g e , 2∗r , 2∗r , newwidth , n e w h e i g h t ) ; 220
221
222 #i f d e f DEBUG TIME
49
223 // S t o p c l o c k
224 s t r u c t t i m e b s t o p t i m e s t ; 225 f t i m e (& s t o p t i m e s t ) ;
226 double e l a p s e d = ( (double) s t o p t i m e s t . t i m e +
227 ( (double) s t o p t i m e s t . m i l l i t m ∗ 0 . 0 0 1 ) ) −
228 ( (double) s t a r t t i m e s t . t i m e + ( (double) s t a r t t i m e s t . m i l l i t m ∗ 0 . 0 0 1 ) ) ; 229
230 s t d : : c o u t << ”∗ Time e l a p s e d : ” << s t d : : s e t p r e c i s i o n ( 5 ) << e l a p s e d << ” s e c ” << s t d : : e n d l ; 231 #e n d i f
232
233 char∗ b u f f =new char[ newwidth ∗ n e w h e i g h t ∗ bpp ] ; 234 i f ( ! b u f f )
235 {
236 s t d : : c o u t <<”ERROR: F a i l e d t o a l l o c a t e memory”<< s t d : : e n d l ;
237 return −1;
238 }
239 240
241 f o r (i n t i = 0 ; i < ( newwidth ∗ n e w h e i g h t ) ; i ++)
242 {
243 b u f f [ i ∗ bpp + 0 ] = (char) f l o o r ( c p u o u t i m a g e [ i ∗ 4 + 0 ] ∗ 2 5 5 . f ) ; 244 b u f f [ i ∗ bpp + 1 ] = (char) f l o o r ( c p u o u t i m a g e [ i ∗ 4 + 1 ] ∗ 2 5 5 . f ) ; 245 b u f f [ i ∗ bpp + 2 ] = (char) f l o o r ( c p u o u t i m a g e [ i ∗ 4 + 2 ] ∗ 2 5 5 . f ) ;
246 }
247
248 #i f d e f DEBUG
249 s t d : : c o u t << s t d : : e n d l << ”>>> D e b u g g i n g Output d a t a : ”<< s t d : : e n d l ; 250 f o r (i n t i =0; i < w i d t h∗h e i g h t∗bpp ; i+=bpp )
251 {
252 i f ( ! ( i % ( w i d t h∗bpp ) ) ) 253 s t d : : c o u t << s t d : : e n d l ; 254
255 s t d : : c o u t << s t d : : d e c << ”R : ”<< (i n t) b u f f [ i ] <<
256 ” G: ”<< (i n t) b u f f [ i +1]<<
257 ” B : ”<< (i n t) b u f f [ i +2]<< ” ” ;
258 }
259 s t d : : c o u t << s t d : : e n d l << s t d : : e n d l ; 260 #e n d i f
261
262 I p l I m a g e∗ o u t i m a g e = c v C r e a t e I m a g e ( c v S i z e ( newwidth , n e w h e i g h t ) ,
263 i n p u t i m a g e−>depth , bpp ) ;
264 i f ( ! o u t i m a g e )
265 {
266 s t d : : c o u t <<”ERROR: F a i l e d c v C r e a t e I m a g e ”<< s t d : : e n d l ;
267 return −1;
268 }
269
270 o u t i m a g e−>imageData = b u f f ; 271
272 i f ( ! c v S a v e I m a g e ( a r g v [ 2 ] , o u t i m a g e ) )
273 {
274 s t d : : c o u t <<”ERROR: F a i l e d c v S a v e I m a g e ” << s t d : : e n d l ;
275 }
276
277 c v R e l e a s e I m a g e (& i n p u t i m a g e ) ; 278 c v R e l e a s e I m a g e (& o u t i m a g e ) ; 279
280
281 d e l e t e[ ] c p u i m a g e ; 282 d e l e t e[ ] b u f f ; 283
284 return 0 ;
285 }
Drugi program je spisan v programskem okolju Matlab, opisan v poglavju 4:
Priloga k 4.1:
Program unroll
Program kliˇ cemo iz Matlabove lupine kot unroll(’slika.jpg’, parametri);. Parametri za posamezne transformacije so opisani v poglavju 4.
unroll.m
1 f u n c t i o n [ i m g W r i t e , TOC ] = u n r o l l ( i m e S l i k e , t i p , a , b , c ) 2
3 %% S t r o j n o d o l o c a n j e c e n t r a 4 s o u r c e v = i m r e a d ( i m e S l i k e ) ; 5 command= ’ . / u n r o l l c e n t e r ’ ; 6 command=h o r z c a t ( command , i m e S l i k e ) ; 7 [ s t a t u s , c e n t e r ]= s y s t e m ( command ) ; 8 [ x , c e n t e r ] = s t r t o k( c e n t e r ) ; 9 x= s t r 2 d o u b l e ( x ) ;
10 [ y , c e n t e r ] = s t r t o k( c e n t e r ) ; 11 y= s t r 2 d o u b l e ( y ) ;
12 [ r , c e n t e r ] = s t r t o k( c e n t e r ) ; 13 r= s t r 2 d o u b l e ( r ) ;
14
15 % n o v a o p t i m a l n a v e l i k o s t s l i k e , m a k s i m a l n a r e s o l u c i j a 16 n e w w i d t h=d o u b l e ( i n t 6 4 (p i∗r ) ) ;
17 t i c;
18 i f ( t i p== ’ b ’ )
19 panorama= u n r o l l b a s i c ( i m e S l i k e , x , y ) ; 20 e l s e
21 i f ( t i p== ’ h ’ )
22 i f ( n o t (e x i s t( ’ a ’ , ’ v a r ’ ) ) )
23 a = 3 9 . 2 9 2 ;
24 e n d
25 i f ( n o t (e x i s t( ’ b ’ , ’ v a r ’ ) ) )
26 b = 1 9 . 6 4 6 ;
27 e n d
28 i f ( n o t (e x i s t( ’ c ’ , ’ v a r ’ ) ) )
29 c = 2 . 3 ;
30 e n d
31 panorama= u n r o l l h y p e r b o l i c ( i m e S l i k e , x , y , a , b , c ) ;
32 e l s e
33 i f ( t i p== ’ k ’ )
34 panorama= u n r o l l k r o g l a ( i m e S l i k e , x , y , a , b , c ) ;
35 e l s e
36 i f ( t i p== ’ p ’ )
37 panorama= u n r o l l p a r a b o l i c ( i m e S l i k e , x , y , a ) ;
38 e l s e
39 panorama= u n r o l l b a s i c ( i m e S l i k e , x , y ) ;
51
40 e n d
41 e n d
42 e n d
43 e n d
44 TOC=t o c; 45 i f ( ( t i p ˜= ’ f ’ ) )
46 imshow ( panorama ) ;
47 [ ˜ , y ]=g i n p u t( 2 ) ; 48
49 i f ( d o u b l e ( i n t 6 4 ( y ( 1 ) ) ) > d o u b l e ( i n t 6 4 ( y ( 2 ) ) ) )
50 panorama=i m c r o p ( panorama , [ 0 d o u b l e ( i n t 6 4 ( y ( 2 ) ) ) n e w w i d t h d o u b l e ( i n t 6 4 ( y (1)−y ( 2 ) ) ) ] ) ;
51 e l s e
52 panorama=i m c r o p ( panorama , [ 0 d o u b l e ( i n t 6 4 ( y ( 1 ) ) ) n e w w i d t h d o u b l e ( i n t 6 4 ( y (2)−y ( 1 ) ) ) ] ) ;
53 e n d
54 e n d
55 %imshow ( panorama ) ; 56 i m g W r i t e=panorama ; 57
58 e n d
unroll basic.m
Funkcije za osnovno transformacijo brez upoˇ stevanja zrcala:
1 f u n c t i o n [ i m g W r i t e ] = u n r o l l b a s i c ( i m e S l i k e , x , y )
2 %UNROLL Vrne r a z v i t o p a n o r a m s k o s l i k o o m n i d i r e k c i j s k e f o t o g r a f i j e . 3 % i m e S l i k e − ime s l i k e
4 % x − c e n t e r o f f s e t z a x 5 % y − c e n t e r o f f s e t z a y 6 s o u r c e v = i m r e a d ( i m e S l i k e ) ; 7 % V e l i k o s t s l i k e
8 [ h e i g h t , w i d t h ]=s i z e( s o u r c e v ) ; 9 % O d k o m e n t i r a j z a b a r v n e s l i k e 10 w i d t h = w i d t h / 3 ;
11 i f ( h e i g h t > d o u b l e ( i n t 6 4 ( 2 .∗y ) ) ) 12 h e i g h t = d o u b l e ( i n t 6 4 ( 2 .∗y ) ) ; 13 e l s e
14 h e i g h t = d o u b l e ( i n t 6 4 ( 2∗( h e i g h t−y ) ) ) ;
15 e n d
16 %Premer
17 i f ( w i d t h<h e i g h t )
18 r=d o u b l e ( i n t 6 4 ( w i d t h / 2 ) ) ; 19 e l s e
20 r=d o u b l e ( i n t 6 4 ( h e i g h t / 2 ) ) ;
21 e n d
22 n e w w i d t h=d o u b l e ( i n t 6 4 (p i∗r ) ) ; 23 n e w h e i g h t=d o u b l e ( i n t 6 4 ( r ) ) ;
24 s o u r c e=s o u r c e v ( y−r +1: y+r−1 , x−r +1: x+r−1 , : ) ; 25 % F u n k c i j e p r e s l i k a v e
26 u=@( x ) x ( : , 2 ) .∗c o s( x ( : , 1 ) ) ; 27 v=@( x ) x ( : , 2 ) .∗s i n( x ( : , 1 ) ) ; 28 i p a n o r a m a=@( x ) [ u ( x ) , v ( x ) ] ; 29 i n v e r s e =@( x , t ) i p a n o r a m a ( x ) ;
30 t f o r m 2 = m a k e t f o r m ( ’ c u s t o m ’ , 2 , 2 , [ ] , i n v e r s e , [ ] ) ;
31 i m g W r i t e = i m t r a n s f o r m ( s o u r c e , t f o r m 2 , ’ b i l i n e a r ’ , ’ UData ’ , [−1 1 ] , . . .
32 ’ VData ’ , [−1 1 ] , ’ XData ’ , [ 2∗p i 0 ] , ’ YData ’ , [ 1 0 ] , . . .
33 ’ S i z e ’ , [ n e w h e i g h t n e w w i d t h ] ) ;
34 e n d
53
unroll hiperbolic.m
Funkcije za hiperboliˇ cno zrcalo:
1 f u n c t i o n [ i m g W r i t e ] = u n r o l l h y p e r b o l i c ( i m e S l i k e , x , y , a , b , f o c u s ) 2 %UNROLL HYPERBOLIC Vrne r a z v i t o p a n o r a m s k o s l i k o o m n i d i r e k c i j s k e f o t o g r a f i j e . 3 % D e t a i l e d e x p l a n a t i o n g o e s h e r e
4 s o u r c e v = i m r e a d ( i m e S l i k e ) ; 5
6 % V e l i k o s t s l i k e
7 [ h e i g h t , w i d t h ]=s i z e( s o u r c e v ) ; 8 % O d k o m e n t i r a j z a b a r v n e s l i k e 9 w i d t h = w i d t h / 3 ;
10 i f ( h e i g h t > d o u b l e ( i n t 6 4 ( 2 .∗y ) ) ) 11 h e i g h t = d o u b l e ( i n t 6 4 ( 2 .∗y ) ) ; 12 e l s e
13 h e i g h t = d o u b l e ( i n t 6 4 ( 2∗( h e i g h t−y ) ) ) ;
14 e n d
15
16 %Premer
17 i f ( w i d t h<h e i g h t )
18 r=d o u b l e ( i n t 6 4 ( w i d t h / 2 ) ) ; 19 e l s e
20 r=d o u b l e ( i n t 6 4 ( h e i g h t / 2 ) ) ;
21 e n d
22 % n o v a o p t i m a l n a v e l i k o s t s l i k e , m a k s i m a l n a r e s o l u c i j a 23 n e w w i d t h=d o u b l e ( i n t 6 4 (p i∗r ) ) ;
24 n e w h e i g h t=d o u b l e ( i n t 6 4 ( r ) ) ;
25 s o u r c e=s o u r c e v ( y−r +1: y+r−1 , x−r +1: x+r−1 , : ) ; 26 e=s q r t( ( a . ˆ 2 ) + ( b . ˆ 2 ) ) ;
27 % F u n k c i j e p r e s l i k a v e
28 q t=@( x ) f o c u s .∗( a . ˆ 2 ) . / ( ( ( a . ˆ 2 )−2 .∗( e . ˆ 2 ) ) .∗t a n( x ) + . . . 29 2 .∗b .∗e .∗s q r t(1+t a n( x ) . ˆ 2 ) ) ;
30
31 u=@( x ) q t ( x ( : , 2 ) ) .∗c o s( x ( : , 1 ) ) ; 32 v=@( x ) q t ( x ( : , 2 ) ) .∗s i n( x ( : , 1 ) ) ; 33 i p a n o r a m a=@( x ) [ u ( x ) , v ( x ) ] ; 34 i n v e r s e =@( x , t ) i p a n o r a m a ( x ) ; 35
36 % I s k a n j e n i c l e f u n k c i j e q t 1 z f s o l v e i n i z r a c u n s l i k e ; 37 q t 1=@( x ) q t ( x )−1;
38 x 0=−p i/ 2 ;
39 t f o r m 2 = m a k e t f o r m ( ’ c u s t o m ’ , 2 , 2 , [ ] , i n v e r s e , [ ] ) ;
40 i m g W r i t e = i m t r a n s f o r m ( s o u r c e , t f o r m 2 , ’ b i l i n e a r ’ , ’ UData ’ , [−1 1 ] , . . .
41 ’ VData ’ , [−1 1 ] , ’ XData ’ , [ 2∗p i 0 ] , . . .
42 ’ YData ’ , [ f s o l v e ( q t 1 , x 0 ) −p i/ 2 ] , . . .
43 ’ S i z e ’ , [ n e w h e i g h t n e w w i d t h ] ) ;
44 e n d
unroll krogla.m
Funkcije za krogelno zrcalo:
1 f u n c t i o n [ i m g W r i t e ] = u n r o l l k r o g l a ( i m e S l i k e , x , y , a , b , c ) 2 s o u r c e v = i m r e a d ( i m e S l i k e ) ;
3 % V e l i k o s t s l i k e
4 [ h e i g h t , w i d t h ]=s i z e( s o u r c e v ) ; 5 % O d k o m e n t i r a j z a b a r v n e s l i k e 6 w i d t h = w i d t h / 3 ;
7 i f ( h e i g h t > d o u b l e ( i n t 6 4 ( 2 .∗y ) ) )
8 h e i g h t = d o u b l e ( i n t 6 4 ( 2 .∗y ) ) ; 9 e l s e
10 h e i g h t = d o u b l e ( i n t 6 4 ( 2∗( h e i g h t−y ) ) ) ;
11 e n d
12 %Premer
13 i f ( w i d t h<h e i g h t )
14 r=d o u b l e ( i n t 6 4 ( w i d t h . / 2 ) ) ; 15 e l s e
16 r=d o u b l e ( i n t 6 4 ( h e i g h t . / 2 ) ) ;
17 e n d
18 % n o v a o p t i m a l n a v e l i k o s t s l i k e , m a k s i m a l n a r e s o l u c i j a 19 n e w w i d t h=d o u b l e ( i n t 6 4 (p i∗r ) ) ;
20 n e w h e i g h t=d o u b l e ( i n t 6 4 ( r ) ) ;
21 s o u r c e=s o u r c e v ( y−r +1: y+r−1 , x−r +1: x+r−1 , : ) ; 22 % K o n s t a n t e k r o g l e .
23 g=a ;
24 d=b ;
25 f o c u s=c ; 26 f =f o c u s ;
27 % F u n k c i j e p r e s l i k a v e
28 i k s =@( x ) ( ( d .∗f . / x ) . / ( 1 + ( f . / x ) . ˆ 2 ) )−(s q r t( ( d ˆ 2 .∗( f . / x ) . ˆ 2 )−( d .ˆ2−g . ˆ 2 ) .∗ . . . 29 (1+( f . / x ) . ˆ 2 ) ) . / ( 1 + ( f . / x ) . ˆ 2 ) ) ;
30 q t=@( x ) f o c u s . / ( ( d+s q r t( g .ˆ2−i k s ( x ) . ˆ 2 ) ) . / i k s ( x ) ) ; 31 u=@( x ) q t ( x ( : , 2 ) ) .∗c o s( x ( : , 1 ) ) ;
32 v=@( x ) q t ( x ( : , 2 ) ) .∗s i n( x ( : , 1 ) ) ; 33 i p a n o r a m a=@( x ) [ u ( x ) , v ( x ) ] ; 34 i n v e r s e =@( x , t ) i p a n o r a m a ( x ) ;
35 % I s k a n j e n i c l e f u n k c i j e q t 1 z f s o l v e i n i z r a c u n s l i k e ; 36 q t 1=@( x ) ( q t ( x ) )−1 ;
37 x 0 =1;
38 t f o r m 2 = m a k e t f o r m ( ’ c u s t o m ’ , 2 , 2 , [ ] , i n v e r s e , [ ] ) ;
39 i m g W r i t e = i m t r a n s f o r m ( s o u r c e , t f o r m 2 , ’ b i c u b i c ’ , ’ UData ’ , [−1 1 ] , . . .
40 ’ VData ’ , [−1 1 ] , ’ XData ’ , [p i −p i] , . . .
41 ’ YData ’ , [ f s o l v e ( q t 1 , x 0 ) 0 ] , . . .
42 ’ S i z e ’ , [ n e w h e i g h t n e w w i d t h ] ) ;
unroll parabolic.m
Funkcije za paraboliˇ cno zrcalo:
1 f u n c t i o n [ i m g W r i t e ] = u n r o l l p a r a b o l i c ( i m e S l i k e , x , y , c )
2 %UNROLL PARABOLIC Vrne r a z v i t o p a n o r a m s k o s l i k o o m n i d i r e k c i j s k e f o t o g r a f i j e . 3 % D e t a i l e d e x p l a n a t i o n g o e s h e r e
4 s o u r c e v = i m r e a d ( i m e S l i k e ) ; 5 % V e l i k o s t s l i k e
6 [ h e i g h t , w i d t h ]=s i z e( s o u r c e v ) ; 7 % O d k o m e n t i r a j z a b a r v n e s l i k e 8 w i d t h = w i d t h / 3 ;
9 i f ( h e i g h t > d o u b l e ( i n t 6 4 ( 2 .∗y ) ) ) 10 h e i g h t = d o u b l e ( i n t 6 4 ( 2 .∗y ) ) ; 11 e l s e
12 h e i g h t = d o u b l e ( i n t 6 4 ( 2∗( h e i g h t−y ) ) ) ;
13 e n d
14 %Premer
15 i f ( w i d t h<h e i g h t )
16 r=d o u b l e ( i n t 6 4 ( w i d t h / 2 ) ) ; 17 e l s e
18 r=d o u b l e ( i n t 6 4 ( h e i g h t / 2 ) ) ;
19 e n d
20 % n o v a o p t i m a l n a v e l i k o s t s l i k e , m a k s i m a l n a r e s o l u c i j a 21 n e w w i d t h=d o u b l e ( i n t 6 4 (p i∗r ) ) ;