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)

相关推荐
CTA终结者4 分钟前
期货量化下单前资金怎么核对:天勤 get_account 与可用、权益字段
python·区块链
zyl837216 分钟前
Python NumPy 学习
python·学习·numpy
山居秋暝LS29 分钟前
PaddleLabel标注注意事项_完整版
计算机视觉
装不满的克莱因瓶30 分钟前
学习使用 Python 机器学习工具 sklearn
人工智能·python·学习·机器学习·ai·agent·智能体
辣椒思密达37 分钟前
Python HTTP请求中的重试与超时控制:提升稳定性的实用方法
开发语言·python·http
Omics Pro2 小时前
3种蛋白结构输入方式!已申报欧洲发明专利
数据库·人工智能·python·机器学习·plotly
Psycho_MrZhang2 小时前
Codex 高效开发协作手册
python
HappyAcmen2 小时前
1.pdfplumber安装,PDF文字提取
python·pdf
弹简特2 小时前
【零基础学Python-收尾】10-Python第三方库的安装介绍
开发语言·python
itfallrain2 小时前
Spring 构造器循环依赖排查:@RequiredArgsConstructor + @Lazy 到底有没有生效
数据库·python·spring