Coloured checkerboard in Matlab

I'm using the Matlab checkerboard function to create a checkerboard. I'm interested in having the checkerboard as stripes (rather than checks) which I have figured out. I also want to change the colour of the stripes so that it's red and white or red and pink rather than black and white. I don't understand how to do this.
In the Matlab checkerboard function, black is defined as zeros(n) where n is the size and because the colour code for black in Matlab is [0 0 0], this works. But I don't get how to set this for it to produce red coloured tiles or stripes.
I have tried
red = repmat([1 0 0], 81,27)
to get red stripes for a checkerboard that I want with 81 squares. This produces an 81 x 81 matrix that looks like:
1 0 0 1 0 0 1 0 0 .... 1 0 0
1 0 0 1 0 0 1 0 0 .... 1 0 0
1 0 0 1 0 0 1 0 0 .... 1 0 0
.
.
.
1 0 0 1 0 0 1 0 0 .... 1 0 0
And it isn't red, it's just black and white stripes of varying thickness.
Can anyone help?!

 Accepted Answer

red = repmat(uint8([1 0 0]), 81,27);
cmap = [0 0 0; 1 0 0];
image(red);
colormap(cmap);

5 Comments

The code I am using is at the bottom.
In the first few lines of the code the colours black and white are defined and then "tile" is defined. The order of the colours in tile is how the checkerboard will be coloured.
The function is used like this:
K=checkmatlab(81);
imshow(K);
It's not my own code, I took the Matlab code for checkerboard and thought I could modify it to include the colour red.
black = zeros(n);
white = ones(n);
tile = [black white; white black];
I = repmat(tile,p,q)
Black and white are simply defined. I tried to define red the way you have suggested so that I had
tile = [white red; white red]
but it just shows a giant black square at the end.
function I = checkmatlab(varargin)
%CHECKERBOARD Create checkerboard image.
% I = CHECKERBOARD creates a checkerboard image composed of squares that
% have 10 pixels per side. The light squares on the left half of the
% checkerboard are white. The light squares on the right half of the
% checkerboard are gray.
%
% I = CHECKERBOARD(N) creates a checkerboard where each square has N
% pixels per side.
%
% I = CHECKERBOARD(N,P,Q) creates a rectangular checkerboard. There are P
% rows of TILE and Q columns of TILE. TILE = [DARK LIGHT; LIGHT DARK]
% where DARK and LIGHT are squares with N pixels per side. If you omit Q,
% it defaults to P and the checkerboard is square.
%
% The CHECKERBOARD function is useful for creating test images for
% geometric operations.
%
% Examples
% --------
% I = checkerboard(20);
% figure, imshow(I)
%
% J = checkerboard(10,2,3);
% figure, imshow(J)
%
% K = (checkerboard > 0.5); % creates a black and white checkerboard
% figure, imshow(K)
%
% See also CP2TFORM, IMTRANSFORM, MAKETFORM.Copyright 1993-2004 The MathWorks, Inc.
% Input-output specs
% ------------------
% N,P,Q: scalar positive integers
%
% I: real double 2D matrix
[n, p, q] = ParseInputs(varargin{:});
black = zeros(n);
white = ones(n);
tile = [black white; white black];
I = repmat(tile,p,q)
% make right half plane have light gray tiles
ncols = size(I,2);
midcol = ncols/2 + 1;
I(:,midcol:ncols) = I(:,midcol:ncols) - .3;
I(I<0) = 0;
%-------------------------------
% Function ParseInputs
%
function [n, p, q] = ParseInputs(varargin)
% defaults
n = 10;
p = 4;
q = p;
narginchk(0,3);
varNames={'N', 'P', 'Q'};
for x = 1:1:length(varargin)
validateattributes(varargin{x}, {'numeric'},...
{'integer' 'real' 'positive' 'scalar'}, ...
mfilename,varNames{x},x);
end
switch nargin
case 0
% I = CHECKERBOARD
return;
case 1
% I = CHECKERBOARD(N)
n = varargin{1};
case 2
% I = CHECKERBOARD(N,P)
n = varargin{1};
p = varargin{2};
q = p;
case 3
% I = CHECKERBOARD(N,P,Q)
n = varargin{1};
p = varargin{2};
q = varargin{3};
end
n = double(n);
p = double(p);
q = double(q);
K = checkmatlab(81);
cmap = [0 0 0; 1 0 0]; %black, and red
imshow(uint8(K), cmap);
Perhaps you would prefer,
K = checkmatlab(81);
cmap = [0 0 0; 1 0 0]; %black, and red
board = ind2rgb(uint8(K), cmap);
imshow(board)
To expand on this:
If you want to produce something that will display as color as soon as you imshow() it, then the result of the function needs to be an RGB array -- a 3 dimensional array. The code only produces 2D arrays.
The attached is what I end up with when I run this. That's the problem you see, it's not displaying as I'd like it to. There are solid red blocks in the middle instead of a consistent pattern across the board... Do you know why this may be happening?
Actually sorry! I had written some very stupid lines in the code that I forgot to comment out. Correcting them gives me the desired result. Thanks very much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!