MATLAB Answers

How to create a checker box image of white and black pixels without using inbuilt function?

16 views (last 30 days)
Deep P
Deep P on 3 Jun 2016
Reopened: Walter Roberson on 23 Nov 2019
Hello All,
I would like to create an image, that has checker box pattern of black and white pixels.
For ex, How do I create an image of dimensions 1280x960, black and white pixels 16x16 each. Please help.

Answers (2)

John BG
John BG on 25 Jul 2016
Edited: John BG on 25 Jul 2016
Deep
this works
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;
% A(sety(s,:),setx(k,:))=A(sety(s,:),setx(k,:)).*L12;
end;
end;
check you get a chequered boar with
imshow(A)
every 16x16 block has either +1 or -1
Would you please be so kind to mark my answer as accepted answer
thanks in advance
John BG

Community Treasure Hunt

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

Start Hunting!