Change Minimum Distance of Zero Matrix

1 visualización (últimos 30 días)
Angga Lisdiyanto
Angga Lisdiyanto el 18 de En. de 2016
Editada: Angga Lisdiyanto el 19 de En. de 2016
How to change a low distance of non-zero matrix with a custom value? Example :
m = [0 0 0 0 1 2 2 0 0 5 3 4 5 0 0 0 0 0 9 9 0 0 0];
The result that i want is : [0 0 0 0 1 2 2 1 1 5 3 4 5 0 0 0 0 0 9 9 0 0 0]
So the minimum count (ie. < 3) of zero pixel would become 1.
Thanks in advance.

Respuesta aceptada

Image Analyst
Image Analyst el 18 de En. de 2016
Assuming you have the widely-held Image Processing Toolbox, you can do this very simply in 2 lines of code. Just call bwareaopen() to find stretches of zeros that are less than your desired length. Then assign those stretches to 1 (or whatever number you want). Here is the code:
m = [0 0 0 0 1 2 2 0 0 5 3 4 5 0 0 0 0 0 9 9 0 0 0];
% Step 1: Find stretches of m 2 or less in length.
short0s = xor((m==0), bwareaopen(m==0, 3))
% Step 2: Assign those elements to 1
m(short0s) = 1
  2 comentarios
Angga Lisdiyanto
Angga Lisdiyanto el 19 de En. de 2016
Editada: Angga Lisdiyanto el 19 de En. de 2016
Thankyou very much for your help. This code is very simple and great. :)
Angga Lisdiyanto
Angga Lisdiyanto el 19 de En. de 2016
Editada: Angga Lisdiyanto el 19 de En. de 2016
I accept this as an answer because of it's simplest code.
Thanks

Iniciar sesión para comentar.

Más respuestas (2)

Thorsten
Thorsten el 18 de En. de 2016
Editada: Thorsten el 18 de En. de 2016
m = [0 0 0 0 1 2 2 0 0 5 3 4 5 0 0 0 0 0 9 9 0 0 0];
custom_value = 1;
ind = diff([1 m 1] == 0);
i1 = find(ind == 1);
i2 = find(ind == -1);
ii = find(i2 - i1 == min(i2-i1));
m(i1(ii):i2(ii)-1) = custom_value;
In the general case where there is more than one sequence with the minimum number of zeros, replace the last line with
i1_i2 = cell2mat(arrayfun(@(x) (i1(x):i2(x)-1), ii, 'UniformOutput', false));
m(i1_i2) = custom_value;

Guillaume
Guillaume el 18 de En. de 2016
Editada: Guillaume el 18 de En. de 2016
This is a variation on the common problem of finding the length of sequences in a vector. This is typically solved by using diff to find the start and end of the sequences. Alternatively, you could search for run length encoding on the fileexchange.
m = [0 0 0 0 1 2 2 0 0 5 3 4 5 0 0 0 0 0 9 9 0 0 0];
thresholdlength = 3;
%convert into a sequence of 0 and 1
seqs = m == 0;
%find transitions into the sequence
diffseqs = diff([0 seqs 0]); %because of the padding, 1st transition will always be positive, last always negative
%a +1 in diffseqs indicates the start of a run of 0 in the original vector
seqstart = find(diffseqs == 1);
%a -1 in diffseqs indicates one past the end of a run of 0 in the original vector
seqsend = find(diffseqs == -1);
%because of the padding in the diff call, it's guaranteed that there'll be the same number of +1 and -1
seqslength = seqsend - seqstart;
for seqcount = 1:numel(seqslength)
if seqslength(seqcount) < thresholdlength
m(seqstart(seqcount) : seqsend(seqcount) - 1) = 1; %replace too short sequence by 1
end
end
edit: made it explicit that the threshold length is user input.
  3 comentarios
Guillaume
Guillaume el 18 de En. de 2016
Editada: Guillaume el 18 de En. de 2016
"This does not work if the minimum count is larger than 3." is a bit strong considering the OP question is ambiguous. I've read "How to change a low distance of non-zeros" as the length of zero runs is arbitrarily defined (for example 3). You've read "the minimum count" as the length of zero runs is the minimum.
Regardless, our answer is the same algorithm and only differ in the threshold selection. Mine is a constant value entered by the user, yours is the shortest sequence(s).
Angga Lisdiyanto
Angga Lisdiyanto el 19 de En. de 2016
Thank you for your answer. :)

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by