MATLAB Answers

how to create color checkerboard any size

51 views (last 30 days)
nora elhasi
nora elhasi on 22 Mar 2016
Commented: nora elhasi on 1 Apr 2016
should i make a black white checkerboard and convert it to coloured one? how to make it with random coloures?

Accepted Answer

Charles Dunn
Charles Dunn on 22 Mar 2016
I think you have the right idea with generating a black and white checkerboard first.
If you have a 3x3 array, you have 9 total squares. The pattern of odd/even is [1 0 1 0 1 0 1 0 1]. Now create an array of the two RGB colors: [0 0 0; 1 1 1]. I went with black and white but these could be any colors. Now, index that color array with your odd/even logical vector. With a final reshape and concatenation, you will have a checkerboard image.
N = 3;
colors = [0 0 0; 1 1 1];
inds = 1:N^2;
color_inds = 1+mod(inds,2);
r = colors(color_inds,1);
g = colors(color_inds,2);
b = colors(color_inds,3);
checkers = cat(2,r,g,b);
checkers = reshape(checkers,[N,N,3]);
imagesc(checkers);
axis equal tight;

More Answers (2)

Guillaume
Guillaume on 22 Mar 2016
A simple method would be to generate an image where each individual pixel is a random colour and of the size of the numbers of square in your checkerboard, then resize said image with nearest neighbour interpolation with the scale being the size of each square:
numsquares = [10, 15]; %rows columns
squaresize = 20;
pattern = rand([numsquares, 3]);
checkerboard = imresize(pattern, squaresize, 'nearest');
imshow(checkerboard)
  2 Comments
nora elhasi
nora elhasi on 1 Apr 2016
nawara(:,:,1)=rand(8); >> nawara(:,:,2)=rand(8); >> nawara(:,:,3)=rand(8);
>> big(:,:,1)=kron(nawara(:,:,1), ones(128)); >> big(:,:,2)=kron(nawara(:,:,2), ones(128)); >> big(:,:,3)=kron(nawara(:,:,3), ones(128));
>> imshow(big) i need to put checkerboard with rand function in this code how is that

Sign in to comment.


Image Analyst
Image Analyst on 22 Mar 2016
I already answered this checkerboard homework problem this month. Expand the comments in this link: http://www.mathworks.com/matlabcentral/answers/271727#comment_349813 to see a solution.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!