How is this code binarizing an image?
1 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
Guillaume
2016-3-26
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 个评论
Walter Roberson
2016-3-26
"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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!