Bsoft  2.1.0
Bernard's software package
bshow_histogram.tcl
Go to the documentation of this file.
1 ##
2 # @file bshow_histogram.tcl
3 #
4 # @brief Procedures to draw a histogram in Bshow.
5 #
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20130424
9 
10 ## @brief Draws a histogram of the image data.
11 #
12 
13 proc Histogram { } {
14  global helv12 theimg
15  global orix oriy width height
16  global histplot
17  set w .whis
18  if { [info exists histplot] } { unset histplot }
19  if ![winfo exists $w] {
20  toplevel $w
21  set width 500
22  set height 350
23  set orix 100
24  set oriy 400
25  set xmin [Bimage get $theimg min]
26  set xmax [Bimage get $theimg max]
27  set datatype [Bimage get $theimg datatype]
28  set bins [expr int($xmax - $xmin + 1)]
29  if { $bins < 10 } { set bins 10 }
30  if { $bins > 500 } { set bins 500 }
31  if { [string first "float" $datatype] > -1 } { set bins 250 }
32  set bar_width [expr $width/(2*$bins)]
33  set hist [Bimage histogram $theimg $bins]
34 
35  set ymin 0.0
36  set ymax [lindex $hist 0]
37  for { set i 0 } { $i < $bins } { incr i } {
38  if { $ymax < [lindex $hist $i] } { set ymax [lindex $hist $i] }
39  }
40  if { $xmin == $xmax } { set xmax [expr $xmin + 1] }
41  if { $ymin == $ymax } { set ymax [expr $ymin + 1] }
42 # puts "$datatype $xmin $xmax $ymin $ymax $bins"
43 
44  set xscale [expr ($xmax - $xmin)/$bins]
45  for { set i 0 } { $i < $bins } { incr i } {
46  set histplot(x,$i) [expr $i*$xscale + $xmin]
47  set histplot(y,$i) [lindex $hist $i]
48  }
49 
50  menu $w.menuBar -tearoff 0
51  $w.menuBar add cascade -menu $w.menuBar.his -label "Histogram" -underline 0
52  menu $w.menuBar.his -tearoff 0
53  $w configure -menu $w.menuBar
54  $w.menuBar.his add command -label "Help" \
55  -command { showHelp bshow_graph.hlp } -underline 0
56  $w.menuBar.his add command -label "Reset plot" -underline 0 \
57  -command { drawGraph $w $w.plot histplot "bar" } -accelerator "Ctrl-r"
58  bind . <Control-r> { drawGraph $w $w.plot histplot "bar" }
59 
60  set h $w.plot
61  canvas $h -relief raised -width [expr $orix + $width + 50] \
62  -height [expr $oriy + 100]
63  pack $h -side top -fill x
64 
65  frame $w.point -bg green
66  label $w.point.xtag -font $helv12 -text "x" -bg green
67  label $w.point.x -text "0" -relief sunken -bd 1 -width 8 \
68  -font $helv12 -anchor w
69  label $w.point.ytag -font $helv12 -text "y" -bg green
70  label $w.point.y -text "0" -relief sunken -bd 1 -width 8 \
71  -font $helv12 -anchor w
72  pack $w.point.xtag $w.point.x $w.point.ytag $w.point.y \
73  -side left -pady 10 -padx 10
74 
75  frame $w.lim -bg cyan
76  label $w.lim.x_tag -font $helv12 -text "X limits" -bg cyan
77  entry $w.lim.x_min -width 6 -validate key -vcmd { string is double %P }
78  entry $w.lim.x_max -width 6 -validate key -vcmd { string is double %P }
79  label $w.lim.y_tag -font $helv12 -text "Y limits" -bg cyan
80  entry $w.lim.y_min -width 6 -validate key -vcmd { string is double %P }
81  entry $w.lim.y_max -width 6 -validate key -vcmd { string is double %P }
82  pack $w.lim.x_tag $w.lim.x_min $w.lim.x_max $w.lim.y_tag \
83  $w.lim.y_min $w.lim.y_max -side left -pady 10 -padx 10
84  pack $w.point $w.lim -side left
85 
86  updateLimits $w $xmin $xmax $ymin $ymax
87  plotGraph $w $h histplot "bar" "Bin" "Count"
88  } else {
89  wm deiconify $w
90  raise $w
91  }
92  wm title $w "Histogram"
93  wm iconname $w "Histogram"
94  $h bind point <Any-Enter> { showPoint $w histplot %x %y }
95  $h bind point <Any-Leave> { showPoint $w histplot %x %y }
96  bind $h <Button-1> {
97  set x1 [$w.plot canvasx %x]
98  set y1 [$w.plot canvasy %y]
99  }
100  bind $h <B1-Motion> { drawRectangle $w.plot $x1 $y1 [$w.plot canvasx %x] [$w.plot canvasy %y] }
101  bind $h <B1-ButtonRelease> { drawSubGraph $w $w.plot histplot $x1 $y1 \
102  [$w.plot canvasx %x] [$w.plot canvasy %y] "bar" }
103  bind $w <Return> { plotGraph $w $w.plot histplot "bar" "Bin" "Count" }
104  bind $w <Control-w> {
105  if { [info exists histplot] } { unset histplot }
106  destroy $w
107  }
108 }
109