Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
coloured checkerboard

Subject: coloured checkerboard

From: David Epstein

Date: 17 Apr, 2012 19:10:13

Message: 1 of 6

I have a dx3 RGB matrix, and an NxM zero image matrix, and an integer k>0;
k and d are small compared with N and M. To fix the notation, call the RGB matrix "RGB" and the image matrix "Img". Let's suppose N and M are exact multiples of k. N=n*k and M=m*k. Now I want to fill my image matrix with n*m blocks, each block being of size kxk.
In a block, each pixel has the same colour from the RGB matrix. I'll suppose the colours rotate as one goes along a row, and also as one goes along a column. But it doesn't really matter, provided a simple rule is used, and the pattern is uniform.

How does one do this? I can think of lots of ways, but all of them are seem excessively complicated.

I need to end up with an NxMx3 matrix, not a matrix of indices into a colormap, please.

Subject: coloured checkerboard

From: ImageAnalyst

Date: 17 Apr, 2012 21:05:24

Message: 2 of 6

I'd probably make one colored square and one white (or black) square
stitched together. Then use repmat() to replicate that pair to
complete the checkerboard.

Subject: coloured checkerboard

From: David Epstein

Date: 17 Apr, 2012 22:27:07

Message: 3 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <35c6f6aa-5193-4431-8491-81b14c57b8e1@r9g2000yqd.googlegroups.com>...
> I'd probably make one colored square and one white (or black) square
> stitched together. Then use repmat() to replicate that pair to
> complete the checkerboard.

Thanks. I did manage to figure this out soon after posting my question, though I'm still not sure I'm doing it as efficiently as possible. Despite what I originally said, I have decided it is simpler to index into the colormap.

I have d colours, not one and not two. My procedure is to stitch together d squares, each kxk and each with its own colour, in a column. This is now a d*k by k matrix. Using circshift(columnAlreadyConstructed,k) and stitching, I get a d*k x 2*k matrix. Applying circshift(matrixAlreadyConstructed, 2*k), I get a d*k x 4*k matrix. Continue like this until 2 to the appropriate power is greater than d. Cut the matrix down to a d*k by d*k matrix. Then use repmat to fill out the matrix completely. This actually gives a matrix that will be bigger than the original MxN asked for, but one can just take the first M rows and N columns.

That's the best I can do for now. I was missing circshift when I posted my question. I need to make these chequerboards as quickly as possible, because my code uses the operation quite often.

Subject: coloured checkerboard

From: ImageAnalyst

Date: 18 Apr, 2012 02:01:05

Message: 4 of 6

David:
If you have the Image Processing Toolbox, you can use the checkerboard
function, as I show in this demo. I also show the manual repmat way.
I didn't use circshift at all.

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 30;
format compact;

% Define the size of one tile.
tileRows = 40;
tileColumns = 60;
numberOfTiles = 4;

%-----------------------------------------------------
% First use the checkerboard function
% c = checkerboard(n,p,q) creates a rectangular checkerboard
% where p specifies the number of rows and q specifies
% the number of columns. If you omit q,
% it defaults to p and the checkerboard is square.
% The light squares on the left half of the checkerboard are white.
% The light squares on the right half of the checkerboard are gray.
% checkerboard is in the Image Processing Toolbox.
cb = uint8(255 * checkerboard(tileRows, numberOfTiles,
numberOfTiles));
% Change gray to white.
cb(cb>0) = 255;
% Note: you can make it colored if you want.
subplot(1, 2, 1);
imshow(cb);
title('checkerboard() function', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Checkerboard Demo','numbertitle','off')

%-----------------------------------------------------
% Now setup one manually using repmat.
color1 = [255 0 0]; % Red.
color2 = [0 0 200]; % Purple.
% Make up one pair, color1 on the left and color2 on the right.
% Do it for each color channel individually.
onePairRed = [color1(1) * ones(tileRows, tileColumns),...
color2(1) * ones(tileRows, tileColumns)];
onePairGreen = [color1(2) * ones(tileRows, tileColumns),...
color2(2) * ones(tileRows, tileColumns)];
onePairBlue = [color1(3) * ones(tileRows, tileColumns),...
color2(3) * ones(tileRows, tileColumns)];
onePair = cat(3, onePairRed, onePairGreen, onePairBlue);
% Now flip the pair to make one going in the opposite direction.
% Make up one pair, color2 on the left and color1 on the right.
reversePair = cat(3, fliplr(onePairRed), fliplr(onePairGreen),
fliplr(onePairBlue));
% Combine these two pairs into one 2 by 2 quad.
oneQuad = [onePair; reversePair];
% Replicate the quad so we have 8 by 8 (4 by 4 quads).
checkers = repmat(oneQuad, [numberOfTiles, numberOfTiles]);
subplot(1, 2, 2);
imshow(checkers);
title('Using repmat', 'FontSize', fontSize);

Subject: coloured checkerboard

From: David Epstein

Date: 18 Apr, 2012 08:35:07

Message: 5 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <9f8bfdc6-0479-4afd-94b5-442650ff7227@w7g2000vbg.googlegroups.com>...
> David:
> If you have the Image Processing Toolbox, you can use the checkerboard
> function, as I show in this demo. I also show the manual repmat way.
> I didn't use circshift at all.

Thanks for posting the code. You made a checkerboard with only two colours (not seven). My basic image size is 1000x1000 and I want a checkerboard with 7 colours consisting of 4x4 blocks, each block in a single colour. These 7 colours should follow each other in cyclic order along each row, and in cyclic order along each column. In my 1000x1000 matrix, you will see all 7 colours in each row and all 7 colours in each column.

Subject: coloured checkerboard

From: ImageAnalyst

Date: 18 Apr, 2012 10:18:31

Message: 6 of 6

On Apr 18, 4:35 am, "David Epstein"
<David.Epstein.s...@remove.warwick.ac.uk> wrote:
> Thanks for posting the code. You made a checkerboard with only two colours (not seven). My basic image size is 1000x1000 and I want a checkerboard with 7 colours consisting of 4x4 blocks, each block in a single colour. These 7 colours should follow each other in cyclic order along each row, and in cyclic order along each column. In my 1000x1000 matrix, you will see all 7 colours in each row and all 7 colours in each column.

-----------------------------------------------------------
Luckily my code is general enough to adapt so that you have 7 colors.
It should be no problem.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us