Python:一个挑选黑色棋盘的程序

import cv2

import numpy as np

读取输入图像

image = cv2.imread('grid_origin.png')

将图像从 BGR 转换为灰度图

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

设定阈值,保留深色矩形模块

thresh_value = 100

ret, thresholded_image = cv2.threshold(gray_image, thresh_value, 255, cv2.THRESH_BINARY)

使用形态学操作进一步处理图像

kernel = np.ones((5, 5), np.uint8)

morph_image = cv2.morphologyEx(thresholded_image, cv2.MORPH_CLOSE, kernel)

在原始图像上绘制保留的深色矩形模块

result_image = image.copy()

获取图像的高度和宽度

height, width, channels = result_image.shape

print("图像的宽度为: ", width)

print("图像的高度为: ", height)

white_image = np.ones((height, width, 3), dtype=np.uint8) * 255

result_image = white_image

result_imagemorph_image == 0 = 0, 0, 0 # 将深色区域设为黑色

显示保留深色矩形模块的图像

保存带有深色矩形模块的图像

cv2.imwrite("result_image.png", result_image)

相关推荐
ATMQuant8 分钟前
量化指标解码21:逻辑回归支撑阻力 - 让逻辑回归给关键价位打分
python·机器学习·逻辑回归·量化交易·技术指标
q03718026 分钟前
深色 IDE 截图 OCR 老是漏字,我加了一层预处理,顺手把识别结果做成了可搜索历史
python
蜗牛87544 分钟前
个人机票监控开源项目:6 个平台、10 条通道的自托管方案
python
秋名山小白2 小时前
Playwright UI 自动化测试实战教程
python·ui
AI源码3 小时前
基于 Python 的汽车市场数据分析与可视化平台
python·数据分析·汽车
会飞锦鲤3 小时前
基于 SVM + HOG 的铸造缺陷检测系统
python·opencv·机器学习·计算机视觉·缺陷检测·图像分类·svm
starrysky8104 小时前
zipfile 中文乱码修复:把已知 GBK 旧 ZIP 解压到正确中文目录
python
用户91294046848424 小时前
Codex 从零到能交付(09):让 Codex 写有用的测试,而不是只测成功路径
python
2601_967987374 小时前
Debian如何配置Python调试工具
python·debian·pdb·调试工具·ipdb
MoRay4 小时前
放弃云端 Computer Use,我用 Python 手搓了一个带“物理防呆”的本地 GUI Agent 底座
python·github