0

I'm using the Matlab function checkerboard to create a checkerboard and then display it as a circle rather than a square or rectangle. I have written the code below to do this but because my meshgrid seems to be so coarse, when I do imshow(checks) you can see that the edges of the circle are jagged and not at all smooth. Could anyone tell me how to overcome this problem?

Alternatively, the reason why I have had to set such a small meshgrid is that I need the K matrix generated from checkerboard to be really small as I want there to display less of the checkerboard to make it appear as though the squares have a bigger distance. If anybody knows of a way of doing this without creating a meshgrid, that will also work.

This is part of my script that uses Psychtoolbox so I'm a little bit restricted in what I can do. Once I have created checks I use it to generate a texture to draw up to the screen while scaling it up to make it bigger.

Can anyone help?

Code:

  K=checkerboard(9); % using Matlab checkerboard function to create a checkerboard
  K=K(1:27,1:27); % using a small part of the checkerboard as I want to have a wide distances between the lines
  cmap = [0.48 0.48 0.48; 0.54 0.54 0.54]; % colour map to make the colour grey
  bw1 = ind2rgb(uint8(K), cmap);
  white = 1;
  grey = white/2;
  rcycles = 8;

   % Now we make our checkerboard pattern
   xylim = 1;
   [x,y] = meshgrid(-1.25:0.0932:1.25,-1.25:0.0932:1.25);

  checks = bw1;
  circle = x.^2 + y.^2 <= xylim^2;
  checks = circle .* checks + grey * ~circle;

  imshow(checks);
Maheen Siddiqui
  • 539
  • 8
  • 19
  • As I see it, it will be perfectly smooth only when you literally take every single real number between `[-1.25, 1.25]` which clearly would not be possible. You may like the result if you use something like `K=checkerboard(40); K=K(1:269,1:269);` and the meshgrid interval as `-1.25:0.00932:1.25`. btw it gives you this: https://i.stack.imgur.com/uqdhP.jpg it seems smooth but if you zoom it, you will see the same problem because you can't consider all the points between `[-1.25, 1.25]` – Sardar Usama Aug 04 '17 at 19:48

1 Answers1

1

(Late answer, but perhaps someone might find it helpful.)

It seems to me that to achieve a texture without jagged edges you just need to rescale the checkerboard pattern before applying the circular aperture. You can do that easily with the repelem function in matlab:

K=checkerboard(9); % using Matlab checkerboard function to create a checkerboard
K=K(1:27,1:27); % using a small part of the checkerboard as I want to have a wide distances between the lines
cmap = [0.48 0.48 0.48; 0.54 0.54 0.54]; % colour map to make the colour grey
bw1 = ind2rgb(uint8(K), cmap);

% this scale factor indicate by how much the checkerboard size is increased
scale_factor = 23;

bw1 = repelem(bw1,scale_factor,scale_factor);

white = 1;
grey = white/2;
rcycles = 8;

% Now we make our checkerboard pattern
xylim = 1;
[x,y] = meshgrid(linspace(-1.25,1.25, 27*scale_factor),linspace(-1.25,1.25, 27*scale_factor));

checks = bw1;
circle = x.^2 + y.^2 <= xylim^2;

checks = repmat(circle,1,1,3) .* checks + grey * ~repmat(circle,1,1,3);
imshow(checks);

Result: enter image description here

matteo
  • 311
  • 1
  • 9