Skip to main content

Including the actual length scale of the image in processing microscopic images


Following the wonderful methodologies provided in this post, I have learned how to process microscopic images of the types shown below in order to analyze them. When it comes to analysing the size distribution of the particles, the images always have a scale indicator, which give the actual scale of the image. By default, when we measure the size distribution e.g. using ComponentMeasurements, everything is measured in pixels. My question is:



  • Is there a way to incorporate/detect in Mathematica the actual given length-scale of the image? (in example below $500 nm$ for the shown spacing), that is, to convert pixels to $nm$ (or $nm^2$ for area). Example given below (source):


enter image description here





To detect the particles, I've adopted the following approach learned from Niki Estner's previous answer:


img = Import["https://i.stack.imgur.com/ryzmV.jpg"]
ridges = RidgeFilter[-img, 1];
distRidges =
DistanceTransform@ColorNegate@MorphologicalBinarize[ridges];
distMax = MaxDetect[distRidges, 1];
morph = WatershedComponents[ridges, distMax, Method -> "Basins"];
comp = ComponentMeasurements[{img, morph}, {"Centroid", "Neighbors"}];


edges = Dilation[EdgeDetect[Image[morph], 1, .001], 0.5];
edgeOverlay =
Show[img, SetAlphaChannel[ColorReplace[edges, White -> Red], edges]]

yielding the following detection result:


enter image description here


and to extract the particle sizes in order to estimate the size distribution histogram, I've used the "Area" property in ComponentMeasurements as follows:


sizesls = {};
areaInPixels = ComponentMeasurements[morph, {"Area"}];
For[i = 1, i <= Length[areaInPixels], i++,

eltmp = areaInPixels[[i]][[2]];
AppendTo[sizesls, eltmp[[1]]];
];
Histogram[sizels]

enter image description here


but the size distribution is obtained in pixels, as opposed to using the actual scale of the image in $nm$ as indicated in the original image. From the source of the image, the indicated expected mean particle size is $60 nm.$



Answer



First of all, I would exclude the scale from the "particle search algorithm", like this:


imgWithScale = Import["https://i.stack.imgur.com/ryzmV.jpg"];

img = ImageTake[imgWithScale, 280]

(followed by the same steps you used above).


Then I would extract the nm/pixel scale:


scaleArea = ImageTake[imgWithScale, {-20, -15}]
whitePixels = PixelValuePositions[Binarize[scaleArea], 1];
{left, right} = MinMax[whitePixels[[All, 1]]];
nmPerPx = 500./(right - left)

(Note that I'm using Part array access ([[All, ...) instead For loops to extract specific elements from a nested list. This is almost always shorter, faster and more readable. Never use For in Mathematica)



Next, I'm not sure the Area is the best measure of particle size. Your particles are mostly ciruclar, so we can try a few others:


measurements = {"EquivalentDiskRadius", "MeanCentroidDistance", 
"Length", "CaliperLength", "BoundingDiskRadius", "MeanIntensity"};
(* MeanIntensity is dimensionless, the other ones are lengths,
i.e scaled by nmPerPx^1 *)
scale = nmPerPx^{1, 1, 1, 1, 1, 0};
(* only count particles that aren't clipped by an image border, with
condition #AdjacentBorderCount == 0 & *)
comp = ComponentMeasurements[{img, morph}, measurements, #AdjacentBorderCount == 0 &];
Multicolumn[

MapThread[
Histogram[#1, PlotLabel -> #2, ImageSize -> 300] & , {scale*
Transpose[comp[[All, 2]]], measurements}]]

enter image description here


(I've used EquivalentDiskRadius, the radius of a disk with the same area, so it's easier to compare with the other lengths.)


It seems that Length and CaliperLength are much better estimates for the particle size, because they are less dependent on occlusion.


We can also easily see that there's a correlation between intensity (i.e. depth) and size:


compare = {4, 6};
ListPlot[comp[[All, 2, compare]],

AxesLabel -> measurements[[compare]]]

enter image description here


If you want to explore the correlation further, you can use LocatorPane and Dynamic to see which component is where interactively:


nfIdx = Nearest[comp[[All, 2, compare]] -> comp[[All, 1]]];
nf = Nearest[comp[[All, 2, compare]]];
pt = comp[[1, 2, compare]];

Row[{
LocatorPane[Dynamic[pt],

Dynamic[ListPlot[comp[[All, 2, compare]],
AxesLabel -> measurements[[compare]], GridLines -> nf[pt],
ImageSize -> 500]]],
Dynamic[
HighlightImage[img,
ColorNegate[Binarize[Image[(morph - nfIdx[pt][[1]])^2], 0]],
ImageSize -> 400], TrackedSymbols :> {pt}]}]

enter image description here


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...