python 图像判断,清晰度(明暗),彩色与黑白

1,判断图像清晰度,明暗,

原理,Laplacian算法。偏暗的图片,二阶导数小,区域变化小;偏亮的图片,二阶导数大,区域变化快。

import cv2

def getImageVar(imgPath):
    image = cv2.imread(imgPath)
    img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()
    return imageVar

imageVar = getImageVar("./lena.jpg")
print(imageVar)

python 图像判断,清晰度(明暗),彩色与黑白_第1张图片

输出结果:

2119.0913581351697

2,判断黑白或彩色图片

原理,通道变化

def is_color_image(url):
    im=Image.open(url)
    pix=im.convert('RGB')
    width=im.size[0]
    height=im.size[1]
    oimage_color_type="Grey Image"
    is_color=[]
    for x in range(width):
        for y in range(height):
            r,g,b=pix.getpixel((x,y))
            r=int(r)
            g=int(g)
            b=int(b)
            if (r==g) and (g==b):
                pass
            else:
                oimage_color_type='Color Image'
    return oimage_color_type

 

 

你可能感兴趣的:(python)