how to create color checkerboard any size
30 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
nora elhasi
el 22 de Mzo. de 2016
should i make a black white checkerboard and convert it to coloured one? how to make it with random coloures?
0 comentarios
Respuesta aceptada
Charles Dunn
el 22 de Mzo. de 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;
Más respuestas (3)
Guillaume
el 22 de Mzo. de 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)
4 comentarios
loes henstra
el 14 de Mayo de 2021
@Guillaume when using what you did in your code, you get a checkerboard with many differnt colored squares. I am trying to make a checkerboard with only three different colors. Do you have any advise on how to change your code to manipulate the colors in the checkerboard?
Image Analyst
el 14 de Mayo de 2021
@loes henstra, post a picture of what you'd like your checkerboard to look like. (Preferably in a new question with a link back to this one.)
Image Analyst
el 22 de Mzo. de 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.
DGM
el 22 de Nov. de 2022
Editada: DGM
el 1 de Abr. de 2023
I'm going to treat this as two separate questions:
- How to create a colored checkerboard/chessboard
- How to make the squares random color
Since I wouldn't call a grid of random-colored squares a "checkerboard" pattern, I'm going to treat that as a minor tangent to the core form of the question as asked.
A Colored Checkerboard
There are a number of ways to make a colored checkerboard image. Most of these are examples I had left over from a question that got deleted last year. The next four examples begin by creating a logical mask and then constructing the color image from the mask. While IPT has checkerboard() for creating checkerboard patterns, it's relatively inflexible and does not generate a binarized image. Instead, the mask can be generated using mod() and basic logic. A simple way to convert the mask to a colored image is to treat it as an indexed image.
% using indexed image tools
% parameters
CT = [0.8 0.3 1; 0.5 0.1 0.4]; % the tile colors
squaresize = [20 20]; % the size of squares [y x]
sizeout = [200 200]; % the image size [y x]
% create a checkerboard mask
xx = mod(0:(sizeout(2)-1),squaresize(2)*2) < squaresize(2);
yy = mod(0:(sizeout(1)-1),squaresize(1)*2) < squaresize(1);
mask = xor(xx,yy');
% treat the mask as an indexed image and convert to RGB
outpict = ind2rgb(mask,CT);
imshow(outpict)
Alternatively, the mask can be used as a mask. Implicit array expansion can be leveraged to create a composite image directly from the color tuples.
% using basic compositing
% parameters
CT = [0.8 0.3 1; 0.5 0.1 0.4]; % the tile colors
squaresize = [20 20]; % the size of squares [y x]
sizeout = [200 200]; % the image size [y x]
% create a checkerboard mask
xx = mod(0:(sizeout(2)-1),squaresize(2)*2) < squaresize(2);
yy = mod(0:(sizeout(1)-1),squaresize(1)*2) < squaresize(1);
mask = xor(xx,yy');
% use the mask to create an image directly from the two color tuples
outpict = permute(CT(2,:),[1 3 2]).*mask + permute(CT(1,:),[1 3 2]).*(~mask);
imshow(outpict)
That's a bit of a mess to write to create a dang checkerboard. You can try to use IPT checkerboard() and then binarize the result, or you can use MIMT imcheckerboard() or MIMT freecb(), both of which produce (optionally) binarized output. Similarly, MIMT has tools that can simplify the compositing.
% using basic compositing with MIMT tools
% parameters
CT = [0.8 0.3 1; 0.5 0.1 0.4]; % the tile colors [BG; FG]
squaresize = [20 20]; % the size of squares [y x]
sizeout = [200 200]; % the image size [y x]
% create a checkerboard mask
mask = freecb(sizeout,squaresize);
% use the mask to create an image from the two color tuples
outpict = replacepixels(CT(2,:),CT(1,:),mask);
imshow(outpict)
But why do this by compositing at all? Using the indexed approach is less verbose and probably faster. The answer is flexibility. A compositing-based approach allows the tiles to be anything. They can present a solid color, a gradient, a texture, whatever.
% full compositing allows for extra flexibility
% parameters
CTbg = [0.8 0.3 1; 1 0.35 0.4]; % the BG gradient colors
CTfg = [0.033 0.65 0.26;0.15 0.5 0.88]; % the FG gradient colors
squaresize = [20 20]; % the size of squares [y x]
sizeout = [200 200]; % the image size [y x]
% create a checkerboard mask
mask = freecb(sizeout,squaresize);
% use the mask to create an image from two generated images
BG = lingrad([sizeout 3],[0 0; 1 1],CTbg,'linear','double'); % color gradients
FG = lingrad([sizeout 3],[1 0; 0 1],CTfg,'linear','double');
outpict = replacepixels(FG,BG,mask);
imshow(outpict)
Random Colored Squares?
I think @Guillaume basically said what I would've said. If it's random, then the need for structure is alleviated. It's not a checkerboard anymore; it's just a bunch of random colored tiles. It's easy to create random pixels. Just do that and upscale it.
% completely random colors
% parameters
squaresize = [20 20]; % the size of squares [y x]
nsquares = [10 10]; % the tiling [y x]
outpict = rand([nsquares 3]); % create small 1px/tile random image
outpict = imresize(outpict,round(squaresize.*nsquares),'nearest'); % expand to final size
imshow(outpict)
What might be less awful would be to draw the colors randomly from a colormap. That can also be done using a similar approach.
% random colors from a colormap
% parameters
squaresize = [20 20]; % the size of squares [y x]
nsquares = [10 10]; % the tiling [y x]
CT = hot(256);
sizeout = round(squaresize.*nsquares);
outpict = randi([1 size(CT,1)],nsquares); % create small 1px/tile random index image
outpict = imresize(outpict,sizeout,'nearest'); % expand to final size
outpict = ind2rgb(outpict,CT); % apply colormap
imshow(outpict)
We can combine this with the prior examples to create something which is both "random-colored" and has a clear checkerboard pattern of some sort.
% parameters
squaresize = [20 20]; % the size of squares [y x]
nsquares = [10 10]; % the tiling [y x]
CT = parula(256); % a color table to select from randomly
bgcolor = [0.2 0 0.4]; % fixed tile color
sizeout = round(squaresize.*nsquares);
outpict = randi([1 size(CT,1)],nsquares); % create small 1px/tile random index image
mask = freecb(nsquares,[1 1]); % create a small checkerboard mask
outpict(mask) = size(CT,1)+1; % set the index of the selected elements
outpict = imresize(outpict,sizeout,'nearest'); % expand to final size
outpict = ind2rgb(outpict,[CT; bgcolor]); % apply augmented colormap
imshow(outpict)
... but wait, if we can do that, then couldn't we also do this?
% recreate initial examples using upscaling method
% parameters
CT = [0.8 0.3 1; 0.5 0.1 0.4]; % the tile colors
squaresize = [20 20]; % the size of squares [y x]
nsquares = [10 10]; % the tiling [y x]
sizeout = round(squaresize.*nsquares);
outpict = freecb(nsquares,[1 1]); % create a small checkerboard mask
outpict = imresize(outpict,sizeout,'nearest'); % expand to final size
outpict = ind2rgb(outpict,CT); % apply colormap
imshow(outpict)
0 comentarios
Ver también
Categorías
Más información sobre Colormaps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!