Antonio,
Here is a solution based on the Bresenham's algorithm implemented by halirutan:
bresenham[p0_, p1_] := Module[{dx, dy, sx, sy, err, newp},
{dx, dy} = Abs[p1 - p0];
{sx, sy} = Sign[p1 - p0];
err = dx - dy;
newp[{x_, y_}] :=
With[{e2 = 2 err}, {If[e2 > -dy, err -= dy; x + sx, x],
If[e2 < dx, err += dx; y + sy, y]}];
NestWhileList[newp, p0, # =!= p1 &, 1]
];
Since Bresenham's algorithm requires integer pixel positions for both ends of the line we have to snap the line to the pixel grid in the coordinate system used by PixelValue
which differs from the standard image coordinate system by 1/2
:
\[Alpha] = Pi/4;
radius = 300;
crcl = ComponentMeasurements[RegionBinarize[Dilation[img, 3], {{600, 440}}, 0.5],
"Centroid"];
(* centroid in the standard image coordinate system *)
center = 1 /. crcl;
(*integer position of the central pixel in the coordinate system used by PixelValue*)
centralPixelPosition = Round@N[center + 1/2];
(*integer position of the ending pixel in the coordinate system used by PixelValue*)
endingPixelPosition = Round@N[AngleVector[centralPixelPosition, {radius, \[Alpha]}]];
radialPixelPositionsBresenham = bresenham[centralPixelPosition, endingPixelPosition];
radialProfileBresenham =
Transpose[{Map[N@EuclideanDistance[centralPixelPosition, #] &,
radialPixelPositionsBresenham], PixelValue[img, radialPixelPositionsBresenham]}];
model = a/b Erfc[(-x + \[Mu])/(Sqrt[2] \[Sigma])];
fit = FindFit[radialProfileBresenham, model, {a, b, \[Mu], \[Sigma]}, x];
Row[{Show[ListLinePlot[radialProfileBresenham, ImageSize -> Medium],
Plot[model /. fit, {x, 0, radius}, PlotStyle -> Red, PlotRange -> All,
Evaluated -> True]],
Show[img, Epilog -> {Red, Thickness[.01],
Arrow[{centralPixelPosition - 1/2, endingPixelPosition - 1/2}],
Circle[centralPixelPosition - 1/2, radius]}, ImageSize -> Small]}]
Also find attached updated version of your Notebook.
Attachments: