EDIT: Solutions by @Alexey Popkov and @Vitaliy Kaurov are very intuitive and both can be used to find a solution for the task.
I have a slit of 15-18 microns by 1 mm projected with magnified 4X onto a cmos sensor of 1944 x 2592 pixels. However, the width is not uniform and I want to find a method which shows how much variation occurs in the width along the whole length of the slit, which is 4 mm when magnified.
Answer
I think the essence of the problem here is that width needs to be counted orthogonally to some best fit line going through the elongated shape. Even naked eye would estimate some non-zero slope. We need to make line completely horizontal on average. We could use ImageLines (see this compact example) but I suggest optimization approach. Import image:
i = Import["http://i.stack.imgur.com/BGbTa.jpg"];
See slope with this:
ImageAdd[#, ImageReflect[#]] &@ImageCrop[i]
Use this to devise a function to optimize:
f[x_Real] := Total[ImageData[
ImageMultiply[#, ImageReflect[#]] &@ImageRotate[ImageCrop[i], x]], 3]
Realize where you are looking for a maximum:
Table[{x, f[x]}, {x, -.05, .05, .001}] // ListPlot
Find more precise maximum:
max = FindMaximum[f[x], {x, .02}]
{19073.462745098062
, {x -> 0.02615131131124671
}}
Use it to zero the slope
zeroS = ImageCrop[ImageRotate[i, max[[2, 1, 2]]]]
ListPlot3D[ImageData[ColorConvert[zeroS, "Grayscale"]],
BoxRatios -> {5, 1, 1}, Mesh -> False]
and get the width data (you can use different Binarize threshold or function):
data = Total[ImageData[Binarize[zeroS]]];
ListLinePlot[data, PlotTheme -> "Detailed", Filling -> Bottom, AspectRatio -> 1/4]
Get stats on your data:
N[#[data]] & /@ {Mean, StandardDeviation}
{14.28099910793934
, 1.7445029175852613
}
Remove narrowing end points outliers and find that your data are approximately under BinomialDistribution:
dis = FindDistribution[data[[5 ;; -5]]]
BinomialDistribution[19, 0.753676644441292`]
Show[Histogram[data[[5 ;; -5]], {8.5, 20, 1}, "PDF", PlotTheme -> "Detailed"],
DiscretePlot[PDF[dis, k], {k, 7, 25}, PlotRange -> All, PlotMarkers -> Automatic]]
Comments
Post a Comment