How to create a checkerboard image without using the inbuilt function?
이전 댓글 표시
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.
댓글 수: 2
Adam
2016년 6월 6일
Almost everything is an inbuilt function, but assuming you have read the help what aspect of this are you struggling with?
Using a for loop and basic matrix indexing this seems to be relatively simple to achieve.
채택된 답변
추가 답변 (3개)
How about using only mod(), xor(), and basic operators (colon, transpose, relational)?
% parameters
squaresize = [20 20]; % the size of squares [y x]
sizeout = [200 200]; % the image size [y x]
% create a checkerboard mask
xx = mod(0:(sizeout(2)-1),squaresize(2)*2) < squaresize(2);
yy = mod(0:(sizeout(1)-1),squaresize(1)*2) < squaresize(1);
mask = xor(xx,yy');
% display it
imshow(mask)
Okay, I guess I did use imshow(), but that's not necessary to "create" the image.
I guess I'm also relying on implicit array expansion, so if I were to answer this in early 2016, I'd have to use bsxfun() or repmat().
% generalized expansion was introduced in R2016b
mask = bsxfun(@xor,xx,yy'); % works back to R2007a
Maybe that reduces the value a bit.
While this example is more complicated than simple blockwise replication using a single 2x2 checkerboard block and repmat(), it does allow the image size to be independent of the square size without adding extra steps. This makes it simple to generalize further without the introduction of new concepts.
% parameters
squaresize = [4 4]; % the size of squares [y x]
sizeout = [20 25]; % the image size [y x]
offset = [2 2]; % offset from the NW corner [y x]
% create a checkerboard mask
xx = offset(2):(sizeout(2) + offset(2) - 1);
yy = offset(1):(sizeout(1) + offset(1) - 1);
xx = mod(xx,squaresize(2)*2) < squaresize(2);
yy = mod(yy,squaresize(1)*2) < squaresize(1);
mask = xor(xx,yy');
% display it
imshow(mask)
See also:
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
TEGENE Garedew
2019년 11월 23일
thanks that is nice but it doesn't display 8 squares only 7 squares and a white region on the left and bottom region
Nipuna
2020년 9월 7일
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)
카테고리
도움말 센터 및 File Exchange에서 Blocked Images에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

