0
$\begingroup$

I used in my calculations zome polynomials evaluated on a grid with Array command and characteristic functions of some polygons, which I evaluated using Rasterize function and I realised that the results of these two sampling paradigms are not consistent. It looks like Rasterize ignores or adjusts the PlotRange option. The best it can be illustrated by this MWE:

Manipulate[ 
  graphics = 
    Graphics[{GrayLevel[0], Disk[{0, 0}, .8]}, 
      PlotRange -> {{-1, 1}, {-1, 1}}, Frame -> True]; 
  raster = 
    Rasterize[
      Graphics[{GrayLevel[0], Disk[{0, 0}, .8]}, 
        PlotRange -> {{-1, 1}, {-1, 1}}], 
      RasterSize -> {picsize, picsize}, 
      ImageSize -> {picsize, picsize}];
  array = 
    Image @ Boole @ 
      Array[{##} ∉ Disk[{0, 0}, .8] &, {picsize, picsize}, {{-1, 1}, {-1, 1}}];
  Grid[
    {{"Graphics", "Rasterize", "Array", "Rasterize vs Array"}, 
     Show[#, ImageSize -> 200, Frame -> True] & /@ 
       {graphics, raster, array, ColorCombine[{raster, array, array}]}}],
  {{picsize, 15}, 3, 50, 1}]

In this example, where a disk with a radius of 0.8 is shown in a square region [-1, 1]^2, it's visible that while Graphics and Array show nicely centred disk independently of the spatial sampling rate, Rasterize behaves weird.

The figure below shows the graphics object and its sampled versions as calculated by Rasterize and Array (Mathematica 12.1, Windows 10). The last plot overlays the two representations for comparison, so the misalignment is visible.

An example of a graphics object and its sampled versions as calculated by <code>Rasterize</code> and <code>Array</code>. The last plot overlays the two representations for comparison.

Is it a bug or do I miss something?

The reason I use Rasterize and not Array that Rasterize is much faster for large image sizes.

Update and Workaround: the misalignment can be corrected by changing the plot region by adding to it right and top margins of one pixel width. In the example above, for instance, the perfect alignment can be obtained by using Rasterize[ Graphics[{GrayLevel[0], Disk[{0, 0}, .8]}, PlotRange -> {{-1, 1 + 2/picsize}, {-1 - 2/picsize, 1}}], RasterSize -> {picsize, picsize}]

So the MWE above can be modified to

Manipulate[
 graphics = 
  Graphics[{GrayLevel[0], Disk[{0, 0}, .8]}, 
   PlotRange -> {{-1, 1 + 2/picsize}, {-1 - 2/picsize, 1}}, 
   GridLines -> {{-1, 0, 1}, {-1, 0, 1}}];
 tr = Timing[
   raster = 
      Rasterize[graphics, RasterSize -> {picsize, picsize}];];
 tarr = Timing[
   array = 
      Image @ Boole @ 
      Array[{##} ∉ Disk[{0, 0}, .8] &, {picsize, picsize}, {{-1, 1}, {-1, 1}}];];
 Grid[
   {{"Graphics", 
     "Rasterize, " <> ToString[tr[[1]]] <> "s", 
     "Array, " <> ToString[tarr[[1]]] <> "s", 
     "Rasterize vs Array"}, 
     Show[#, ImageSize -> 200, Frame -> True] & /@ 
       {graphics, raster, array, ColorCombine[{raster, array, array}]}}], 
 {{picsize, 15}, 3, 50, 1}]

which results in Aligned Rasterize

From all this, my guess is that Rasterize select the sampling points uniformly in the specified plot region, and uses them as top-left corners for the pixels. Effectively, this increases the total plot region.

$\endgroup$
1
  • $\begingroup$ I suggest you make your update into a self-answer $\endgroup$
    – m_goldberg
    Apr 26, 2020 at 22:40

1 Answer 1

1
$\begingroup$

I'm running V12.0 on MacOS 10.13 (High Sierra). I don't see any problem unless picsize rather small, forcing a coarse rasterization, which is documented as causing problems.

When the settings for RasterSize or ImageResolution are small, the graphic will appear coarse, and text may be illegible. There may also be artifacts associated with aliasing.

I think this is the problem you are encountering. Since it documented I don't thing we have call it feature rather than bug. The work-around is avoid coarse rasterization.

$\endgroup$
1
  • $\begingroup$ What I meant that Rasterize changes the plot region, and the disk is not centred in the resulted matrix, independently from the coarseness. The rightmost plot compares the image obtained by Array and Rasterize with the same coarseness. $\endgroup$ Apr 25, 2020 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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