# How to create a smooth black and white texture without jaggies?

The following code creates a random black and white texture with ugly jaggies (pixelized "stairs") all around the shapes, because of the Binarize command. I could add a final Blur or GaussianFilter to remove them, but the result is too blurry. How can I modify that code to get nice smooth shapes, without bluring it?

randomTiles = Table[{RandomReal[], RandomReal[], RandomInteger[{0, 1}]}, {n, 1, 1000}];

Binarize[Blur[ListDensityPlot[
randomTiles,
InterpolationOrder -> 0,
Frame -> False,
ImageSize -> {400, 400}
], 10]]


Preview of what this code is doing:

• The simplest thing might be to create the image larger than you need and reduce it using ImageResize. – Simon Woods Jan 6 at 22:04
• @SimonWoods, I believe this would be similar to adding a blur to the image. But I'll try it. – Cham Jan 6 at 22:05
• @SimonWoods, apparently, it doesn't work. I used 1500 pixels instead of 1024 in the code above, then added ImageResize with {1024, 1024} as new size. It still gives strong pixelated shapes. – Cham Jan 6 at 22:13

• Use ColorFunction -> Graylevel in ListDensityPlot to start from a black and white image, avoiding Binarize altogether.
• ask for a much higher number of pixels in your image with ImageSize; that alone causes considerable smoothing when you downsize the image;
• for further effect, apply e.g. MedianFilter with an appropriate parameter.
SeedRandom[1234]
randomTiles =
Table[{RandomReal[], RandomReal[], RandomInteger[{0, 1}]}, {n, 1, 1000}];

MedianFilter[#, 5] &@
ListDensityPlot[
randomTiles,
InterpolationOrder -> 0, Frame -> False,
PlotRangePadding -> 0, ImageSize -> {1000, 1000},
ColorFunction -> GrayLevel
]


• Oh yes! This works very nicely, and I can even choose another color scheme with this! Can you explain more in depth what the MedianFilter is doing, with its option 5? – Cham Jan 6 at 22:20
• @Cham Since a picture is worth a thousand words, particularly in this case, take a look at the results of its usage in the documentation. The effect is pretty clear to the eye, although I would be hard pressed to put it in words. Technically, MedianFilter replaces a pixel's value with the median value of pixels in a neighborhood whose size is controlled by its parameter. – MarcoB Jan 6 at 22:26
• Ok, I think it's clear. Thanks! – Cham Jan 6 at 22:27

Here's another option using Rasterize.

SeedRandom[1234];
img = Binarize[
Blur[ListDensityPlot[randomTiles, InterpolationOrder -> 0,
Frame -> False, PlotRangePadding -> 0,
ImageSize -> {1600, 1600}], 40]];
rimg = Rasterize[img, RasterSize -> 400, ImageSize -> 400]
ImageDimensions[rimg]


• Feed that into MorphologicalComponents[rimg] // Colorize and you have some high art. – Rudy Potter Jan 6 at 22:41