前言

本篇博客使用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 imageret, thresh = cv2.threshold(gray, bwThresh, 255, cv2.THRESH_BINARY)  # binarization# get h and w of imgimgShape = img.shapeh = imgShape[0]w = imgShape[1]# init black and white point numberblackNum, whiteNum = 0, 0k = h / wfor x in range(w):y1 = int(k * x)y2 = int((-k) * x + h - 1)# prevent overflowif 0 <= y1 <= (h - 1) and 0 <= y2 <= (h - 1):# first diagonal lineif thresh[y1][x] == 0:blackNum += 1else:whiteNum += 1# second diagonal lineif thresh[y2][x] == 0:blackNum += 1else:whiteNum += 1print(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 imgimgShape = img.shapeh = imgShape[0]w = imgShape[1]# init black and white point numberblackNum, whiteNum = 0, 0k = h / wfor x in range(w):y1 = int(k * x)y2 = int((-k) * x + h - 1)# prevent overflowif 0 <= y1 <= (h - 1) and 0 <= y2 <= (h - 1):# first diagonal lineif gray[y1][x] <= bwThresh:blackNum += 1else:whiteNum += 1# second diagonal lineif gray[y2][x] <= bwThresh:blackNum += 1else:whiteNum += 1print(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 whiteBorW_binary(img, bwThresh)BorW_not_binary(img, bwThresh)

Python+OpenCV判断图像是黑底还是白底相关推荐

  1. Python+OpenCV:图像梯度

    Python+OpenCV:图像梯度(Image Gradients) 理论 OpenCV提供了三种类型的梯度滤波器或高通滤波器,Sobel, Scharr和Laplacian. 1. Sobel和S ...

  2. Python OpenCV 之图像乘除与像素的逻辑运算,图像处理取经之旅第 17 天

    今天的学习的内容是:通过 Python OpenCV 对图像实现乘除操作,涉及函数为 cv2.multiply 与 cv2.divide.后面又补充了一些像素的逻辑运算,以及一个综合案例 cv2.mu ...

  3. 【Python OpenCV】图像直方图 calcHist方法 equalizeHist方法

    [Python OpenCV]图像直方图 calcHist方法 equalizeHist方法 (一)图像直方图 图像的构成是有像素点构成的,每个像素点的值代表着该点的颜色(灰度图或者彩色图).所谓直方 ...

  4. Python+OpenCV:图像修复(Image Inpainting)

    Python+OpenCV:图像修复(Image Inpainting) 理论 Most of you will have some old degraded photos at your home ...

  5. Python+OpenCV:图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features)

    Python+OpenCV:图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features) 理论 We know SIFT us ...

  6. Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection)

    Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection) 理论 Feature Detection using FAST Select ...

  7. Python+OpenCV:图像Shi-Tomasi角点检测器

    Python+OpenCV:图像Shi-Tomasi角点检测器 理论 The scoring function in Harris Corner Detector was given by: Inst ...

  8. Python+OpenCV:图像Harris角点检测(Harris Corner Detection)

    Python+OpenCV:图像Harris角点检测(Harris Corner Detection) 理论 corners are regions in the image with large v ...

  9. Python+OpenCV:图像对比度受限自适应直方图均衡化(CLAHE, Contrast Limited Adaptive Histogram Equalization)

    Python+OpenCV:图像对比度受限自适应直方图均衡化(CLAHE, Contrast Limited Adaptive Histogram Equalization) ############ ...

最新文章

  1. ASP编程学习的28个观点
  2. oracle protocol=beq 不可用,Oracle BEQ方式连接配置
  3. 运用多种知识点实现一个综合小游戏
  4. Google邮局可以实时开通了
  5. 基于matplotlib的数据可视化 - 热图imshow
  6. 【Python环境搭建】PyCharm鼠标滚轮改变字体大小 等
  7. 遇到的retain cycle例子
  8. idea运行maven:No URLs will be polled as dynamic configuration sources
  9. Python知识笔记总结
  10. 图像处理、分析与机器视觉(基于labview)_基于3D技术的机器视觉解决方案
  11. Android11安装谷歌,Android 11正式版
  12. QQ快速登录协议的分析与漏洞利用
  13. 2021年2月世界编程语言排行榜
  14. 高中计算机兴趣班一般学什么内容,高中主要学什么课程 有哪些科目
  15. html5 游戏制作教程,利用HTML5 Canvas制作一个简单的打飞机游戏
  16. Python培训价格多少
  17. 央视家庭厨房节目 <天天饮食> 43道家常菜
  18. 数据结构与算法精选面试50题(附答案)
  19. android 模拟器 blue,Win下的Android模拟器BlueStacks
  20. Python采集全国各地百度地图上店铺POI数据(母婴、美食等)

热门文章

  1. 【DL】血与泪的炼丹心得
  2. 柱状图折线图混合使用
  3. 基于交换芯片的五元组过滤功能
  4. 计算机毕业设计基于asp.net的旅游网站
  5. 【ArcGIS Pro二次开发】(15):用地用海名称和代码互转
  6. 天池竞赛-资金流入流出预测总结
  7. pd.concat()和pd.merge()
  8. QT离线安装包下载地址
  9. 实验二 实现中点分割直线段裁剪算法
  10. 新川抖音短视频营销平台是怎么样帮助企业运营抖音蓝V号的?