<p>将图像转换为二值图像的方法基本上相当于随机抖动,这是在二元介质上产生灰度值错觉的糟糕方法。传统的印刷品是一种二元介质,几个世纪以来,他们对印刷品中的灰值照片进行了微调。这个过程被称为<a href="https://en.wikipedia.org/wiki/Halftone" rel="nofollow noreferrer">halftoning</a>,它在一定程度上是由纸上墨水的特性决定的,我们不必在二值图像中处理这些特性。在</p>
<p>那么人们在印刷品之外想出了什么方法呢?有序抖动(主要是<a href="https://en.wikipedia.org/wiki/Ordered_dithering" rel="nofollow noreferrer">Bayer matrix</a>)和{a3}。在维基百科上阅读更多关于抖动的信息。几年前我写了一篇<a href="http://www.crisluengo.net/index.php/archives/355" rel="nofollow noreferrer">blog post showing how to implement all of these methods in MATLAB</a>。在</p>
<p>我建议您在特定的应用程序中使用错误扩散抖动。以下是MATLAB中的一些关于Floyd Steinberg算法的代码(摘自我上面的博客文章),我希望你能把它翻译成Python:</p>
<pre class="lang-matlab prettyprint-override"><code>img = imread('https://i.stack.imgur.com/d5E9i.png');
img = img(:,:,1);
out = double(img);
sz = size(out);
for ii=1:sz(1)
for jj=1:sz(2)
old = out(ii,jj);
%new = 255*(old >= 128); % Original Floyd-Steinberg
new = 255*(old >= 128+(rand-0.5)*100); % Simple improvement
out(ii,jj) = new;
err = new-old;
if jj<sz(2)
% right
out(ii ,jj+1) = out(ii ,jj+1)-err*(7/16);
end
if ii<sz(1)
if jj<sz(2)
% right-down
out(ii+1,jj+1) = out(ii+1,jj+1)-err*(1/16);
end
% down
out(ii+1,jj ) = out(ii+1,jj )-err*(5/16);
if jj>1
% left-down
out(ii+1,jj-1) = out(ii+1,jj-1)-err*(3/16);
end
end
end
end
imshow(out)
</code></pre>
<p><a href="https://i.stack.imgur.com/PdWf8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PdWf8.png" alt="enter image description here"/></a></p>
<p>在应用抖动之前对图像进行重新采样可以大大改善结果:</p>
^{pr2}$
<p><a href="https://i.stack.imgur.com/3Z02u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Z02u.png" alt="enter image description here"/></a></p>
<p><strong>注意</strong>上述过程期望输入在[0255]范围内。很容易适应不同的范围,比如[01328]或[0,1],但也很容易将图像缩放到[0255]范围。在</p>