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_image[morph_image == 0] = [0, 0, 0] # 将深色区域设为黑色

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

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

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

相关推荐
Metaphor6923 分钟前
使用 Python 将 Excel 转换为 PDF
python·pdf·excel
彦为君11 分钟前
长时间运行的 Agent:如何设计可靠的执行框架
python·ai·ai编程
qqqweiweiqq18 分钟前
Jetson Orin nx 无法train pi0
人工智能·python·深度学习
AAA大运重卡何师傅(专跑国道)20 分钟前
scrapling框架源码5/19
python
xingyuzhisuan24 分钟前
Jupyter Notebook 云GPU配置全解析(含实操+选型指南)
ide·python·jupyter·gpu算力
ITIRONMAN31 分钟前
开源data-compare:轻量级数据对比工具
人工智能·python
云姜.42 分钟前
如何快速使用Langchain上手编程
python·langchain
念恒123061 小时前
Python(for循环进阶)
开发语言·python
AI玫瑰助手1 小时前
Python运算符:算术运算符(加减乘除取模幂)详解
开发语言·python
情绪总是阴雨天~1 小时前
深度解析:LangChain、Agent、RAG、FC、ReAct、LangGraph、A2A、MCP — 区别、联系与全景图
python·langchain·agent·rag·langgraph·mcp·a2a