Skip to main content
Tweeted twitter.com/StackMma/status/1002733037053009921
deleted 2 characters in body
Source Link
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323

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]

enter image description here

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]}]

enter image description here

In this way,

  1. I can notcannot isolate the regions - for example, the edges of the circle and the quarter circle in the corner.

  2. I can notcannot find a smooth line for the edges.

  3. 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?

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]

enter image description here

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]}]

enter image description here

In this way,

  1. I can not isolate the regions - for example, the edges of the circle and the quarter circle in the corner.

  2. I can not find a smooth line for the edges.

  3. 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?

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]

enter image description here

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]}]

enter image description here

In this way,

  1. I cannot isolate the regions - for example, the edges of the circle and the quarter circle in the corner.

  2. I cannot find a smooth line for the edges.

  3. 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?

edited tags
Link
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323
Source Link
Sumit
  • 16k
  • 2
  • 33
  • 74

How to find the boundary of a region from a dataset

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]

enter image description here

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]}]

enter image description here

In this way,

  1. I can not isolate the regions - for example, the edges of the circle and the quarter circle in the corner.

  2. I can not find a smooth line for the edges.

  3. 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?