0

我有一系列大小相同的图像,并且都是用黑色书写的标志,它们都非常简单(+,-,x,/,1-9)在一个颜色背景上,背景色改变它有时是绿色有时是蓝色,有时是红色,但始终是统一的颜色。

我正在尝试将这些图像转换为标志为黑色且背景始终为白色的黑白图像。

我这样做是为了能够比较图像以找到重复的符号。

那么如何使用 PIL 进行灰度转换。

有没有更好的方法来进行比较?

谢谢

4

3 回答 3

1

因此,只需将其转换为黑白

black_and_white = im.convert('1')

啊,你也可以使用im.getcolors(maxcolors)

http://effbot.org/imagingbook/image.htm - 这是文档

如果您的图片真的只有两种颜色,请使用im.getcolors(2),您将在此列表中只看到两个项目,然后您将能够用白色和黑色替换图像中的它们。

于 2012-11-11T19:45:17.450 回答
1

这是我要做的:

  • 计算图像颜色的中值。如果颜色真的很均匀,它应该能够正确分割它(注意不要在对图像进行阈值处理时反转黑/白颜色)。否则,对图像的颜色直方图使用更稳健的方法,例如 2-means 算法
  • 对于符号比较,我会使用 2D-crosscorralation(scipy 和 openCV 有很多有用的方法可以做到这一点)。一些提示:如何量化两个图像之间的差异?
于 2012-11-11T20:00:44.293 回答
1

你可能想看看,scipy.ndimage因为skimage这两个 python 库会让你更轻松地处理这种简单的图像比较。
让您简要了解这两个库可以做什么。

>>> from scipy.ndimage import find_objects,label
>>> import scipy.misc          
>>> img=scipy.misc.imread('filename.jpg')  
>>> labeled,number=label(img) # (label) returns the lebeled objects while  
                              # (number) returns the numer ofthe labeled signs  
>>> signs=find_objects(labeled)  #this will extract the signs in your image  
#once you got that,you can simply determine  
# if two images have the same sign using sum simple math-work.  

但是要使用上面的代码,您需要将背景设置为黑色,以便标签方法可以工作。如果您不想打扰自己将背景反转为黑色,那么您应该使用替代库skimage

>>> import skimage.morphology.label  
>>> labeled=skimage.morphology.label(img,8,255) #255 for the white background
                                                #in the gray-scale mode  
#after you label the signs you can use the wonderful method  
#`skimag.measure.regionprops` as this method will surely  
# help you decide which two signs are the same.
于 2012-11-14T00:11:19.663 回答