使用Python/PIL检测图像是彩色,灰度还是黑白 [英] Detect if image is color, grayscale or black and white with Python/PIL

查看:1765
本文介绍了使用Python/PIL检测图像是彩色,灰度还是黑白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从jpeg格式的PDF文件中提取页面图像,我需要确定每个图像是否是灰度级更高,彩色还是黑白(具有容差系数).

I extract pages images from a PDF file in jpeg format and I need to determine if each image is much more grayscale, color ou black and white (with a tolerance factor).

我发现了一些使用PIL进行颜色检测的方法(此处此处),但我可以t想出如何回答这个简单的(视觉)问题:是黑白图像,彩色图像还是灰度图像?

I have found some ways to work with color detection with PIL ( here and here ) but I can't figure out how to answer this simple (visual) question : is it much more black and white, color or grayscale image ?

对于这部分,我更喜欢使用Python和PIL,但如果有人有线索(或解决方案),我也可以使用OpenCV.

I prefer working with Python and PIL for this part but I could use too OpenCV if someone has a clue (or solution).

推荐答案

我尝试了Gepeto的解决方案,它有很多误报,因为颜色大方差可能只是偶然而相似.正确的方法是计算每个像素的方差.首先缩小图像,这样您就不必处理数百万个像素.

I tried Gepeto's solution and it has a lot of false positives since the color grand variances can be similar just by chance. The correct way to do this is to calculate the variance per pixel. Shrink down the image first so you don't have to process millions of pixels.

默认情况下,此功能还使用平均色彩偏差调整,我发现它可以改善预测.这样做的副作用是它还将检测单色但非灰度的图像(通常是棕褐色的东西,该模型在检测到较大的灰度偏差时似乎会崩溃一些).您可以通过对色带均值进行阈值处理,将它们与真正的灰度分开.

By default this function also uses a mean color bias adjustment, which I find improves the prediction. A side effect of this is that it will also detect monochrome but non-grayscale images (typically sepia-toned stuff, the model seems to break down a little in detecting larger deviations from grayscale). You can separate these out from true grayscale by thresholding on the color band means.

我在13,000张摄影图像的测试集上进行了此测试,并以99.1%的精度和92.5%的召回率进行了分类.通过使用非线性偏差调整(例如,颜色值必须在0到255之间),精度可能会进一步提高.也许看中位数平方误差而不是MSE会更好地允许例如带有小颜色标记的灰度图像.

I ran this on a test set of 13,000 photographic images and got classification with 99.1% precision and 92.5% recall. Accuracy could probably be further improved by using a nonlinear bias adjustment (color values must be between 0 and 255 for example). Maybe looking at median squared error instead of MSE would better allow e.g. grayscale images with small color stamps.

from PIL import Image, ImageStat
def detect_color_image(file, thumb_size=40, MSE_cutoff=22, adjust_color_bias=True):
    pil_img = Image.open(file)
    bands = pil_img.getbands()
    if bands == ('R','G','B') or bands== ('R','G','B','A'):
        thumb = pil_img.resize((thumb_size,thumb_size))
        SSE, bias = 0, [0,0,0]
        if adjust_color_bias:
            bias = ImageStat.Stat(thumb).mean[:3]
            bias = [b - sum(bias)/3 for b in bias ]
        for pixel in thumb.getdata():
            mu = sum(pixel)/3
            SSE += sum((pixel[i] - mu - bias[i])*(pixel[i] - mu - bias[i]) for i in [0,1,2])
        MSE = float(SSE)/(thumb_size*thumb_size)
        if MSE <= MSE_cutoff:
            print "grayscale\t",
        else:
            print "Color\t\t\t",
        print "( MSE=",MSE,")"
    elif len(bands)==1:
        print "Black and white", bands
    else:
        print "Don't know...", bands

这篇关于使用Python/PIL检测图像是彩色,灰度还是黑白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆