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

조회 수: 6 (최근 30일)
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.

답변 (2개)

John BG
John BG 2016년 7월 25일
편집: John BG 2016년 7월 25일
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

TastyPastry
TastyPastry 2016년 6월 3일
myIm = uint8(zeros(rows,cols));
myIm = cat(3,myIm+255,myIm,myIm);
imshow(myIm);

카테고리

Help CenterFile Exchange에서 Modify Image Colors에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by