2 # @file bshow_image.tcl
4 # @brief Procedures for manipulating images in Bshow.
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20191001
11 set show_mont_labels 1
12 set show_mont_select 1
24 ## @brief Menu for manipulating image files
27 proc setupImageMenu {} {
28 .menuBar add cascade -menu .menuBar.image -label "Image" -underline 0
29 menu .menuBar.image -tearoff 0
30 .menuBar.image add command -label "Set scales" -underline 0 \
31 -command { setScaleDialog } -accelerator "Ctrl-z"
32 .menuBar.image add command -label "Switch slices and images" -underline 0 \
33 -command { switchImage }
34 .menuBar.image add command -label "Modify image" -underline 0 \
35 -command { modifyImage }
37 menu .menuBar.image.dt -tearoff 0
38 .menuBar.image add cascade -menu .menuBar.image.dt -label "Change data type" -underline 0
39 .menuBar.image.dt add command -label "Unsigned char (byte)" -underline 0 \
40 -command { changeDataType byte } -accelerator "Ctrl-z"
41 .menuBar.image.dt add command -label "Signed char" -underline 0 \
42 -command { changeDataType char } -accelerator "Ctrl-z"
43 .menuBar.image.dt add command -label "Unsigned short" -underline 0 \
44 -command { changeDataType "unsigned short" } -accelerator "Ctrl-z"
45 .menuBar.image.dt add command -label "Signed short" -underline 0 \
46 -command { changeDataType short } -accelerator "Ctrl-z"
47 .menuBar.image.dt add command -label "Unsigned int" -underline 0 \
48 -command { changeDataType "unsigned int" } -accelerator "Ctrl-z"
49 .menuBar.image.dt add command -label "Signed int" -underline 0 \
50 -command { changeDataType int } -accelerator "Ctrl-z"
51 .menuBar.image.dt add command -label "Unsigned long" -underline 0 \
52 -command { changeDataType "unsigned long" } -accelerator "Ctrl-z"
53 .menuBar.image.dt add command -label "Signed long" -underline 0 \
54 -command { changeDataType long } -accelerator "Ctrl-z"
55 .menuBar.image.dt add command -label "Float" -underline 0 \
56 -command { changeDataType float } -accelerator "Ctrl-z"
57 .menuBar.image.dt add command -label "Double" -underline 0 \
58 -command { changeDataType double } -accelerator "Ctrl-z"
60 .menuBar.image add command -label "Fix type sign" -underline 0 \
62 .menuBar.image add command -label "Set origin" -underline 0 \
63 -command { setOrigin }
64 .menuBar.image add command -label "Center origin" -underline 0 \
65 -command { centerOrigin }
66 .menuBar.image add command -label "Shift origin to zero" -underline 0 \
67 -command { zeroOrigin }
68 .menuBar.image add command -label "Crop" -underline 0 \
69 -command { cropImage }
70 .menuBar.image add command -label "Pad" -underline 0 \
72 .menuBar.image add command -label "Bin" -underline 0 \
74 .menuBar.image add command -label "Montage" -underline 0 \
76 .menuBar.image add command -label "Invert" -underline 0 \
77 -command { invertImage }
78 .menuBar.image add command -label "Histogram" -underline 0 \
79 -command { Histogram }
80 .menuBar.image add command -label "Filter" -underline 0 \
82 .menuBar.image add command -label "Fourier transform" -underline 0 \
83 -command { FourierTransform -1 }
84 .menuBar.image add command -label "Fourier back transform" -underline 0 \
85 -command { FourierTransform 1 }
86 .menuBar.image add command -label "Power spectrum" -underline 0 \
87 -command { PowerSpectrum }
88 .menuBar.image add command -label "Diffraction" -underline 0 \
89 -command { Diffraction }
94 ## @brief Switches the images and slices.
97 proc switchImage { } {
104 ## @brief Sets the image type.
107 proc setImageType { } {
110 if ![winfo exists $w] {
113 radiobutton $w.notdef -text "Not defined" -variable imgtype -value ""
114 radiobutton $w.mg -text "Micrograph(s)" -variable imgtype -value "mg"
115 radiobutton $w.frame -text "Frame(s)" -variable imgtype -value "frame"
116 radiobutton $w.part -text "Particle(s)" -variable imgtype -value "part"
117 radiobutton $w.fil -text "Filament(s)" -variable imgtype -value "fil"
118 radiobutton $w.ft -text "Fourier transform(s)" -variable imgtype -value "ft"
119 radiobutton $w.ps -text "Power spectrum(s)" -variable imgtype -value "ps"
120 radiobutton $w.rec -text "Reconstruction(s)" -variable imgtype -value "rec"
123 button $w.buttons.ok -text OK -command "destroy $w"
125 pack $w.notdef $w.mg $w.frame $w.part $w.fil $w.ft \
126 $w.ps $w.rec $w.buttons -side top -fill x -pady 5
127 pack $w.buttons.ok -side left -expand 1
129 bind $w <Return> "destroy $w"
134 wm title $w "Image type"
135 wm iconname $w "Image type"
140 ## @brief Switchs the sign convention for the data type.
145 Bimage fixtype $theimg
150 ## @brief Sets the image origin.
157 set ori [Bimage get $theimg origin]
162 wm title $w "Set origin"
163 wm iconname $w "Origin"
165 label $w.msg -font $helv12 -wraplength 4i -justify left \
166 -text {Selecting OK sets the image origin}
167 pack $w.msg -side top -padx 5 -pady 5
169 setupVectorEntry $w.coor "Coordinates" double $ori "The image origin coordinates"
170 pack $w.coor -side top -fill x -pady 2m
173 button $w.buttons.center -text Center -command "setImageOrigin $w 1"
174 button $w.buttons.ok -text OK -command "setImageOrigin $w 0"
175 button $w.buttons.cancel -text Cancel -command "destroy $w"
177 pack $w.buttons -side bottom -fill x -pady 2m
178 pack $w.buttons.center $w.buttons.ok $w.buttons.cancel -side left -expand 1
180 bind $w <Return> "setImageOrigin $w 0"
183 ## @brief Sets the image origin.
186 # @param t 0=coordinates, 1=center.
188 proc setImageOrigin { w t } {
191 set ox [expr [Bimage get $theimg width] / 2]
192 set oy [expr [Bimage get $theimg height] / 2]
193 set oz [expr [Bimage get $theimg nslices] / 2]
195 set ox [$w.coor.x get]
196 set oy [$w.coor.y get]
197 set oz [$w.coor.z get]
199 Bimage set $theimg origin $ox $oy $oz
205 ## @brief Gets the x origin in canvas coordinates.
207 # @param c Canvas object.
209 proc getOriginX { c } {
211 set wc [getControlWindow $theimg]
212 set scale [$wc.scale.scale get]
213 set origin [Bimage get $theimg origin]
214 return [expr $scale * [lindex $origin 0]]
217 ## @brief Gets the y origin in canvas coordinates.
219 # @param c Canvas object.
221 proc getOriginY { c } {
223 set wc [getControlWindow $theimg]
224 set h [lindex [$c cget -scrollregion] 3]
225 set scale [$wc.scale.scale get]
226 set origin [Bimage get $theimg origin]
227 return [expr $h - $scale * ([lindex $origin 1] + 1)]
230 ## @brief Centers the image origin with wrapping.
233 proc centerOrigin { } {
236 if ![Bimage exists $theimg] { return }
237 Bimage center $theimg
238 set origin [Bimage get $theimg origin]
240 Bmg set $project_item origin [lindex $origin 0] [lindex $origin 1] [lindex $origin 2]
245 ## @brief Shifts the image origin to zero {0,0,0} with wrapping.
248 proc zeroOrigin { } {
251 if ![Bimage exists $theimg] { return }
252 Bimage zero_origin $theimg
253 set origin [Bimage get $theimg origin]
255 Bmg set $project_item origin [lindex $origin 0] [lindex $origin 1] [lindex $origin 2]
260 ## @brief Extracts part of an image.
263 proc extractImage { } {
269 global sx1 sx2 sy1 sy2 sz1 sz2
270 if ![Bimage exists $theimg] { return }
271 set wc [getControlWindow $theimg]
272 set x [Bimage get $theimg width]
273 set y [Bimage get $theimg height]
274 set z [$wc.slice.scale get]
275 set vlist "0 0 $z $x $y $z"
276 if { [winfo exists .wsel] && $tool == "select" } {
277 if { $sx1 != $sx2 || $sy1 != $sy2 } {
278 set vlist "$sx1 $sy1 $sz1 $sx2 $sy2 $sz2"
282 set img_num [$wc.image.scale get]
284 if { [string length $currdir] < 2 } { set currdir [pwd] }
285 set extfile [tk_getSaveFile -filetypes $filetypes \
286 -initialfile [file tail $filename] -initialdir $currdir -defaultextension .mrc]
287 if { [string length $extfile] < 1 } { return }
288 Bimage extract $theimg $extfile $img_num $vlist
291 ## @brief Crops an image to a smaller size.
297 global sx1 sx2 sy1 sy2 sz1 sz2
298 if ![Bimage exists $theimg] { return }
299 set wc [getControlWindow $theimg]
300 set img_num [$wc.image.scale get]
301 set x [Bimage get $theimg width]
302 set y [Bimage get $theimg height]
303 set z [$wc.slice.scale get]
304 set vlist "0 0 $z $x $y $z"
305 if { [winfo exists .wsel] && $tool == "select" } {
306 set vlist "$sx1 $sy1 $sz1 $sx2 $sy2 $sz2"
315 Bimage crop $theimg $img_num $vlist
320 ## @brief Pads an image to a larger size.
327 set bkg [Bimage get $theimg background]
331 wm title $w "Pad image"
334 label $w.msg -font $helv12 -wraplength 4i -justify left \
335 -text {Setting these options modify the internal image on selecting OK}
336 pack $w.msg -side top -padx 5 -pady 5
341 label $w.fill.tag -font $helv12 -text "Fill type" -width 12
342 radiobutton $w.fill.avg -text "Average" -variable filltype -value 1
343 radiobutton $w.fill.back -text "Background" -variable filltype -value 2
344 radiobutton $w.fill.val -text "Value" -variable filltype -value 0
345 entry $w.fill.entry -width 6 -validate key -vcmd { string is double %P }
346 $w.fill.entry insert 0 $bkg
348 pack $w.fill.tag $w.fill.avg $w.fill.back $w.fill.val \
349 $w.fill.entry -side left -fill both -expand true
354 setupVectorEntry $w.start "Translation" int $start "Translation in pixels (can be negative)"
355 setupVectorEntry $w.end "Size" int $end "Final size in pixels"
356 pack $w.start $w.end $w.fill -side top -padx 5 -pady 5
359 button $w.buttons.ok -text OK -command "padImage $w"
360 button $w.buttons.cancel -text Cancel -command "destroy $w"
362 pack $w.buttons -side bottom -fill x -pady 2m
363 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
365 bind $w <Return> "padImage $w"
368 ## @brief Pads the image.
370 # @param w the window to destroy.
372 proc padImage { w } {
375 set xm [$w.start.x get]
376 set xp [$w.end.x get]
377 set ym [$w.start.y get]
378 set yp [$w.end.y get]
379 set zm [$w.start.z get]
380 set zp [$w.end.z get]
381 set fill [$w.fill.entry get]
383 Bimage pad $theimg $xm $ym $zm $xp $yp $zp $filltype $fill
388 ## @brief Invokes a dialog box with modifyable image parameters.
391 proc modifyImage { } {
395 set datatype [Bimage get $theimg datatype]
396 set origin [Bimage get $theimg origin]
397 set nimages [Bimage get $theimg nimages]
398 set nslices [Bimage get $theimg nslices]
402 wm title $w "Modify image"
403 wm iconname $w "Modify"
405 label $w.msg -font $helv12 -wraplength 4i -justify left \
406 -text {Setting these options modify the internal image on selecting OK}
409 frame $w.panel.datatype
410 label $w.panel.datatype.tag -text "Data type"
411 tk_optionMenu $w.panel.datatype.menu datatype "unknown" "unsigned char (byte)" \
412 "signed char" "unsigned short" "short" "unsigned int" "int" \
413 "unsigned long" "long" "float" "double"
415 frame $w.panel.origin
416 label $w.panel.origin.tag -font $helv12 -text "Origin"
417 entry $w.panel.origin.entryx -width 6 -validate key -vcmd { string is double %P }
418 entry $w.panel.origin.entryy -width 6 -validate key -vcmd { string is double %P }
419 entry $w.panel.origin.entryz -width 6 -validate key -vcmd { string is double %P }
420 $w.panel.origin.entryx insert 0 [lindex $origin 0]
421 $w.panel.origin.entryy insert 0 [lindex $origin 1]
422 $w.panel.origin.entryz insert 0 [lindex $origin 2]
424 frame $w.panel.reslice
425 label $w.panel.reslice.tag -font $helv12 -text "Reslice"
426 entry $w.panel.reslice.entry -width 10
427 $w.panel.reslice.entry insert 0 "xyz"
429 frame $w.panel.rotate
430 label $w.panel.rotate.tagpsi -font $helv12 -text "Rotate: psi"
431 label $w.panel.rotate.tagtheta -font $helv12 -text "theta"
432 label $w.panel.rotate.tagphi -font $helv12 -text "phi"
433 entry $w.panel.rotate.entrypsi -width 4 -validate key -vcmd { string is double %P }
434 entry $w.panel.rotate.entrytheta -width 4 -validate key -vcmd { string is double %P }
435 entry $w.panel.rotate.entryphi -width 4 -validate key -vcmd { string is double %P }
436 $w.panel.rotate.entrypsi insert 0 "0"
437 $w.panel.rotate.entrytheta insert 0 "0"
438 $w.panel.rotate.entryphi insert 0 "0"
440 frame $w.panel.truncate
441 label $w.panel.truncate.tagmin -font $helv12 -text "Truncate: min"
442 label $w.panel.truncate.tagmax -font $helv12 -text "max"
443 entry $w.panel.truncate.entrymin -width 6 -validate key -vcmd { string is double %P }
444 entry $w.panel.truncate.entrymax -width 6 -validate key -vcmd { string is double %P }
445 $w.panel.truncate.entrymin insert 0 [Bimage get $theimg min]
446 $w.panel.truncate.entrymax insert 0 [Bimage get $theimg max]
448 frame $w.panel.rescale
449 label $w.panel.rescale.tagavg -font $helv12 -text "Rescale: avg"
450 label $w.panel.rescale.tagstd -font $helv12 -text "std"
451 entry $w.panel.rescale.entryavg -width 6 -validate key -vcmd { string is double %P }
452 entry $w.panel.rescale.entrystd -width 6 -validate key -vcmd { string is double %P }
453 $w.panel.rescale.entryavg insert 0 [Bimage get $theimg average]
454 $w.panel.rescale.entrystd insert 0 [Bimage get $theimg standard_deviation]
457 button $w.buttons.ok -text OK -command "modifyImage_and_DestroyWindow $w"
458 button $w.buttons.cancel -text Cancel -command "destroy $w"
460 pack $w.msg -side top -padx 5 -pady 5
461 pack $w.panel -side top -fill both
462 pack $w.panel.datatype $w.panel.origin $w.panel.reslice $w.panel.rotate \
463 $w.panel.truncate $w.panel.rescale -side top -fill both
465 pack $w.panel.datatype.tag $w.panel.datatype.menu \
466 -side left -padx 5 -pady 5
467 pack $w.panel.origin.tag $w.panel.origin.entryx $w.panel.origin.entryy \
468 $w.panel.origin.entryz -side left -padx 5 -pady 5
469 pack $w.panel.reslice.tag $w.panel.reslice.entry -side left -padx 5 -pady 5
470 pack $w.panel.rotate.tagpsi $w.panel.rotate.entrypsi $w.panel.rotate.tagtheta \
471 $w.panel.rotate.entrytheta $w.panel.rotate.tagphi $w.panel.rotate.entryphi \
472 -side left -padx 5 -pady 5
473 pack $w.panel.truncate.tagmin $w.panel.truncate.entrymin \
474 $w.panel.truncate.tagmax $w.panel.truncate.entrymax -side left -padx 5 -pady 5
475 pack $w.panel.rescale.tagavg $w.panel.rescale.entryavg \
476 $w.panel.rescale.tagstd $w.panel.rescale.entrystd -side left -padx 5 -pady 5
477 pack $w.buttons -side bottom -fill x -pady 2m
478 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
480 bind $w <Return> "modifyImage_and_DestroyWindow $w"
483 ## @brief Modifies the image.
485 # @param w the window to destroy.
487 proc modifyImage_and_DestroyWindow { w } {
489 global datatype project_item
492 set ox [$w.panel.origin.entryx get]
493 set oy [$w.panel.origin.entryy get]
494 set oz [$w.panel.origin.entryz get]
495 set order [$w.panel.reslice.entry get]
496 set psi [$w.panel.rotate.entrypsi get]
497 set theta [$w.panel.rotate.entrytheta get]
498 set phi [$w.panel.rotate.entryphi get]
499 set min [$w.panel.truncate.entrymin get]
500 set max [$w.panel.truncate.entrymax get]
501 if { [expr abs($min - [Bimage get $theimg min])] > 0.001 \
502 || [expr abs($max - [Bimage get $theimg max])] > 0.001 } {
505 set avg [$w.panel.rescale.entryavg get]
506 set std [$w.panel.rescale.entrystd get]
507 if { [expr abs($avg - [Bimage get $theimg average])] > 0.001 \
508 || [expr abs($std - [Bimage get $theimg standard_deviation])] > 0.001 } {
512 Bimage set $theimg origin $ox $oy $oz
513 if [Bmg exists] { Bmg set $project_item origin $ox $oy $oz }
514 Bimage reslice $theimg $order
515 Bimage rotate $theimg $psi $theta $phi
517 Bimage truncate $theimg $min $max
520 Bimage rescale $theimg $avg $std
522 set olddatatype [Bimage get $theimg datatype]
523 if { $datatype != $olddatatype } {
524 Bimage set $theimg datatype $datatype
530 ## @brief This chnages the data type.
533 proc changeDataType { datatype } {
535 set olddatatype [Bimage get $theimg datatype]
536 if { $datatype != $olddatatype } {
537 Bimage set $theimg datatype $datatype
543 ## @brief This inverts the image.
546 proc invertImage {} {
548 Bimage invert $theimg
553 ## @brief This invokes a dialog box with binning parameters.
563 wm title $w "Bin image"
568 label $w.msg -justify left \
569 -text {Setting these options modify the internal image on selecting OK}
570 pack $w.msg -side top -padx 5 -pady 5
572 setupVectorEntry $w.bin "Binning" int $bin "The level of binning"
575 button $w.buttons.ok -text OK -command "binImage $w"
576 button $w.buttons.cancel -text Cancel -command "destroy $w"
577 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
579 pack $w.msg $w.bin $w.buttons -side top -pady 2m
581 bind $w <Return> "binImage $w"
584 ## @brief This inverts the image.
587 proc binImage { w } {
589 set bx [$w.bin.x get]
590 set by [$w.bin.y get]
591 set bz [$w.bin.z get]
593 Bimage bin $theimg $bx $by $bz
599 ## @brief This invokes a dialog box with filter parameters.
603 global theimg filter_type
604 global helv12 kernel avg_check
609 wm title $w "Filter image"
610 wm iconname $w "Filter"
612 label $w.msg -justify left \
613 -text {Setting these options modify the internal image on selecting OK}
614 pack $w.msg -side top -padx 5 -pady 5
617 label $w.type.l -font $helv12 -text "Type" -bg orange
618 tk_optionMenu $w.type.opt filter_type "Averaging" "Variance" "Normalizing" "Gaussian"
619 pack $w.type.l $w.type.opt -side left -ipadx 2
621 setupEntry $w.kernel "Kernel" integer $kernel "The size of the filter kernel"
624 button $w.buttons.pref -text "Set preference" -command { set avg_check 1 }
625 button $w.buttons.ok -text Filter -command "filterImage $w"
626 button $w.buttons.cancel -text Cancel -command "destroy $w"
627 pack $w.buttons.pref $w.buttons.ok $w.buttons.cancel -side left -expand 1
629 pack $w.msg $w.type $w.kernel $w.buttons -side top -pady 2m
631 bind $w <Return> "filterImage $w"
634 ## @brief This filters the image.
637 proc filterImage { w } {
638 global theimg filter_type kernel
639 set kernel [$w.kernel.e get]
641 if { [string compare $filter_type "Averaging"] == 0 } {
642 Bimage average $theimg $kernel $kernel $kernel
643 } elseif { [string compare $filter_type "Variance"] == 0 } {
644 Bimage variance $theimg $kernel $kernel $kernel
645 } elseif { [string compare $filter_type "Normalizing"] == 0 } {
646 Bimage normalize $theimg $kernel $kernel $kernel
647 } elseif { [string compare $filter_type "Gaussian"] == 0 } {
648 set sigma [expr $kernel/6.0]
649 puts "Gaussian filter: $sigma"
650 Bimage gaussian $theimg $kernel $sigma
656 ## @brief This invokes a dialog box with montage parameters.
661 global helv12 font_size selected_color
663 set bkg [Bimage get $theimg background]
667 wm title $w "Montage image"
668 wm iconname $w "Montage"
670 label $w.msg -font $helv12 -wraplength 4i -justify left \
671 -text {Setting these options modify the internal image on selecting OK}
672 pack $w.msg -side top -padx 5 -pady 5
674 setupEntry $w.col "Columns" integer 0 "Number of columns"
675 setupEntry $w.row "Rows" integer 0 "Number of rows"
676 setupEntry $w.first "First panel" integer 0 "First image or slice to start from"
677 setupEntry $w.skip "Skip" integer 0 "Number of images or slices to skip between panels"
678 setupEntry $w.pad "Padding" integer 0 "Amount of padding between panels"
679 setupEntry $w.fill "Fill value" double $bkg "User-defined fill value between panels"
681 checkbutton $w.flip.x -text "Flip X" -variable flipx
682 checkbutton $w.flip.y -text "Y" -variable flipy
683 pack $w.flip.x $w.flip.y -side left
684 pack $w.col $w.row $w.first $w.skip $w.pad \
685 $w.fill $w.flip -side top -padx 5 -pady 5
688 label $w.switches.tag -text "Show:"
689 checkbutton $w.switches.boxes -text "Boxes" -variable show_mont \
690 -command { drawMontageOverlay }
691 checkbutton $w.switches.labels -text "Labels" -variable show_mont_labels \
692 -command { drawMontageOverlay }
693 checkbutton $w.switches.select -text "Selection" -variable show_mont_select \
694 -command { drawMontageOverlay }
695 pack $w.switches.tag $w.switches.boxes $w.switches.labels $w.switches.select \
696 -side left -ipadx 5 -ipady 5 -padx 1m
699 label $w.font.tag -font $helv12 -text "Font size:"
700 tk_optionMenu $w.font.size font_size "12" "18" "24" "32" "40"
701 canvas $w.font.color -width 30 -height 30 -relief sunken \
702 -borderwidth 2 -background $selected_color
703 pack $w.font.tag $w.font.size $w.font.color -side left -ipadx 5 -ipady 5 -padx 1m
706 button $w.buttons.ok -text Montage -command "montageImage $w"
707 button $w.buttons.cancel -text Unset -command "unsetMontage"
708 button $w.buttons.save -text Save -command "saveMontage"
709 button $w.buttons.del -text "Delete selected" -command "deleteMontageSelection"
710 pack $w.buttons.ok $w.buttons.cancel $w.buttons.save $w.buttons.del -side left -expand 1
712 pack $w.switches $w.font $w.buttons -side top -fill x -pady 2m
714 bind $w.font.color <1> { selectColor .wmont.font.color }
715 bind $w <Return> "montageImage $w"
718 ## @brief Montages the image.
721 proc montageImage { w } {
723 global flipx flipy montover font_size selected_color
724 set first [$w.first.e get]
725 set ncol [$w.col.e get]
726 set nrow [$w.row.e get]
727 set skip [$w.skip.e get]
728 set pad [$w.pad.e get]
729 set fill [$w.fill.e get]
730 set flip [expr $flipx + 2 * $flipy]
731 set nx [Bimage get $theimg width]
732 set ny [Bimage get $theimg height]
733 Bimage set $theimg montage $ncol $nrow $first $skip $pad $fill $flip
734 set montover(active) 1
737 set montover(ncol) $ncol
738 set montover(nrow) $nrow
739 set montover(first) $first
740 set montover(skip) $skip
741 set montover(pad) $pad
742 set montover(flip) $flip
743 set montover(fontsize) $font_size
744 set montover(fontcolor) $selected_color
749 proc unsetMontage { } {
750 global theimg montover
751 set c [getImageCanvas $theimg]
753 set montover(active) 0
754 Bimage set $theimg montage 0 0
759 proc drawMontageOverlay { } {
761 global flipx flipy montover show_mont show_mont_labels show_mont_select
762 set c [getImageCanvas $theimg]
764 if { $montover(active) < 1 } { return }
765 if { $montover(nx) < 1 } { return }
766 if { $montover(ny) < 1 } { return }
768 set nimg [Bimage get $theimg nimages]
769 set scale [.scale.scale get]
770 set nx [expr $scale * $montover(nx)]
771 set ny [expr $scale * $montover(ny)]
772 set first $montover(first)
773 set skip $montover(skip)
774 set ncol $montover(ncol)
775 set nrow $montover(nrow)
776 set pad [expr $scale * $montover(pad)]
778 set ymin [expr $pad + $ny]
780 set ymin [expr $pad * (2 * $nrow - 1) + $ny * $nrow]
783 set show_mont_font [list "Helvetica" $montover(fontsize) "bold"]
784 # puts $show_mont_font
785 for { set row 0 } { $row < $nrow } { incr row 1 } {
786 set ymax [expr $ymin - $ny + 1]
788 set xmin [expr $nx * ($ncol - 1) + $pad * (2 * $ncol - 1)]
792 for { set col 0 } { $col < $ncol } { incr col 1 } {
793 set xmax [expr $xmin + $nx - 1]
794 # puts "$xmin $ymin $xmax $ymax"
795 set xt [expr $xmin + 5]
796 set yt [expr $ymin - 3]
797 set sel [array get montover "$col,$row" ]
804 $c create rect $xmin $ymin $xmax $ymax -outline $color \
808 if $show_mont_labels {
809 $c create text $xt $yt -text $i -anchor sw -fill $color \
810 -font $show_mont_font -tags mont
812 set xt [expr $xmax - 5]
813 if $show_mont_select {
814 set img_sel [Bimage get $theimg select $i]
815 $c create text $xt $yt -text $img_sel -anchor se \
816 -fill $montover(fontcolor) -font $show_mont_font -tags mont
820 set xmin [expr $xmin - $nx - 2 * $pad]
822 set xmin [expr $xmin + $nx + 2 * $pad]
824 set i [expr $i + $skip + 1]
827 set ymin [expr $ymin + $ny + 2 * $pad]
829 set ymin [expr $ymin - $ny - 2 * $pad]
832 bind $c <1> "montageSelect %x %y"
833 bind . <Key-o> { saveMontageSelection }
836 proc montageSelect { x y } {
837 global flipx flipy montover
838 set scale [.scale.scale get]
839 set nx [expr $scale * $montover(nx)]
840 set ny [expr $scale * $montover(ny)]
841 set ncol $montover(ncol)
842 set nrow $montover(nrow)
843 set pad [expr $scale * $montover(pad)]
845 set col [expr $ncol - int($x/($nx + 2 * $pad)) - 1]
847 set col [expr int($x/($nx + 2 * $pad))]
850 set row [expr int($y/($ny + 2 * $pad))]
852 set row [expr $nrow - int($y/($ny + 2 * $pad)) - 1]
854 set sel [array get montover "$col,$row" ]
856 unset montover($col,$row)
858 set montover($col,$row) 1
861 # foreach key [array names montover "*,*"] {
862 # puts "$key $montover($key)"
867 proc saveMontage { } {
868 global theimg montover
869 global imgdir filename
872 if { [string length $currdir] < 2 } { set currdir [pwd] }
873 set new_filename [tk_getSaveFile -filetypes $filetypes \
874 -initialfile [file tail $filename] -initialdir $currdir \
875 -defaultextension .mrc]
876 if { [string length $new_filename] < 1 } { return }
877 set new_filename [relativePath [pwd] $new_filename]
878 Bimage save_montage $theimg $new_filename
881 proc saveMontageSelection { } {
884 set wt [openTextWindow $w]
885 set first $montover(first)
886 set skip $montover(skip)
887 set ncol $montover(ncol)
888 foreach key [array names montover "*,*"] {
889 # puts "$key $montover($key)"
890 set coor [split $key ,]
891 set col [lindex $coor 0]
892 set row [lindex $coor 1]
893 set i [expr $first + $ncol * $row + $col]
894 # $wt insert end "$col\t$row\t$i\n"
895 $wt insert end "$i\n"
900 proc deleteMontageSelection { } {
901 global montover theimg
902 set first $montover(first)
903 set skip $montover(skip)
904 set ncol $montover(ncol)
906 foreach key [array names montover "*,*"] {
907 # puts "$key $montover($key)"
908 set coor [split $key ,]
909 set col [lindex $coor 0]
910 set row [lindex $coor 1]
911 set i [expr $first + $ncol * $row + $col]
912 if [string length $delist] {
920 Bimage delete $theimg $delist
925 ## @brief Calculates a Fourier transform of the image data.
928 proc FourierTransform { dir } {
931 Bimage fft $theimg $dir
941 ## @brief Calculates a power spectrum of the image data.
944 proc PowerSpectrum { } {
945 global theimg project_item
947 global lg avgtileps avgframeps tiled
948 global tilex tiley tilez
949 global tilt_axis tilt_angle tilt_offset defocus
951 set nx [Bimage get $theimg width]
952 set ny [Bimage get $theimg height]
953 set nz [Bimage get $theimg nslices]
954 if { $tilex > $nx } { set tilex $nx }
955 if { $tiley > $ny } { set tiley $ny }
956 if { $tilez > $nz } { set tilez $nz }
958 set tilesize "$tilex $tiley $tilez"
963 set defocus [ expr 1e-4 * [Bmg get $project_item defocus] ]
964 set tilt_axis [Bmg get $project_item axis]
965 set tilt_angle [Bmg get $project_item tilt]
966 set cs [expr 1e-7 * [Bmg get $project_item Cs] ]
967 set volt [expr 1e-3 * [Bmg get $project_item volt] ]
975 wm title $w "Power spectrum"
978 label $w.msg -font $helv12 -wraplength 4i -justify left \
979 -text {Select parameters to calculate a power spectrum}
980 pack $w.msg -side top -padx 5 -pady 5
982 checkbutton $w.logbutton -text "Logarithm" -variable lg
983 checkbutton $w.avgtiles -text "Average tile power spectra" -variable avgtileps
984 checkbutton $w.avgframes -text "Average frame power spectra" -variable avgframeps
986 setupCheckSizeEntry $w.tiles "Tiles" int $tilesize tiled "Size of tiles to divide the micrograph"
988 labelframe $w.tilt -text "Tilt parameters"
989 setupEntry $w.tilt.axis "Tilt axis angle" double $tilt_axis "Micrograph tilt axis angle conter-clockwise from the x-axis in degrees"
990 setupEntry $w.tilt.angle "Tilt angle" double $tilt_angle "Micrograph tilt angle in degrees"
991 setupEntry $w.tilt.offset "Offset from origin" double $tilt_offset "Tilt axis offset from the micrograph origin"
992 setupEntry $w.tilt.defocus "Defocus" double $defocus "Estimate/guess of defocus"
993 setupEntry $w.tilt.cs "Spherical aberration" double $cs "Cs in mm"
994 setupEntry $w.tilt.volt "Acceleration voltage" double $volt "Acceleration voltage in kV"
995 pack $w.tilt.axis $w.tilt.angle $w.tilt.offset $w.tilt.defocus \
996 $w.tilt.cs $w.tilt.volt -side top -expand 1
999 button $w.buttons.ok -text OK -command "calcPowerSpec $w"
1000 button $w.buttons.cancel -text Cancel -command "destroy $w"
1001 pack $w.buttons.ok $w.buttons.cancel -side left -expand 1
1003 pack $w.logbutton $w.avgframes $w.avgtiles $w.tiles $w.tilt $w.buttons -side top
1005 bind $w <Return> "calcPowerSpec $w"
1008 ## @brief Calculates a power spectrum of the image data.
1010 # @param w the window to destroy.
1012 proc calcPowerSpec {w} {
1014 global lg avgtileps avgframeps tiled
1015 global tilex tiley tilez
1016 global tilt_axis tilt_angle tilt_offset defocus
1018 global filename project_item
1019 global window_width window_scale
1021 set tilex [$w.tiles.x get]
1022 set tiley [$w.tiles.y get]
1023 set tilez [$w.tiles.z get]
1024 set tilt_axis [$w.tilt.axis.e get]
1025 set tilt_angle [$w.tilt.angle.e get]
1026 set tilt_offset [$w.tilt.offset.e get]
1027 set defocus [$w.tilt.defocus.e get]
1028 set cs [$w.tilt.cs.e get]
1029 set volt [$w.tilt.volt.e get]
1030 if { $tilt_angle != 0 } { set tiled 1 }
1032 # puts "Powerspectrum: $lg $tiled"
1033 set flags [expr 1 + 2*$avgtileps + 4 + 8*$lg + 16*$avgframeps]
1035 Bimage powerspec $theimg $flags $tilex $tiley $tilez $tilt_axis $tilt_angle $tilt_offset $defocus $cs $volt
1037 Bimage powerspec $theimg $flags
1042 Bmg set $project_item filename $filename ps
1043 Bmg set $project_item axis [expr $tilt_axis]
1044 Bmg set $project_item tilt [expr $tilt_angle]
1046 reconfigureRangeStep
1049 set width [Bimage get $theimg width]
1050 if { $width > $window_width } { set scale [expr 0.1*((10*$window_width)/$width) ] }
1051 if { $scale < 0.1 } { set scale 0.1 }
1052 set window_scale $scale
1053 .scale.scale set $scale
1054 set ps [Bimage get $theimg pixel_size 0]
1055 puts "Power spectrum pixel size: $ps"