查看骨架图像,可以注意到与文本图像相比,噪声图像中有很多分支,这看起来是可以利用的特征之一。下面显示的代码实验试图使用 OP 的图像来验证相同 -
实验代码
%%// Experiment to research what features what might help us
%%// differentiate betwen noise and text images
%%// Read in the given images
img1 = imread('noise.png');
img2 = imread('text.png');
%%// Since the given images had the features as black and rest as white,
%%// we must invert them
img1 = ~im2bw(img1);
img2 = ~im2bw(img2);
%%// Remove the smaller blobs from both of the images which basically
%%// denote the actual noise in them
img1 = rmnoise(img1,60);
img2 = rmnoise(img2,60);
%// Get the skeleton images
img1 = bwmorph(img1,'skel',Inf);
img2 = bwmorph(img2,'skel',Inf);
%%// Find blobs branhpoints for each blob in both images
[L1, num1] = bwlabel(img1);
[L2, num2] = bwlabel(img2);
for k = 1:num1
img1_bpts_count(k) = nnz(bwmorph(L1==k,'branchpoints'));
end
for k = 1:num2
img2_bpts_count(k) = nnz(bwmorph(L2==k,'branchpoints'));
end
%%// Get the standard deviation of branch points count
img1_branchpts_std = std(img1_bpts_count)
img2_branchpts_std = std(img2_bpts_count)
注意:上面的代码使用了一个函数——rmnoise,它是基于link讨论的问题构建的:
function NewImg = rmnoise(Img,threshold)
[L,num] = bwlabel( Img );
counts = sum(bsxfun(@eq,L(:),1:num));
B1 = bsxfun(@eq,L,permute(find(counts>threshold),[1 3 2]));
NewImg = sum(B1,3)>0;
return;
输出
img1_branchpts_std =
73.6230
img2_branchpts_std =
12.8417
可以看到两个输入图像的标准差之间的巨大差异,表明可以使用此功能。
在其他一些样本上运行
为了让我们的理论更具体一点,让我们使用纯文本图像并逐渐添加噪声,看看分支点的标准偏差是否有任何建议,命名为check_value。
(一)纯文字图片
check_value = 1.7461
(二)一些添加的噪声图像
check_value = 30.1453
(III) 再添加一些噪点图片
check_value = 54.6446
结论:可以看出,这个参数提供了一个很好的指标来判断图像的性质。
最终代码
可以编写一个脚本来测试另一个输入图像是文本还是噪声图像,像这样 -
%%// Parameters
%%// 1. Decide this based on the typical image size and count of pixels
%%// in the biggest noise blob
rmnoise_threshold = 60;
%%// 2. Decide this based on the typical image size and how convoluted the
%%// noisy images are
branchpts_count_threshold = 50;
%%// Actual processing
%%// We are assuming input images as binary images with features as true
%%// and false in rest of the region
img1 = im2bw(imread(FILE));
img1 = rmnoise(img1,rmnoise_threshold);
img1 = bwmorph(img1,'skel',Inf);
[L1, num1] = bwlabel(img1);
for k = 1:num1
img1_bpts_count(k) = nnz(bwmorph(L1==k,'branchpoints'));
end
if std(img1_bpts_count) > branchpts_count_threshold
disp('This is a noise image');
else
disp('This is a text image');
end