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

27 ビュー (過去 30 日間)
Deep P
Deep P 2016 年 6 月 6 日
編集済み: DGM 2023 年 2 月 20 日
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
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.

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2016 年 6 月 6 日
編集済み: Stephen23 2016 年 6 月 6 日
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
  2 件のコメント
Adam
Adam 2016 年 6 月 6 日
toeplitz is pretty "inbuilt" though!
Image Analyst
Image Analyst 2016 年 6 月 6 日
Yes, and so is repmat() which you could use if you created one white & dark pair. Maybe it's only the "checkerboard()" function that is prohibited. http://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions

サインインしてコメントする。

その他の回答 (3 件)

DGM
DGM 2023 年 2 月 20 日
編集済み: DGM 2023 年 2 月 20 日
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:

John BG
John BG 2016 年 6 月 7 日
編集済み: John BG 2016 年 7 月 25 日
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
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
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)

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by