Python+OpenCV判断图像是黑底还是白底
前言
本篇博客使用Python+OpenCV判断图像是黑底还是白底,利用图像对角线上的黑白像素点个数进行判断,详情见下文。
本篇博客内容包含代码逻辑、说明、依赖、实现,这几部分。代码实现部分包含2种实现方式,1种进行二值化操作,1种不进行二值化操作。Python版本为3.6。
代码逻辑
- 导入OpenCV第三方库。
- 读入图像。
- 图像灰度化。
- 图像二值化。
- 获取图像2条对角线上黑白像素点个数。
- 比较黑白像素点个数,若黑像素多于白像素,则为黑底;否则为白底。
代码说明
- 代码包含2种实现方式,1种进行二值化操作,即
BorW_binary()
函数;1种不进行二值化操作,即BorW_not_binary()
函数。 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判断图像是黑底还是白底相关推荐
- Python+OpenCV:图像梯度
Python+OpenCV:图像梯度(Image Gradients) 理论 OpenCV提供了三种类型的梯度滤波器或高通滤波器,Sobel, Scharr和Laplacian. 1. Sobel和S ...
- Python OpenCV 之图像乘除与像素的逻辑运算,图像处理取经之旅第 17 天
今天的学习的内容是:通过 Python OpenCV 对图像实现乘除操作,涉及函数为 cv2.multiply 与 cv2.divide.后面又补充了一些像素的逻辑运算,以及一个综合案例 cv2.mu ...
- 【Python OpenCV】图像直方图 calcHist方法 equalizeHist方法
[Python OpenCV]图像直方图 calcHist方法 equalizeHist方法 (一)图像直方图 图像的构成是有像素点构成的,每个像素点的值代表着该点的颜色(灰度图或者彩色图).所谓直方 ...
- Python+OpenCV:图像修复(Image Inpainting)
Python+OpenCV:图像修复(Image Inpainting) 理论 Most of you will have some old degraded photos at your home ...
- Python+OpenCV:图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features)
Python+OpenCV:图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features) 理论 We know SIFT us ...
- Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection)
Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection) 理论 Feature Detection using FAST Select ...
- Python+OpenCV:图像Shi-Tomasi角点检测器
Python+OpenCV:图像Shi-Tomasi角点检测器 理论 The scoring function in Harris Corner Detector was given by: Inst ...
- Python+OpenCV:图像Harris角点检测(Harris Corner Detection)
Python+OpenCV:图像Harris角点检测(Harris Corner Detection) 理论 corners are regions in the image with large v ...
- Python+OpenCV:图像对比度受限自适应直方图均衡化(CLAHE, Contrast Limited Adaptive Histogram Equalization)
Python+OpenCV:图像对比度受限自适应直方图均衡化(CLAHE, Contrast Limited Adaptive Histogram Equalization) ############ ...
最新文章
- ASP编程学习的28个观点
- oracle protocol=beq 不可用,Oracle BEQ方式连接配置
- 运用多种知识点实现一个综合小游戏
- Google邮局可以实时开通了
- 基于matplotlib的数据可视化 - 热图imshow
- 【Python环境搭建】PyCharm鼠标滚轮改变字体大小 等
- 遇到的retain cycle例子
- idea运行maven:No URLs will be polled as dynamic configuration sources
- Python知识笔记总结
- 图像处理、分析与机器视觉(基于labview)_基于3D技术的机器视觉解决方案
- Android11安装谷歌,Android 11正式版
- QQ快速登录协议的分析与漏洞利用
- 2021年2月世界编程语言排行榜
- 高中计算机兴趣班一般学什么内容,高中主要学什么课程 有哪些科目
- html5 游戏制作教程,利用HTML5 Canvas制作一个简单的打飞机游戏
- Python培训价格多少
- 央视家庭厨房节目 <天天饮食> 43道家常菜
- 数据结构与算法精选面试50题(附答案)
- android 模拟器 blue,Win下的Android模拟器BlueStacks
- Python采集全国各地百度地图上店铺POI数据(母婴、美食等)