三通道彩图与灰度模式图分辨

 有的图虽然是三通道的,但其实是灰度模式,即三通道RBG相等

import cv2
import os
import numpy as np


def file_name(file_dir):
    """
    该函数的作用:获取file_dir文件夹下所有图片地址
    :param file_dir: 文件目录
    :return: 返回图片地址列表
    """
    L = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            # 其中os.path.splitext()函数将路径拆分为文件名+扩展名
            if os.path.splitext(file)[1] == '.png':
                L.append(os.path.join(root, file))
    return L


def img_color(src):
    """
    判断图像是彩图还是三通道灰度模式图
    :param src:
    :return:
    """
    h, w, c = src.shape[:]  # hwc
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)  # 灰度图
    img = np.empty(shape=(h, w, c), dtype=np.uint8)  # 创建空白模板
    # 三通道灰度模式图,即三通道RGB值相等
    for ch in range(c):
        img[:, :, ch] = gray
    # 判断是否为彩色图像,是则返回1,否则返回0

    if (src != img).sum() !=0 :
    # if (src == img).all() == 0:
    #     print("color")
        return 1
    else:
        # print("gray")
        return 0


# 输出列表中所有彩图序号
path = './2020'
L = file_name(path)
list = []
for i in L:
    src = cv2.imread(i)  # 原图
    if img_color(src) ==1:
        # print(i)
        if len(i) == 42:
            list.append(int(i[37:38]))
            # print(i[37:38])
        elif len(i) == 43:
            list.append(int(i[37:39]))
            # print(i[37:39])
        elif len(i) == 44:
            list.append(int(i[37:40]))
            # print(i[37:40])
list.sort()
print(list)

cv2.waitKey(0)
cv2.destroyAllWindows()

                
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值