필터 지우기
필터 지우기

How is this code binarizing an image?

조회 수: 1 (최근 30일)
Recap
Recap 2016년 3월 25일
댓글: Recap 2016년 3월 26일
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;

채택된 답변

Guillaume
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
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.
Recap
Recap 2016년 3월 26일
Correction to the above "one line" code
bw = blockproc(img1, [30 30],@(b) b.data <= mean(b.data(:)) + 10);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by