Logic for replacing numbers surrounded by zeros for a file of 350x350
Show older comments
Hi,
I have matrix of 350x350 which contains all 1's and 0's, Now i have to replace all the set of 1's which are surrounded by 0's with some random number(that number should be same through out the set) and the random number for every set should be different and Note: even column one and row one should be treated like a boundary example. Please help me with building the code.
0000111000000111000
11111100000111111000
1111100000011111000
00000000000000000000
should change to
0 0 0 0 0 45 45 0 0 0 00 0 0 0 0 60 60 60 0 0 0 0
45 45 45 45 45 45 0 0 0 0 0 60 60 60 60 60 60 000
45 45 45 45 45 0 0 0 0 0 0 0 60 60 60 60 60 00 000
00000000000000000000000000000000000000000
Thanks in Advance,
Agastya
Answers (2)
Image Analyst
on 27 Nov 2014
If you have the IMage Processing Toolbox, this will do exactly what you asked for:
binaryImage = [...
0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0
1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0
1 1 1 1 1 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 0 0 0]
% Label.
[labeledImage, numberOfRegions] = bwlabel(binaryImage);
% Now assign random numbers.
largestRandomNumber = 100; % Whatever you want.
% Get the length that the look up table needs to be for thie data type.
N = double(intmax('uint16')) + 1;
% Get random numbers in a look up table.
randomNumbers = uint16(randi(largestRandomNumber, 1, N));
randomNumbers(1) = 0; % Don't change the 0's.
% Apply the look up table to the labeled image.
finalImage = intlut(uint16(labeledImage), randomNumbers)
3 Comments
Agastya
on 27 Nov 2014
Image Analyst
on 27 Nov 2014
You have to buy it. You can get a free download by asking them for a trial. It also comes bundled with one flavor of the student edition https://www.mathworks.com/academia/student_version/ - the MATLAB and Simulink Student Suite.
If you don't have / can't get the imaging toolbox, it's not particularly difficult to implement bwlabel yourself. See this answer for some details.
I assume you mean each body of connected ones surrounded by 0s should be replaced by a number (as oppose to individual 1s that are surrounded entirely by 0s)?
If so it sounds like a simple body-labelling problem for which
doc bwlabel
would do the job (if you have Image Processing toolbox).
That will label the bodies with (I assume) consecutive numbered labels starting at 1, but then once the bodies have been identified with a unique label you can trivially assign a random value to each body if you don't want the consecutive labels as e.g.
labelledBodies = bwlabel( inputMatrix );
labelledBodies( labelledBodies == 1 ) = 45;
labelledBodies( labelledBodies == 2 ) = 60;
etc
There is almost certainly a neater way to do the substitution rather than that, but it'll do for the example.
Categories
Find more on Univariate Discrete Distributions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!