4
$\begingroup$

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,
   PlotRangePadding -> 0,
   ImageSize -> {400, 400}
   ], 10]]

Preview of what this code is doing:

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ The simplest thing might be to create the image larger than you need and reduce it using ImageResize. $\endgroup$ – Simon Woods Jan 6 at 22:04
  • $\begingroup$ @SimonWoods, I believe this would be similar to adding a blur to the image. But I'll try it. $\endgroup$ – Cham Jan 6 at 22:05
  • $\begingroup$ @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. $\endgroup$ – Cham Jan 6 at 22:13
3
$\begingroup$
  • 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
 ]

smoother version

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ – Cham Jan 6 at 22:20
  • 1
    $\begingroup$ @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. $\endgroup$ – MarcoB Jan 6 at 22:26
  • $\begingroup$ Ok, I think it's clear. Thanks! $\endgroup$ – Cham Jan 6 at 22:27
4
$\begingroup$

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]

Rasterized image

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.