matlab morphological closing with imdilate and imerode - matlab

In the the matlab documentation there is this sentence about closing an image with imclose:
The morphological close operation is a dilation followed by an
erosion, using the same structuring element for both operations
I tried it out but the result is not the same. Can sombody tell me how I can close an image only with imdilate and imerode?
I = [0 0 0 0 0 0 0 0;
0 0 1 1 1 0 0 0;
0 0 0 1 1 1 0 0;
0 1 1 1 0 0 1 0;
0 0 1 0 0 1 1 0;
0 1 0 0 1 0 1 0;
0 0 1 0 1 0 1 0;
0 0 0 0 0 0 0 0];
Q = [0 1 0; 0 1 0; 0 1 1];
Q_n = [1 1 0; 0 1 0; 0 1 0];
J = imclose(I,Q)
D = imdilate(I,Q);
S = imerode(D,Q)
Closed result:
0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0
0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
0 1 0 0 1 1 1 0
0 0 1 0 1 1 1 0
0 0 0 0 0 0 0 0
dilate and then erode:
0 0 1 1 1 0 0 0
0 0 1 1 1 0 0 0
0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
0 1 0 0 1 1 1 1
0 0 1 0 1 1 1 1
0 0 1 0 1 1 1 1

Related

Simulate obstacles in logical matrix

I'm writing an optimal path planning algorithm for one of my projects, and am a bit stuck on how to generate the map. In order to test my algorithm, I want to generate random maps that the object will pass through with obstacles in the way. To generate the maps I am making a logical array with "1" for wall or obstacle and "0" when there is nothing in the way. I am having some issues placing the obstacles however.
For now, this is what I've written in MATLAB (though I think the core idea can be solved in any language):
% lenx = length in x-direction of map [m]
% leny = length in y-direction of map [m]
% numObs = number of obstacles
% obsLoc = 2D array with the central location(s) of the obstacle(s)
% [x1, y1; x2 y2; ...]
% obsSize = size of the obstacles (all same size) [m]
% assume the obstacles are square.
% res = resolution of the map [cell/m]
%
% Outputs:
% map = 2D logical array which translates into a map
%
%---------------------------------------------------------
function map = createMap(lenx, leny, numObs, obsLoc, obsSize, res)
%Making map of the given size
map = zeros(leny*res, lenx*res);
%Adding walls
map(:,1) = 1;
map(:, lenx*res) = 1;
map(1,:) = 1;
map(leny*res, :) = 1;
%Recalculating obstacle size for this grid
obsSize = obsSize - 1;
%Adding obstacles
for i = 1:1:numObs
map(obsLoc(i,1)*res-obsSize*res:obsLoc(i,1)*res+obsSize*res,...
obsLoc(i,2)*res-obsSize*res:obsLoc(i,2)*res+obsSize*res) = 1;
end
end
The obsSize parameter is representing the dimension of one of the sides of the square obstacle. Here is a sample solution I ran:
>> createMap(10,10,1,[5,5],1,1)
ans =
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
Looks good! But the issue begins for obstacle sizes and resolutions that are not 1 or 2...
>> createMap(10,10,1,[5,5],0.5,2)
ans =
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1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 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
For the first one, we would expect there to be a 1 somewhere close to the middle, but it's nowhere to be found. In the second one, we'd expect to find 6 ones in each direction (3m * 3cell/m = 6 cell), but we are getting 8 (It's also possible that this is not what my code is calculating, but this is what I'm hoping it does. Neither case is working, and I think it has to do with this line:
Recalculating obstacle size for this grid
obsSize = obsSize - 1;
I'm pretty sure I do have to recalculate the object size so it fits to the grid, but how can I do this for any grid and obstacle? Is there a way to scale that "-1" such that it acts according to the size of the grid/obstacle?
Let me know what you come up with, and if there's any other glaring issues in my code?
Thanks!

MxN matrix with ones and zeros following a specific rule

I want to create a MxN matrix as shown below:
[1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
I have window size, let's say, 5 and it moves 3 in every row. Is it possible to create such a matrix without using for loops? Or is there any optimum way to do it?
This is a one line solution:
reshape([reshape([ones(5,6);zeros(21,6)], 1,[]), ones(1,5)],[],7).'
note:
The desired matrix can be seen as concatenation of a [6, 5+21] matrix:
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
and a [1 ,5] matrix:
1 1 1 1 1
that reshaped to a [7 , 23] matrix.
Other solution using repelem + bsxfun + accumarray:
r = repelem (1:7,5);
c= bsxfun(#plus, ((1:5)-3).',3*(1:7));
out = accumarray([r(:) c(:)] ,1)
Indices of rows and columns of 1 s can be generated and accumarray can be used to create the desired matrix.

MATLAB: how to "equally distribute" the Trues in each column of a full lower triangular logical matrix over the columns of m new matrices?

My first question on stackoverflow! The title is vague, so let me elaborate: I have a NxN lower triangular logical matrix
N = 10 % for example
L = tril(true(N),-1)
L =
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 0
with all trues below the diagonal. For a m=2^p a power of 2, I want to end up with m NxN lower triangular logical matrices L_1, ..., L_m such that each column of L_i contains the i-th 1/m-th (rounded) number of the Trues in the corresponding column in L. One consequence is that \sum_i(L_i) == L again.
For example, for m = 2 I know that
L_2 = L(:,ceil((N:2*N-1)/2))
L_2 =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 1 1 0
L_1 = L - L_2
L_1 =
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0
0 1 1 1 1 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
will do the trick, but this trick does not generalize to higher powers of 2 for m. Any ideas how to do this reasonably fast for general N and m = 2^p?
(Context: each column of L are logical indices for a bisection type algorithm. Every next power p of m = 2^p corresponds to a deeper level of the bisection algorithm)

character or pattern recognition [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to make a character recognition using linear network but I'm getting some error when running my code, anyone who can help me with a single or basic explanation or how I can go about it? below is my code
A1 = [ 0 0 1 1 0 0 0;
0 0 0 1 0 0 0;
0 0 0 1 0 0 0;
0 0 1 0 1 0 0;
0 0 1 0 1 0 0;
0 1 1 1 1 1 0;
0 1 0 0 0 1 0;
0 1 0 0 0 1 0;
1 1 1 0 1 1 1];
B1 = [ 1 1 1 1 1 1 1;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 1 1 1 1 1 1;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 1 1 1 1 1 1];
C1 = [ 0 0 1 1 1 1 1;
0 1 0 0 0 0 1;
1 0 0 0 0 0 0;
1 0 0 0 0 0 0;
1 0 0 0 0 0 0;
1 0 0 0 0 0 0;
1 0 0 0 0 0 0;
0 1 0 0 0 0 1;
0 0 1 1 1 1 0];
A2 = [ 0 0 0 1 0 0 0;
0 0 0 1 0 0 0;
0 0 0 1 0 0 0;
0 0 1 0 1 0 0;
0 0 1 0 1 0 0;
0 1 0 0 0 1 0;
0 1 1 1 1 1 0;
0 1 0 0 0 1 0;
0 1 0 0 0 1 0];
B2 = [ 1 1 1 1 1 1 0;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 1 1 1 1 1 0;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 0 0 0 0 0 1;
1 1 1 1 1 1 0];
C2 = [ 0 0 1 1 1 0 0;
0 1 0 0 0 1 0;
1 0 0 0 0 0 1;
1 0 0 0 0 0 0;
1 0 0 0 0 0 0;
1 0 0 0 0 0 0;
1 0 0 0 0 0 1;
0 1 0 0 0 1 0;
0 0 1 1 1 0 0];
p = [A1(1:end); B1(1:end); C1(1:end)]';
t = [A2(1:end); B2(1:end); C2(1:end)]';
net = newlin(minmax(p),1);
net.trainParam.goal = 10e-5;
net.trainParam.epochs = 500;
net = train(net, p, t);
my error is on line 62 and the code on line 62 is
net = train(net, p, t);
anyone with a good example or how i can make this code run?thanks in advance im trying to learn and im new to matlab
I ran the code and the error states: Output data size does not match net.outputs{1}.size.
Check the format for matrix sizes. I think that is the issue.

Vertcat error in matlab

When I run a code it shows error using vertcat in the marked line. What does it mean and how to change it?
f = [-10 -20 -40 -10 -10 80 70 90];
A = [ 1 1 2 1 3 0 0 0 -----> vertcat error
2 1 3 2 2 0 0 0
1 2 1 1 0 0 0 0
-1 0 0 0 0 1 1 1
0-1 0 0 0 1 1 1
0 0-1 0 0 1 0 0
0 0 0-1 0 0 1 0
0 0 0 0-1 0 0 1];
b = [800 600 900 0 0 0 0 0];
ub = [inf inf inf inf inf 90 57 93];
lb = zeros(8,1);
[x fval]=linprog(-f,A,b,[],[],lb,ub);
A was missing some spaces. This should work:
A = [ 1 1 2 1 3 0 0 0;
2 1 3 2 2 0 0 0;
1 2 1 1 0 0 0 0;
-1 0 0 0 0 1 1 1;
0 -1 0 0 0 1 1 1;
0 0 -1 0 0 1 0 0;
0 0 0 -1 0 0 1 0;
0 0 0 0 -1 0 0 1]
A =
1 1 2 1 3 0 0 0
2 1 3 2 2 0 0 0
1 2 1 1 0 0 0 0
-1 0 0 0 0 1 1 1
0 -1 0 0 0 1 1 1
0 0 -1 0 0 1 0 0
0 0 0 -1 0 0 1 0
0 0 0 0 -1 0 0 1