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)

相关推荐
花酒锄作田4 分钟前
如何发布自己的 Python 库到 PyPI
python
researcher-Jiang27 分钟前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
谙忆10244 小时前
图片放大为什么会糊?从插值到AI超分,超分辨率技术是怎么演进的
人工智能·计算机视觉
飞猪~4 小时前
LangChain python 版本 第一集
开发语言·python·langchain
2601_956319885 小时前
最新AI量化提效,先做可验证的小流程
人工智能·python
开飞机的舒克_6 小时前
FastAPI 实战入门:从路由、参数校验到依赖注入的后端开发指南
python·fastapi
霸道流氓气质7 小时前
Kiro 中反编译 JAR 包并分析字节码的流程指南
chrome·python·jar
人工智能时代 准备好了吗7 小时前
AI回答内容进入率监测:引用识别、文本匹配与语义判断
开发语言·人工智能·python
yyywxk7 小时前
ICML 2026 超分辨率(super-resolution)方向上接收论文总结
计算机视觉·icml·超分辨率