Azzera filtri
Azzera filtri

How is this code binarizing an image?

1 visualizzazione (ultimi 30 giorni)
Recap
Recap il 25 Mar 2016
Commentato: Recap il 26 Mar 2016
Is there anyone way someone would be able to explain to me what these for loops are doing to turn that image into binary?
%binarization
% im1 being the orignal image that is passed to this function
Radius=30;
intA=10;
lengthV=length(img1(:,1)); % vertical length
lengthH=length(img1(1,:)); horizontal length
for offV=1:lengthV/Radius % Vertical Offset
for offH=1:lengthH/Radius % Horizontal offset
% Convert region of radius Radius in a row
for i=1:Radius
for j=1:Radius
Obl((i-1)*Radius+j)=img1(j+(offV*Radius-Radius),i+(offH*Radius-Radius));
end
end
Obl;
Thres=mean(Obl)+intA; % Threshold
for i=1:Radius
for j=1:Radius
if img1(j+(offV*Radius-Radius),i+(offH*Radius-Radius)) > Thres
bw(j+(offV*Radius-Radius),i+(offH*Radius-Radius))=0;
else bw(j+(offV*Radius-Radius),i+(offH*Radius-Radius))=1;
end
end
end
end
end
bw;

Risposta accettata

Guillaume
Guillaume il 26 Mar 2016
The code divides the image into 30x30 square block (size is given by the badly named Radius). It calculates the mean intensity of each block, adds 10 (value given by intA) and uses that as a threshold. Anything in the block above the threshold is converted to black, everything else to white.
The whole code is badly written, obviously by someone who's not very good at matlab: convoluted way of calculating the size of the image, slow and unnecessary loops, badly named variables, lack of preallocation, useless if, and more
You can replace the entirety of the code with just one line:
bw = blockproc(img1, [30 30],(@b) b.data <= mean(b.data(:)) + 10);
  3 Commenti
Walter Roberson
Walter Roberson il 26 Mar 2016
"b" is the dummy parameter for the anonymous function
(@b) b.data <= mean(b.data(:)) + 10
That is, b is whatever value that blockproc passed in to the anonymous function. See blockproc for information about the struct that it passes to anonymous functions.
Recap
Recap il 26 Mar 2016
Correction to the above "one line" code
bw = blockproc(img1, [30 30],@(b) b.data <= mean(b.data(:)) + 10);

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by