3
$\begingroup$

I tried something small first, to get a feeling about this. So I defined a Rectangle[], placed it in a corner of Graphics[PlotRange->{...}] and defined an ImageSize. After applying Binarize[] and ImageData[] consecutively, I got an array of data. Perfect so far, but:

  1. The dimension of the data does not equal the dimensions of ImageSize[], why?
  2. Why aren't the rows filled with 0 directly positioned at the edge, as the Rectangle[] is supposed to be positioned in the corner?

My example:

periode = 10;
length = 5;
height = 2;

rect = Rectangle[{0, 0}, {length, height}];

gr = Graphics[rect, PlotRangePadding -> 0, 
  PlotRange -> {{0, periode}, {0, periode}}, 
  ImageSize -> {periode, periode}]

data = ImageData[Binarize[gr]]
Dimensions[data]

The result looks like this:

image

$\endgroup$
3
  • $\begingroup$ It seems like there is a minimal size gr has to have. In my laptop (MMA v10) Dimensions[data] is always {17,periode} for every periode<13. Setting periode greater than 12 results in a rectangle on the lower left side but with false dimensions. Is 17 perhaps my 'standard line' height in pixels? In your case it would be 16 and in Dr. belisarius (counting the pixels in the image provided) about 9. $\endgroup$
    – Gypaets
    Commented Mar 18, 2016 at 22:26
  • $\begingroup$ Do you see BoxMatrix or I misunderstand your purpose? $\endgroup$
    – yode
    Commented Mar 19, 2016 at 0:38
  • $\begingroup$ Thanks for pointing out BoxMatrix. However, for arbitrary shaped graphics, which I have in mind (e.g. ring, star, whatever) the command won't be useful, wouldn't it be? $\endgroup$
    – Kay
    Commented Mar 20, 2016 at 12:37

1 Answer 1

3
$\begingroup$

As this regular we know the min height of Graphics is 16

(Graphics[{Black, Rectangle[]}, ImageSize -> {1, #}] // Binarize // 
     ImageData // Dimensions) & /@ Range@19

{{16, 1}, {16, 1}, {16, 1}, {16, 1}, {16, 1}, {16, 1}, {16, 1}, {16,1}, {16, 1}, {16, 1}, {16, 1}, {16, 1}, {16, 1}, {16, 1}, {16,1},{17, 1}, {17, 1}, {18, 1}, {19, 1}}

So we make this function like following

getmat[width_, height_] := 
 Module[{pic}, 
  pic = Graphics[{White, Rectangle[{0, 0}, {width, height}]}, 
     ImageSize -> {width, height}] // Binarize;
  If[height <= 16, ImageData@ImageResize[pic, {width, height}], 
   ImageData@pic]]

Usage:

getmat[15, 6] // MatrixForm

enter image description here

Also as the concise you can like this

getmat[width_, height_] :=ImageData@ImageResize[
   Graphics[{White, Rectangle[{0, 0}, {width, height}]}, 
     ImageSize -> {width, height}] // Binarize, {width, height}]

Then getmat[15, 6] // MatrixForm will get same result.

$\endgroup$
1
  • $\begingroup$ Interesting, it never came to my mind to think of something like a lower limit for ImageSize. Your answer is greatly helpful, as it solves both of my above questions. Thank you! $\endgroup$
    – Kay
    Commented Mar 20, 2016 at 12:29

Your Answer

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

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