Mask making help - Divide a meshgrid in to 'boxes' and change the values of the cells in each 'box'

4 views (last 30 days)
Hi,
I am trying to divide up a meshgrid of zeros in to equal sized boxes, then turn the values of a particular box to equal 1. The idea behind this is to make a mask which can be place on top of another meshgrid so that boxes with zero values are 'not visible' and boxes with ones are 'visible'. The goal it to keep some central box 'on' and cycle through all the other boxes, turning them on and then off again individually. I have written some code which makes the mesh of zeros, 680 by 600 and then divides the mesh in to 68 by 60 boxes and can set the value of each box equal to 1, see below.
The issue I am having is that I cannot figure out a way of:
  • Firstly: adding the 1 values of a particular box back to the meshgrid i.e. box 2x3 corresponds to the mesh coordinates (21,31) to (30,40). So the Mesh would be all zeros apart from the cells the square outline by those coordinates.
  • Secondly: A way to set up the code with the features described in above but so that I could select the box I wanted to turn on at will, so for example - turn on the values in box 2x3- do something interesting- turn 2x3 back off- turn 3x3 on- ect...
Thank you for any help you can provide. I feel like I am close but just can pull the last bit together.
clear all; clc
x = 680;
y = 600;
Mesh = zeros(x , y);
[X,Y] = size(Mesh);
L = 10;
ki = 0;
kj = 0;
for i = 1 : L : X
ki = ki + 1;
for j = 1 : L : Y;
kj = kj + 1;
SubMesh = Mesh(i : (i + L - 1), j : (j + L - 1));
SomeOnes = ones( size(SubMesh) );
SomeOnesMesh = SubMesh * SomeOnes;
end
end
kii = ki
kjj = kj / kii

Accepted Answer

Image Analyst
Image Analyst on 10 Feb 2016
Some observations:
Your Mesh and SubMesh are always zero. Even if you multiply by SOmeONes, it's still zero.
Your convention of having rows (the vertical directions, dimension) be called x and the columns (the horizontal direction) be called y is contrary to the way the rest of the world does it and you might want to change that to be consistent with the rest of us and have your code be more maintainable.
You're not even using meshgrid() function so that confused me for a bit.
Beyond that I'm not really sure what you want to do. The code appears to create small arrays of zeros. It does what you tell it to do. Do you want a full size matrix where you have a patch of 1's in some certain box location?
  3 Comments
Image Analyst
Image Analyst on 10 Feb 2016
Is this what you want:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
columns = 680;
rows = 600;
ZeroImage = zeros(columns , rows);
[rows, columns] = size(ZeroImage);
L = 10;
counter = 1;
for row = 1 : L : rows
for column = 1 : L : columns;
SomeOnesMesh = ZeroImage; % Initialize
% Assign a patch of 1's.
SomeOnesMesh(row : (row + L - 1), column : (column + L - 1)) = 1;
imshow(SomeOnesMesh);
axis on;
promptMessage = sprintf('This is patch #%d.\nDo you want to Continue processing,\nor Quit processing?', counter);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
counter = counter + 1;
if strcmpi(buttonText, 'Quit')
return;
end
end
end

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 10 Feb 2016
I would use mat2cell and cell2mat to divide your mesh and reconstruct it.
height = 680; %much better name than x
width = 600; %and y
Mesh = false(height , width);
ngrid = 10;
boxheight = height / ngrid;
boxwidth = width / ngrid;
assert(mod(boxheight, 1) == 0 && mod(boxwidth, 1) == 0, 'Cannot divide Mesh evenly');
griddedmesh = mat2cell(Mesh, ones(1, ngrid) * boxheight, ones(1, ngrid) * boxwidth);
%to set a cell of griddedmesh to 0:
griddedmesh{5, 4} = false(size(griddedmesh{5, 4}));
%to set a cell of griddedmesh to 1:
griddedmesh{6, 2} = true(size(griddedmesh{5, 4}));
%to get back the full mesh:
fullmesh = cell2mat(griddedmesh);
imshow(fullmesh)
  3 Comments
Patrick Bevington
Patrick Bevington on 10 Feb 2016
Sorry I have just figured it out, it set the box size to the size of cell 5, 4 for example. In this case all the cells are the same size (10x10) so it doesn't make a difference. If that correct?
Guillaume
Guillaume on 10 Feb 2016
Edited: Guillaume on 10 Feb 2016
No, the griddedmesh{5, 4} was just an example on how to turn off a box after you've turned it on. Since all the boxes start off, it does nothing in the example.
Here is a more complete demo:
height = 680; %much better name than x
width = 600; %and y
Mesh = false(height , width);
ngrid = 10;
boxheight = height / ngrid;
boxwidth = width / ngrid;
assert(mod(boxheight, 1) == 0 && mod(boxwidth, 1) == 0, 'Cannot divide Mesh evenly');
griddedmesh = mat2cell(Mesh, ones(1, ngrid) * boxheight, ones(1, ngrid) * boxwidth);
sequence = spiral(ngrid);
figure;
for box = 1:numel(sequence)
%turn new box on
[r, c] = find(sequence == box); %not the most efficient, good enough for demo
griddedmesh{r, c} = true(size(griddedmesh{r, c}));
imshow(cell2mat(griddedmesh));
drawnow;
pause(0.05);
%then turn it back off
griddedmesh{r, c} = false(size(griddedmesh{r, c}));
end
imshow(cell2mat(griddedmesh));
I find it's much easier to let matlab do the gridding itself rather than figuring which coordinate of the whole mesh correspond to a particular grid cell.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!