我在Matlab中实现了Niblack的算法。但是,我正在取得一些可疑的结果。
我下载了一些试图执行某些文档二值化的测试图像。但是,我不确定它们看起来是否正确。下面是图像和我的代码,我试图通过执行各种功能使其模块化。谢谢!
将k级提升至2级 结果:
我的代码
% K value
k_threshold = -0.1;
%input image
image1 = imread('test1.jpg');
%convert to grayscale
image1 = rgb2gray(image1);
%convert to double
image1 = double(image1);
% normalise to [0,1] range
image1 = image1 / max(image1(:));
%mean filtering
mean = averagefilter2(image1);
%standard deviation
standardDeviation = standardDev(image1, mean);
%calculate binary image
binaryImage = image1 >= (mean + k_threshold * standardDeviation);
%display image
display = displayImage(binaryImage, image1);
function deviation = standardDev(image1, mean)
meanSquare = averagefilter2(image1.^2);
deviation = (meanSquare - mean.^2).^0.5;
end
function [X,Y] = displayImage (binaryImage, image1)
close;
figure;
X = imshow(image1); title('original image');
figure;
Y = imshow(binaryImage); title('binary image');
end
function img=averagefilter2(image1)
meanFilter = fspecial('average',[3 3]);
img = imfilter (image1,meanFilter);
end