00001 package require image
00002 package require model3d
00003
00004
00005 namespace eval stereo {
00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020
00021
00022 proc stereo_correlation {
00023 {image_dir "/home/ayman/images/dala/indoor/serie05/"}
00024 {image_name "image.037"}
00025 {type_rectification 1}
00026 {filtrage 0}
00027 {type_correlation 2}
00028 {type_interpolation 1}
00029 {window_size 11}
00030 {i_min 0.0}
00031 {i_max 1.0}
00032 {j_min 0.0}
00033 {j_max 1.0}
00034 {do_display true}
00035 {delcorrelimages true}
00036 {debayer false}
00037 } {
00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078
00079
00080
00081 00082 00083 00084 00085
00086
00087 00088 00089 00090 set oldPath [pwd]
00091 cd $image_dir
00092
00093
00094 00095 set param_stereo_file "$image_dir/param.stereo"
00096 set calibrageMatrices [::stereo::new_CalibrageMatrices]
00097 ::stereo::CalibrageMatrices_readFromFile $calibrageMatrices \
00098 $param_stereo_file
00099
00100 00101 00102 00103 00104 if { [file exists $image_dir/$image_name.d] } then {
00105 set separateur "."
00106 set extension ""
00107 } elseif { [file exists $image_dir/$image_name.d.gz] } then {
00108 set separateur "."
00109 set extension ".gz"
00110 } elseif { [file exists $image_dir/$image_name.d.Z] } then {
00111 set separateur "."
00112 set extension ""
00113 } elseif { [file exists $image_dir/$image_name.d.tif] } then {
00114 set separateur "."
00115 set extension ".tif"
00116 } elseif { [file exists $image_dir/$image_name.d.tif.gz] } then {
00117 set separateur "."
00118 set extension ".tif.gz"
00119 } elseif { [file exists $image_dir/$image_name.d.tiff] } then {
00120 set separateur "."
00121 set extension ".tiff"
00122 } elseif { [file exists $image_dir/$image_name.d.tiff.gz] } then {
00123 set separateur "."
00124 set extension ".tiff.gz"
00125 } elseif { [file exists $image_dir/$image_name.d.ras] } then {
00126 set separateur "."
00127 set extension ".ras"
00128 } elseif { [file exists $image_dir/$image_name.d.ras.gz] } then {
00129 set separateur "."
00130 set extension ".ras.gz"
00131 } elseif { [file exists $image_dir/$image_name-d] } then {
00132 set separateur "-"
00133 set extension ""
00134 } elseif { [file exists $image_dir/$image_name-d.gz] } then {
00135 set separateur "-"
00136 set extension ".gz"
00137 } elseif { [file exists $image_dir/$image_name.d.Z] } then {
00138 set separateur "."
00139 set extension ".Z"
00140 } elseif { [file exists $image_dir/$image_name.d.jpeg] } then {
00141 set separateur "."
00142 set extension ".jpeg"
00143 } elseif { [file exists $image_dir/$image_name-d.tif] } then {
00144 set separateur "-"
00145 set extension ".tif"
00146 } elseif { [file exists $image_dir/$image_name-d.tif.gz] } then {
00147 set separateur "-"
00148 set extension ".tif.gz"
00149 } elseif { [file exists $image_dir/$image_name-d.ras] } then {
00150 set separateur "-"
00151 set extension ".ras"
00152 } elseif { [file exists $image_dir/$image_name-d.ras.gz] } then {
00153 set separateur "-"
00154 set extension ".ras.gz"
00155 } elseif { [file exists $image_dir/${image_name}_d] } then {
00156 set separateur "_"
00157 set extension ""
00158 } elseif { [file exists $image_dir/${image_name}_d.gz] } then {
00159 set separateur "_"
00160 set extension ".gz"
00161 } elseif { [file exists $image_dir/$image_name.d.Z] } then {
00162 set separateur "."
00163 set extension ".Z"
00164 } elseif { [file exists $image_dir/${image_name}_d.tif] } then {
00165 set separateur "_"
00166 set extension ".tif"
00167 } elseif { [file exists $image_dir/${image_name}_d.tif.gz] } then {
00168 set separateur "_"
00169 set extension ".tif.gz"
00170 } elseif { [file exists $image_dir/${image_name}_d.ras] } then {
00171 set separateur "_"
00172 set extension ".ras"
00173 } elseif { [file exists $image_dir/${image_name}_d.ras.gz] } then {
00174 set separateur "_"
00175 set extension ".ras.gz"
00176 } else {
00177 puts "Fichiers image non trouvs!!"
00178 return
00179 }
00180
00181 puts "loading of image : ${image_dir}${image_name}${separateur}g$extension "
00182 set source_image_gauche [image::Image_loadImage \
00183 ${image_dir}${image_name}${separateur}g$extension]
00184 puts "loading of image : ${image_dir}${image_name}${separateur}d$extension "
00185 set source_image_droite [image::Image_loadImage \
00186 ${image_dir}${image_name}${separateur}d$extension]
00187 if { $debayer } {
00188 set interm [image::new_Image [$source_image_gauche width] \
00189 [$source_image_gauche height] \
00190 [$source_image_gauche img_depth] \
00191 $::image::JfrImage_CS_BGR]
00192 image::Image_convertColorMode $source_image_gauche $interm \
00193 $::image::Image_bayerRG2bgr
00194 image::Image_convertColorMode $interm $source_image_gauche \
00195 $::image::Image_rgb2grey
00196 image::Image_convertColorMode $source_image_droite $interm \
00197 $::image::Image_bayerRG2bgr
00198 image::Image_convertColorMode $interm $source_image_droite \
00199 $::image::Image_rgb2grey
00200 image::delete_Image $interm
00201 }
00202
00203 set im3d [_stereo_correlation $source_image_gauche \
00204 $source_image_droite $calibrageMatrices \
00205 $type_rectification $filtrage $type_correlation \
00206 $type_interpolation $window_size \
00207 $i_min $i_max $j_min $j_max $do_display $delcorrelimages ]
00208
00209 ::stereo::delete_CalibrageMatrices $calibrageMatrices
00210 ::image::delete_Image $source_image_gauche
00211 ::image::delete_Image $source_image_droite
00212
00213 00214 cd $oldPath
00215
00216 ::model3d::Image3D_minMax $im3d
00217 ::model3d::Image3D_info $im3d
00218
00219 return $im3d
00220 }00221
00222
00223 proc _stereo_correlation {
00224 { source_image_gauche }
00225 { source_image_droite }
00226 { calibrageMatrices }
00227 {type_rectification 1}
00228 {filtrage 0}
00229 {type_correlation 0}
00230 {type_interpolation 1}
00231 {window_size 11}
00232 {i_min 0.0}
00233 {i_max 1.0}
00234 {j_min 0.0}
00235 {j_max 1.0}
00236 {do_display true}
00237 {delcorrelimages true}
00238 } {
00239
00240 set LEFT_IMAGE_ID 0
00241 set RIGHT_IMAGE_ID 1
00242
00243 00244 00245 00246 set do_reduction 1;
00247
00248 00249 00250 00251 set type_reduction 0;
00252
00253 00254 00255 00256 set do_interpolation 1;
00257
00258 00259 00260 00261 00262 00263 00264 00265 00266 set distorsion_type 2;
00267
00268 00269
00270 set disp_min -100;
00271 set disp_max 0;
00272
00273 00274 00275 00276 00277 00278 00279 00280 00281 set type_census 4;
00282
00283 00284 00285 00286 set do_blob_filter 1;
00287
00288 set th_filter_disparity1 500
00289 set th_filter_disparity2 1.0
00290
00291 00292 00293 00294 set type_repere 0;
00295
00296
00297 00298 if {$filtrage == 1} {
00299 set size_low 3;
00300 set size_high 7;
00301 set coefficient 2;
00302 }
00303
00304 00305 00306 00307 00308 00309 00310 set th_sigma 1.0; 00311 set th_score 0.5; 00312 set th_delta 0.05; 00313 set th_voisins 0.0; 00314 set espilon_inverse 1; 00315 00316
00317
00318 00319 00320 global im_reduite_gauche
00321 00322 00323 global im_reduite_droite
00324 00325 00326 00327 00328 global correlation_results
00329
00330
00331
00332 00333 if { $do_display } {
00334 ::display::show $source_image_gauche "image source gauche"
00335 #::display::show $source_image_droite "image source droite"
00336 }
00337
00338
00339 00340 00341 set nbcol [::image::CvImage_width $source_image_gauche]
00342 set nblig [::image::CvImage_height $source_image_gauche]
00343
00344
00345 00346 set images_sizes [::stereo::new_ImagesSizes]
00347 ::stereo::ImagesSizes_initialize $images_sizes \
00348 $nblig $nbcol 1 1 \
00349 $i_min $i_max $j_min $j_max
00350
00351 puts "Info ImagesSizes before reinitialize"
00352 ::stereo::ImagesSizes_info $images_sizes
00353 puts "============================"
00354
00355
00356
00357 00358 ::stereo::ImagesSizes_initialize $images_sizes $calibrageMatrices
00359
00360 puts "Info ImagesSizes after reinitialize"
00361 ::stereo::ImagesSizes_info $images_sizes
00362 puts "============================"
00363 puts "============================"
00364 puts "============================"
00365
00366
00367 00368 set correlation_params [::stereo::new_CorrelationParams]
00369 ::stereo::CorrelationParams_initialize $correlation_params \
00370 $type_correlation $type_interpolation \
00371 $window_size $window_size $disp_min $disp_max \
00372 $th_sigma $th_score $th_delta $th_voisins \
00373 $espilon_inverse
00374
00375 ::stereo::CorrelationParams_info $correlation_params
00376
00377 00378 set correlation_results [::stereo::new_CorrelationResults]
00379 ::stereo::CorrelationResults_initialize $correlation_results \
00380 $images_sizes $correlation_params
00381
00382 ::stereo::CorrelationResults_info $correlation_results
00383
00384
00385 00386 set images_tmp_g [::stereo::new_ImagesTemporary]
00387 ::stereo::ImagesTemporary_initialize $images_tmp_g $images_sizes
00388
00389 set images_tmp_d [::stereo::new_ImagesTemporary]
00390 ::stereo::ImagesTemporary_initialize $images_tmp_d $images_sizes
00391
00392 set rectif_params_droite [::stereo::new_RectificationParams]
00393 ::stereo::RectificationParams_initialize $rectif_params_droite \
00394 $images_sizes $calibrageMatrices $RIGHT_IMAGE_ID
00395
00396
00397 set rectif_params_gauche [::stereo::new_RectificationParams]
00398 ::stereo::RectificationParams_initialize $rectif_params_gauche \
00399 $images_sizes $calibrageMatrices $LEFT_IMAGE_ID
00400
00401
00402 00403 if {$type_rectification == 2} {
00404 ::stereo::RectificationParams_initializeTables_michel \
00405 $rectif_params_gauche $distorsion_type
00406 ::stereo::RectificationParams_initializeTables_michel \
00407 $rectif_params_droite $distorsion_type
00408 }
00409
00410
00411 00412 set reconstruction_params [::stereo::new_ReconstructionParams]
00413 ::stereo::ReconstructionParams_initialize \
00414 $reconstruction_params $calibrageMatrices
00415
00416
00417
00418 00419 00420 00421 00422 set nRectifiedImageWidth \
00423 [::stereo::ImagesSizes_getRectifiedImageWidth $images_sizes]
00424 set nRectifiedImageHeight \
00425 [::stereo::ImagesSizes_getRectifiedImageHeight $images_sizes]
00426
00427 set img_depth $::image::IPL_DEPTH_8U
00428 set color_space $::image::JfrImage_CS_GRAY
00429
00430 set im_reduite_gauche [::image::new_Image \
00431 $nRectifiedImageWidth \
00432 $nRectifiedImageHeight\
00433 $img_depth \
00434 $color_space]
00435
00436
00437 set im_reduite_droite [::image::new_Image \
00438 $nRectifiedImageWidth \
00439 $nRectifiedImageHeight\
00440 $img_depth \
00441 $color_space]
00442
00443
00444 ::stereo::jfrRectification $source_image_gauche $im_reduite_gauche \
00445 $images_sizes $rectif_params_gauche \
00446 $do_reduction $type_rectification\
00447 $distorsion_type $do_interpolation
00448
00449
00450 ::stereo::jfrRectification $source_image_droite $im_reduite_droite \
00451 $images_sizes $rectif_params_droite \
00452 $do_reduction $type_rectification\
00453 $distorsion_type $do_interpolation
00454
00455 00456 if { $do_display } {
00457 ::display::show $im_reduite_gauche "image reduite gauche"
00458 ::display::show $im_reduite_droite "image reduite droite"
00459 }
00460
00461
00462 00463 00464 if {$filtrage == 1} {
00465 ::stereo::laplacien_ima1 $im_reduite_gauche $images_tmp_g \
00466 $size_low $size_low $size_high $size_high $coefficient
00467 ::stereo::laplacien_ima1 $im_reduite_droite $images_tmp_d \
00468 $size_low $size_low $size_high $size_high $coefficient
00469 }
00470
00471
00472
00473 00474 00475 if { [expr {$type_correlation == 0} || {$type_correlation == 1}] } {
00476 # ZNCC: calcul des moyennes et sigmas
00477 ::stereo::jfrMoyennesSigmas $im_reduite_gauche \
00478 $images_tmp_g $correlation_params
00479 ::stereo::jfrMoyennesSigmas $im_reduite_droite \
00480 $images_tmp_d $correlation_params
00481
00482 } else { # Census
00483 if {[expr {$type_census == 3} || \
00484 {$type_census == 4} || \
00485 {$type_census == 5} || \
00486 {$type_census == 6}]} {
00487 # Il faut calculer l'images des moyennes
00488 ::stereo::jfrMoyennes $im_reduite_gauche 3 3 $images_tmp_g
00489 ::stereo::jfrMoyennes $im_reduite_droite 3 3 $images_tmp_d
00490 }
00491
00492 # Creation des images de Census
00493 set im_census_gauche [::image::Image im_census_gauche \
00494 $nRectifiedImageWidth \
00495 $nRectifiedImageHeight\
00496 $img_depth \
00497 $color_space]
00498
00499 set im_census_droite [::image::Image im_census_droite \
00500 $nRectifiedImageWidth \
00501 $nRectifiedImageHeight\
00502 $img_depth \
00503 $color_space]
00504
00505 ::stereo::jfrTransform_census_8bits \
00506 $im_reduite_gauche $images_tmp_g \
00507 $type_census $im_census_gauche
00508
00509 ::stereo::jfrTransform_census_8bits \
00510 $im_reduite_droite $images_tmp_d \
00511 $type_census $im_census_droite
00512 }
00513
00514
00515
00516
00517 00518 00519
00520 if {[expr {$type_correlation == 0} || {$type_correlation == 1}]} {
00521 # ZNCC
00522 ::stereo::jfrCorrelationZncc \
00523 $im_reduite_gauche $im_reduite_droite \
00524 $images_tmp_g $images_tmp_d \
00525 $correlation_params $correlation_results
00526 } else {
00527 # Census
00528 ::stereo::jfrCorrelationCensus \
00529 $im_census_gauche $im_census_droite \
00530 $images_sizes $correlation_params $correlation_results
00531 }
00532
00533
00534 00535 set im_disparity \
00536 [::stereo::CorrelationResults_getImageForDisparity \
00537 $correlation_results]
00538
00539 if { $do_display } {
00540 # ::display::show $im_disparity "image of not filtered disparity "
00541 }
00542
00543 00544
00545 00546 00547 if {$do_blob_filter != 0} {
00548 ::stereo::jfrBlobFilterDisparites $correlation_results \
00549 $th_filter_disparity1 $th_filter_disparity2
00550 }
00551
00552 00553 00554 set im_disparity_fltr \
00555 [::stereo::CorrelationResults_getImageForDisparity \
00556 $correlation_results]
00557
00558 if { $do_display } {
00559 ::display::show $im_disparity_fltr "image of filtered disparity "
00560 }
00561
00562 00563 set im_correlated_pixels \
00564 [::stereo::CorrelationResults_getImageOfCorrelatedPixels \
00565 $correlation_results $im_reduite_gauche]
00566 if { $do_display } {
00567 ::display::show $im_correlated_pixels "image of correlated pixels"
00568 }
00569
00570 00571 set im_regions \
00572 [::stereo::CorrelationResults_getImageForRegions \
00573 $correlation_results]
00574 if { $do_display } {
00575 # ::display::show $im_regions "image of regions"
00576 }
00577
00578 00579 00580 00581
00582 if {[expr {$type_correlation == 1} || {$type_correlation == 3}]} {
00583 set type_reconstruction 1
00584 } else { set type_reconstruction 0 }
00585
00586
00587 00588 set im3d [::model3d::Image3D]
00589 ::model3d::Image3D_initialize $im3d \
00590 $nRectifiedImageWidth $nRectifiedImageHeight
00591
00592
00593 ::stereo::jfrReconstruction3D $correlation_results \
00594 $images_sizes $type_repere \
00595 $type_reconstruction $reconstruction_params \
00596 $rectif_params_gauche $im3d
00597
00598
00599 ::model3d::Image3D_minMax $im3d
00600
00601
00602 00603 ::stereo::delete_ImagesSizes $images_sizes
00604 00605 ::stereo::delete_CorrelationParams $correlation_params
00606 00607 ::stereo::delete_ImagesTemporary $images_tmp_g
00608 ::stereo::delete_ImagesTemporary $images_tmp_d
00609 ::stereo::delete_RectificationParams $rectif_params_droite
00610 ::stereo::delete_RectificationParams $rectif_params_gauche
00611 ::stereo::delete_ReconstructionParams $reconstruction_params
00612 if { $delcorrelimages} {
00613 ::image::delete_Image $im_reduite_gauche
00614 ::image::delete_Image $im_reduite_droite
00615 }
00616
00617 ::image::delete_Image $im_correlated_pixels
00618 ::image::delete_Image $im_disparity
00619
00620 return $im3d
00621 }00622
00623 }00624
00625 package provide stereo 0.1