Searching a matrix for duration value and replacing else
Mostrar comentarios más antiguos
I have a 1600 X 821 matrix that is full of 1 and 0's that makes a binary image. I am trying to make a statement that will cycle through each column and find a series of 0's d (duration) long and then replace all other 0's in the column with 1's regardless of if there are more series of 20 zeroes later and move on to the next column. What I have doesn't work, but here's what I have so far.
k=0;
while (1+k:d+k);
if matrix(1+k:d+k);
else matrix(1+k)= 1;
end
k=k+1;
end
Thanks
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 14 de Oct. de 2012
Matthew, try this:
clc;
format compact
%==============================================
% Make sample data.
% Make a 100 row by 4 columns image
binaryImage = true(100, 4);
% Place 30 random zeros around in it.
randomLocations = randperm(numel(binaryImage));
binaryImage(randomLocations(1:30)) = false;
% Put 2 stretches of zeros at least 20 long in each column
binaryImage(3:22, 1) = false;
binaryImage(33:62, 1) = false;
binaryImage(13:33, 2) = false;
binaryImage(73:92, 2) = false;
binaryImage(23:52, 3) = false;
binaryImage(63:86, 3) = false;
binaryImage(43:74, 4) = false;
binaryImage(81:100, 4) = false;
% Display it in the command window:
binaryImage
% Create an output image
output = true(size(binaryImage))
%=======================================
% Now, finally, we have our sample data and we can begin.
for column = 1 : size(binaryImage, 2)
% Find the zeros.
% This is a map that is true where there is a 0 (false)
zeroLocations = ~binaryImage(:, column);
% Get rid of stretches 19 or less
zeroLocations = bwareaopen(zeroLocations, 20);
% Find the first zero of the first 20+ long stretch
% of zeros in this column.
firstZeroRow = find(zeroLocations, 1, 'first')
% Set that one single pixel to 0 in the output image.
output(firstZeroRow, column);
end
% Display it in the command window:
output
% It will be all true (1's) except for pixel 3, 13, 23, and 43
% in columns 1, 2, 3, and 4 respectively.
11 comentarios
Matthew
el 14 de Oct. de 2012
Matthew
el 14 de Oct. de 2012
Matthew
el 14 de Oct. de 2012
Image Analyst
el 14 de Oct. de 2012
Editada: Image Analyst
el 14 de Oct. de 2012
You didn't copy and paste my code - I know that much. You'd have to show your modifications for me to know how you broke it. For example, you're not using a true binary (logical) image but instead are using your own image which is of type double.
Matthew
el 14 de Oct. de 2012
Image Analyst
el 14 de Oct. de 2012
OK, let me change my code to reflect your non-binary image. It is below:
clc;
format compact
%==============================================
% Make sample data.
% Make a 100 row by 4 columns image
binaryImage = ones(100, 4, 'uint8');
% Place 30 random zeros around in it.
randomLocations = randperm(numel(binaryImage));
binaryImage(randomLocations(1:30)) = 0;
% Put 2 stretches of zeros at least 20 long in each column
binaryImage(3:22, 1) = 0;
binaryImage(33:62, 1) = 0;
binaryImage(13:33, 2) = 0;
binaryImage(73:92, 2) = 0;
binaryImage(23:52, 3) = 0;
binaryImage(63:86, 3) = 0;
binaryImage(43:74, 4) = 0;
binaryImage(81:100, 4) = 0;
% Display it in the command window:
binaryImage
% Create an output image
output = ones(size(binaryImage), 'uint8')
%=======================================
% Now, finally, we have our sample data and we can begin.
for column = 1 : size(binaryImage, 2)
% Find the zeros.
% This is a map that is 1 where there is a 0
zeroLocations = ~binaryImage(:, column);
% Get rid of stretches 19 or less
zeroLocations = bwareaopen(zeroLocations, 20);
% Find the first zero of the first 20+ long stretch
% of zeros in this column.
firstZeroRow = find(zeroLocations, 1, 'first')
% Set that one single pixel to 0 in the output image.
output(firstZeroRow, column) = 0;
end
% Display it in the command window:
output
% It will be all 1's except for pixel 3, 13, 23, and 43
% in columns 1, 2, 3, and 4 respectively.
Matthew
el 14 de Oct. de 2012
Image Analyst
el 14 de Oct. de 2012
Why do you want to do this somewhat unusual thing anyway?
Matthew
el 14 de Oct. de 2012
Image Analyst
el 15 de Oct. de 2012
Do you think it could be even better by using the fact that adjacent columns may be correlated?
Matthew
el 15 de Oct. de 2012
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!