Python+OpenCV判断图像是黑底还是白底

这篇博客介绍了如何利用Python和OpenCV库判断一张图像的背景是黑色还是白色。通过图像灰度化和二值化处理,然后计算两条对角线上黑白像素点的数量,来确定背景色。提供了两种实现方式,一种进行二值化操作,另一种不进行。在main函数中可以调整输入图像和黑白像素判断阈值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

本篇博客使用Python+OpenCV判断图像是黑底还是白底,利用图像对角线上的黑白像素点个数进行判断,详情见下文。

本篇博客内容包含代码逻辑、说明、依赖、实现,这几部分。代码实现部分包含2种实现方式,1种进行二值化操作,1种不进行二值化操作。Python版本为3.6。

代码逻辑

  1. 导入OpenCV第三方库。
  2. 读入图像。
  3. 图像灰度化。
  4. 图像二值化。
  5. 获取图像2条对角线上黑白像素点个数。
  6. 比较黑白像素点个数,若黑像素多于白像素,则为黑底;否则为白底。

代码说明

  1. 代码包含2种实现方式,1种进行二值化操作,即BorW_binary()函数;1种不进行二值化操作,即BorW_not_binary()函数。
  2. main函数中可以通过imread()更改输入图像、也可通过bwThresh变量更改判断黑白像素的阈值。

代码依赖

Python == 3.6
opencv-python == 4.5.5.62

代码实现

# This script is used to judge a image is black based or white based
import cv2


# binarization
def BorW_binary(img, bwThresh):
    gray = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY)  # gray image
    ret, thresh = cv2.threshold(gray, bwThresh, 255, cv2.THRESH_BINARY)  # binarization
    # get h and w of img
    imgShape = img.shape
    h = imgShape[0]
    w = imgShape[1]
    # init black and white point number
    blackNum, whiteNum = 0, 0
    k = h / w
    for x in range(w):
        y1 = int(k * x)
        y2 = int((-k) * x + h - 1)
        # prevent overflow
        if 0 <= y1 <= (h - 1) and 0 <= y2 <= (h - 1):
            # first diagonal line
            if thresh[y1][x] == 0:
                blackNum += 1
            else:
                whiteNum += 1
            # second diagonal line
            if thresh[y2][x] == 0:
                blackNum += 1
            else:
                whiteNum += 1
    print(blackNum, whiteNum)
    if blackNum > whiteNum:
        print("black based image")
    else:
        print("white based image")


# not binary
def BorW_not_binary(img, bwThresh):
    gray = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY)  # gray image
    # ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)  # binarization
    # get h and w of img
    imgShape = img.shape
    h = imgShape[0]
    w = imgShape[1]
    # init black and white point number
    blackNum, whiteNum = 0, 0
    k = h / w
    for x in range(w):
        y1 = int(k * x)
        y2 = int((-k) * x + h - 1)
        # prevent overflow
        if 0 <= y1 <= (h - 1) and 0 <= y2 <= (h - 1):
            # first diagonal line
            if gray[y1][x] <= bwThresh:
                blackNum += 1
            else:
                whiteNum += 1
            # second diagonal line
            if gray[y2][x] <= bwThresh:
                blackNum += 1
            else:
                whiteNum += 1
    print(blackNum, whiteNum)
    if blackNum > whiteNum:
        print("black based image")
    else:
        print("white based image")


if __name__ == "__main__":
    img = cv2.imread("../img/handbag.png")
    bwThresh = 127  # threshold value of black or white
    BorW_binary(img, bwThresh)
    BorW_not_binary(img, bwThresh)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NSJim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值