MATLAB Answers

How to create a checkerboard image without using the inbuilt function?

69 views (last 30 days)
Deep P
Deep P on 6 Jun 2016
Answered: Nipuna on 7 Sep 2020
Hello All,
I wanted to create a checkerboard image(1280x960) where each color(balck or white) is 16x16 in dimension. I do not want to use inbuilt function. Please help me on this.
Thanks.

Accepted Answer

Stephen
Stephen on 6 Jun 2016
Edited: Stephen on 6 Jun 2016
Here is one way without using any toolbox functions, or any slow (and ugly) loops:
>> imgSiz = [16,12];
>> blkSiz = [4,4];
>> numRep = imgSiz./blkSiz;
>> basMat = toeplitz(mod(0:numRep(1)-1,2),mod(0:numRep(2)-1,2));
>> outImg = repelem(basMat,[blkSiz,3])
outImg =
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0
1 1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 0 1 1 1 1
1 1 1 1 0 0 0 0 1 1 1 1
This makes a simple binary image. Obviously you need to replicate along the third dimension if you need an RGB image.
If you don't have repelem, then this FEX submission also does the job: http://www.mathworks.com/matlabcentral/fileexchange/24536-expand

More Answers (2)

John BG
John BG on 7 Jun 2016
Edited: John BG on 25 Jul 2016
Deep P
this works, no inbuilt functions:
sx=1280;sy=960;
bas=16 % length base square side
% assume sx and sy are multiples of base
Lx=(-1).^[1:1:sx/bas];
Ly=(-1).^[1:1:sy/bas];
A=ones(sx,sy);
[Ai Aj]=ind2sub([sx sy],[1:1:sx*sy]);
Ai2=reshape(Ai,[sx sy]);
Aj2=reshape(Aj,[sx sy]);
linex=[1:bas:sx];linex=[linex sx];
liney=[1:bas:sy];liney=[liney sy];
setx=zeros(1,bas);
for k=2:1:(length(linex)-1)
L1=[linex(k-1):1:(linex(k)-1)]
setx=[setx;L1];
end
setx(1,:)=[];
sety=zeros(1,bas);
for k=2:1:length(liney)-1
L2=[liney(k-1):1:liney(k)-1];
sety=[sety;L2];
end
sety(1,:)=[];
blk=ones(bas,bas);
for k=1:1:sx/bas-1
for s=1:1:sy/bas-1
Lx1=blk*Lx(k);
Ly1=blk*Ly(s);
L12=Lx1.*Ly1;
A(setx(k,:),sety(s,:))=A(setx(k,:),sety(s,:)).*L12;
end;
end;
check the result with the marker on
imshow(A)
If you find this answer of any help solving your question,
please click on the thumbs-up vote link, or mark this answer as accepted
thanks in advance
John
  1 Comment
TEGENE  Garedew
TEGENE Garedew on 23 Nov 2019
thanks that is nice but it doesn't display 8 squares only 7 squares and a white region on the left and bottom region

Sign in to comment.


Nipuna
Nipuna on 7 Sep 2020
Following function worked
call the function binerycheckerboard(1280,960)
function a = binerycheckerboard(n,y)
b = ones(n,y);
for i=0:n-1
for a=0:y-1
c = rem(i,16);
d = fix(i/16);
e = rem(d,2);
f = rem(a,16);
g = fix(a/16);
h = rem(g,2);
if e == 0 && h == 0 || e ~= 0 && h ~= 0
b(i+1,a+1) = 1;
else
b(i+1,a+1) = 0;
end
end
end
imshow(b)
imtool(b)

Community Treasure Hunt

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

Start Hunting!

Translated by