Use a picture as the initial distribution of an agent based model
You have to use ColorNegate
and other image processing functions to make the area that you want to sample is white, while the rest is black. After that you can use ImageMesh
and RandomPoint
like this:
img = Import["https://i.stack.imgur.com/J2JQK.png"];
mesh = ImageMesh[ColorNegate[img]];
pts = RandomPoint[mesh, 500];
Show[mesh,ListPlot[pts, PlotStyle -> Red]]
img = Import["https://i.stack.imgur.com/9io21.gif"];
mesh = ImageMesh[ColorNegate[img]];
pts = RandomPoint[mesh, 500];
Show[mesh, ListPlot[pts, PlotStyle -> Red]]
To check if a point is inside the region, you might use RegionMember
. This could be used as a "distribution function". (In this example, mesh
corresponds to the first image in this answer.)
f = RegionMember[mesh];
{f[{0, 0}], f[{1500, 1500}]}
{False, True}
you can use PixelValuePositions
Show[image, MapThread[Graphics[{#2, Point@RandomSample[PixelValuePositions[image, #1],
2000]}] &, {{0, 1}, {Red, Blue}}]]
of course there are some outlying points and that is because your image has a thin white border surrounding it