I have a function to measure a distance. Below I give first a description, and then a function code.
Description
The function measureDistance[image] enables one to get the locators placed into any 2 points of an image, and memorizes them in a global variable entitled "poinTs" and the distance between them - in a global variable "diStance"
Parameters:
image is any image. It should have Head=Image, if it is a Graphics object wrap it with Image statement. The code uses specific Image properties during the rescaling.
Controls:
The Checkbox "whiteLocatorRing" defines, if the locators are shown by a single color ring (unchecked), or with two rings, the outer having a color defined by the ColorSlider (see below), the inner one being white. This may be helpful, if working with a too dark image.
size controls the size of the image. The default value is 450. This slider is used to adjust the size to the one to enable the most comfortable work with the image plot.
opacity controls the opacity of the line connecting the locators
thickness controls the thickness of the double ring that forms each locator.
lineThickness controls the thickness of the line connecting the locators
color is the colour slider that controls the colour of the outer ring forming the locator and the line connecting them. The inner locator ring is always white or no white ring at all.
radius controls the radius of the locators.
InputFields: 1) The input fields Subscript[x, 1] and Subscript[x, 2] should be supplied by the reference points x1 and x2.
2) The input fields "Locators coordinates" and "The inter-locator distance" serve to show the output. The
corresponding figures may be copied from these input fields. Alternatively one can evaluate the variables
poinTs
or
diStance
Buttons: The button
"Memorize the scale"
should be pressed after the first two locators are placed on the corresponding reference points. The button
"Pick up the coordinates of locators and evaluate the distance between them"
should be pressed at the end of the session. Upon its pressing the actual list of points representing those of the curve is assigned to the global variable "poinTs", and its norm to the global variable "diStance".
Operation sequence:
Step 1: Evaluate the function
Step 2: Enter the reference points at the plot x axes into the input fields. Press Enter.
Step3: Alt+Click on the point with x-coordinate x1. This brings up the first locator visible as a circle. Alt+Click (Command + Click for Mac OS X) on that with x2 which gives rise to the second locator. Adjust the locators, if necessary. Press the button "Memorize scale".
Step 4: Move the two already existing locators to the points of the image the distance between which should be measured. Adjust locators, if necessary.
Step 5: Press the button "Pick up the coordinates ...". This assigns the captured coordinates to the global variable "poinTs" and the distance between them to the global variable "diStance". Done.
The results can be read in and copied from the InputFields "Locators coordinates" and "The inter-locator distance", or can be accessed by evaluation the variables "poinTs" and "diStance".
The "poinTs" and "diStance" are a global variable. They can be addressed everywhere in the notebook.
The function measureDistance
Clear[measureDistance];
measureDistance[image_] :=
Manipulate[
DynamicModule[{pts = {}, x1 = Null, x2 = Null, \[CapitalDelta]X, X1, X2, g,
myRound},
myRound[x_] := Round[1000*x]/1000 // N;
(* Begins the column with all the content of the manipulate *)
Column[{
(* Begin LocatorPane*)
Dynamic@LocatorPane[Union[Dynamic[pts]],
Dynamic@
Show[{Image[image, ImageSize -> size],
Graphics[{color, AbsoluteThickness[lineThickness],
Opacity[opacity], Line[Union[pts]]}]
}], LocatorAutoCreate -> True,
(* Begin Locator appearance *)
Appearance -> If[whiteLocatorRing,
Graphics[{{color, AbsoluteThickness[thickness],
Circle[{0, 0}, radius + thickness/2]}, {White,
AbsoluteThickness[thickness], Circle[{0, 0}, radius]}},
ImageSize -> 10]
,
Graphics[{{color, AbsoluteThickness[thickness],
Circle[{0, 0}, radius + thickness/2]}}, ImageSize -> 10]](*
End Locator appearance *)
],(* End LocatorPane*)
(* Begin of the block of InputFields *)
Row[{ Style["\!\(\*SubscriptBox[\(x\), \(1\)]\):"],
InputField[Dynamic[x1],
FieldHint -> "Type \!\(\*SubscriptBox[\(x\), \(1\)]\)",
FieldSize -> 7, FieldHintStyle -> {Red}],
Spacer[20], Style[" \!\(\*SubscriptBox[\(x\), \(2\)]\):"],
InputField[Dynamic[x2],
FieldHint -> "Type \!\(\*SubscriptBox[\(x\), \(2\)]\)",
FieldSize -> 7, FieldHintStyle -> {Red}],
(* Begin button "Memorize scale X" *)
Spacer[30],
Button["Memorize scale",
X1 = Min[Transpose[myRound /@ Union[pts]][[1]]];
X2 = Max[Transpose[myRound /@ Union[pts]][[1]]];
\[CapitalDelta]X = X2 - X1;
](* End of button "Memorize scale X" *)
}],
(* End of the block of InputFields and button *)
Spacer[20],
(* Begin button "Make the list of the curve's points" *)
Button[
Style["Pick up the locators coordinates and calculate the distance \
between them" , Bold],
g[{a_, b_}] := {(x1*X2 - x2*X1)/\[CapitalDelta]X +
a/\[CapitalDelta]X*Abs[x2 - x1], (x1*X2 - x2*X1)/\[CapitalDelta]X +
b/\[CapitalDelta]X*Abs[x2 - x1]};
Clear[poinTs, diStance];
poinTs = Map[myRound, Map[g, pts]];
diStance = poinTs // Differences // Norm
],(* End of button "Make the list..." *)
Row[{Style["Locators coordinates: ", 14, Blue, Bold],
InputField[Dynamic[poinTs]]}],
Spacer[10],
Row[{Style["The inter-locator distance: ", 14, Blue, Bold],
InputField[Dynamic[diStance]]}]
}, Alignment -> Center](*
End of column with all the content of the manipulate *)
],(* End of the DynamicModule *)
(* The massive of sliders begins *)
Column[{Row[{Control[{whiteLocatorRing, {True, False}}], Spacer[50]}],
Row[{Spacer[32.35], Control[{{size, 450}, 300, 800}], Spacer[38.5`],
Control[{{opacity, 0.5}, 0, 1}]}],
Row[{Spacer[10.], Control[{{thickness, 1}, 0.5, 5}], Spacer[13.65],
Control[{{lineThickness, 1}, 0, 10}] }],
Row[{Spacer[22.8], Control[{color, Red}], Spacer[59.3],
Control[{{radius, 0.5}, 0, 3}]}]
}, Alignment -> Center],(* The massive of sliders ends *)
(* Definitions of sliders *)
ControlType -> {Checkbox, Slider, Slider, Slider, Slider, ColorSlider,
Slider},
ControlPlacement -> Top, SaveDefinitions -> True
];
The example of what you should see on the screen is shown in the picture below:
