How to create a chessboard?

45 ビュー (過去 30 日間)
Alber
Alber 2020 年 2 月 21 日
コメント済み: Alber 2020 年 3 月 9 日
I have created a chessboard function that goes from values 1 to -1, where its parameters are M (width of the image), N (height), F (rows), C (columns).
My problem is that for certain values of F and C, the function does not respond to me as it should, resulting in something similar to bands instead of squares. I have reviewed the Matlab documentation and have only seen the checkerboard function, which in my case is not worth it. The code of my function is shown below:
function [a] = chessboard3(N,M,C,F)
L1 = floor(M/C);
L2 = floor(N/F);
k = 1;
for m=1:L1:M
for n=1:L2:N
a(m:m+L1-1,n:n+L2-1)=ones(L1,L2)*(cos(k*pi));
k=k+1;
end
k=k+1;
end
end
The resolutions that I must prove are:
8k = 7680x4320
4k = 3840x2160
1080p = 1920x1080
Thank you very much in advance.
  1 件のコメント
Stephen23
Stephen23 2020 年 2 月 24 日
編集済み: Stephen23 2020 年 2 月 24 日
You don't need loops to generate that chessboard, e.g.:
I = 1-2*kron(toeplitz(mod(1:C,2),mod(1:F,2)),ones(fix(M/C),fix(N/F)))
or use repelem instead of kron.

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

採用された回答

Nishant Gupta
Nishant Gupta 2020 年 2 月 24 日
I am assuming that you are using plot function to create the chessboard, which is resulting in bands in the output. Instead of it, use imshow function as following :
M = 7680;
N = 4320;
C = 8;
F = 8;
I = chessboard3(N,M,C,F);
imshow(I);
function [a] = chessboard3(N,M,C,F)
L1 = floor(M/C);
L2 = floor(N/F);
k = 1;
for m=1:L1:M
for n=1:L2:N
a(m:m+L1-1,n:n+L2-1)=ones(L1,L2)*(cos(k*pi));
k=k+1;
end
k=k+1;
end
end
Refer following documentation to know more about imshow function: imshow
  1 件のコメント
Alber
Alber 2020 年 3 月 9 日
Thank you very much for your answers, in the end to solve this problem I needed to take into account all cases, for odd and even rows, for even and impart columns for non-whole subdivisions,...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by