Hi all, can anyone help me generate a checkerboard where i can control each square color-wise in gray scale?

1 次查看(过去 30 天)
For example i should be able to control the first square from 0 to 255 in gray scale while keeping other squares all black. Then do the same for the second square and the third and so on...
I can generate the matrix to do just that but then the scaling becomes an issue. I need my checkerboard to almost fit in the screen. For practical reasons, i would really appreciate if it was easy to manipulate the size of each pixel and the total number of squares in my pattern.
Any help is appreciated.

采纳的回答

Image Analyst
Image Analyst 2018-12-17
To control every single square, you can't use checkerboard(). You must create an array with the desired values, then use repelem() to replicate it. Not hard - I'm sure you can do it because you're a smart engineer so give it a try.

更多回答(2 个)

Mark Sherstan
Mark Sherstan 2018-12-17
编辑:Mark Sherstan 2018-12-17
Use the checkboard function save it as an image and then adjust the greyscale as desired. Here is a basic example to get you going:
k = (checkerboard(10) > 0.5);
I = uint8(255 * k);
imshow(I)

Walter Roberson
Walter Roberson 2018-12-17
编辑:Walter Roberson 2018-12-17
basic_cell = kron(eye(2), ones(pixel_height, pixel_width));
full_board = repmat(basic_cell, number_of_squares/2, number_of_squares/2);
For example,
basic_cell = kron(eye(2), ones(8,8));
full_board = rempat(basic_cell, 4, 4);
That would give you 8 squares on a side, each of which was 8 pixels by 8 pixels. The pixels would be alternating 1's and 0's.
For your purpose perhaps you would find it easier to use
full_board_cell = repmat( {0 * ones(pixel_height, pixel_width, 'uint8')}, number_of_squares, number_of_squares );
full_board = cell2mat(full_board_cell);
h = image(full_board);
colormap(gray(255));
for K = uint8(1) : uint8(255)
drawnow();
full_board_cell{1} = K * ones(pixel_height, pixel_width, 'uint8');
full_board = cell2mat(full_board_cell);
set(h, 'CData', full_board);
end

标签

Community Treasure Hunt

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

Start Hunting!

Translated by