Change Minimum Distance of Zero Matrix
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Image Analyst
2016년 1월 18일
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
추가 답변 (2개)
Thorsten
2016년 1월 18일
편집: Thorsten
2016년 1월 18일
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
2016년 1월 18일
편집: Guillaume
2016년 1월 18일
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
Guillaume
2016년 1월 18일
편집: Guillaume
2016년 1월 18일
"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).
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!