I have a dataset which supposed to show some regions. For example
r1 = Disk[{-0.1, -0.1}, 0.5];
r2 = Disk[{0.7, 0.5}, 0.2];
ran = 0.25; (*random amplitude*)
f[x_, y_] := Which[RegionMember[r1, {x, y}], 1 + RandomReal[{-ran, ran}],
RegionMember[r2, {x, y}], -1 + RandomReal[{-ran, ran}], True,
RandomReal[{-ran, ran}]]
data = Table[{x, y, f[x, y]}, {x, 0, 1, 0.02}, {y, 0, 0.8, 0.02}]~ Flatten~1;
ListDensityPlot[data, InterpolationOrder -> 0, PlotRange -> All, AspectRatio -> 4/5]
And I want to extract the edges of the r1
and r2
.
I try to convert the data
into an image
and use EdgeDetect
.
ndat = Length[data]
{zmin, zmax} = MinMax@data[[All, 3]]
Do[If[data[[n, 1]] != data[[n + 1, 1]], ny = n; Break[]], {n, ndat}];
nx = ndat/ny;
imdata = Partition[data[[All, 3]], ny];
imdata = Table[(imdata[[j, ny - i + 1]] - zmin)/(zmax - zmin), {i, ny}, {j, nx}];
img = Image[imdata];
pts = PixelValuePositions[EdgeDetect[img], 1];
pts = Partition[data[[All, 1 ;; 2]], ny][[#[[1]], #[[2]]]] & /@ pts;
ListDensityPlot[data, InterpolationOrder -> 0, PlotRange -> All, AspectRatio -> 4/5,
Epilog -> {Red, Point[pts]}]
In this way,
I cannot isolate the regions - for example, the edges of the circle and the quarter circle in the corner.
I cannot find a smooth line for the edges.
It does not work very well when I increase the noise (say
ran=0.5
).
How to find the separate edges as isolated lines/lists?