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)

相关推荐
njsgcs12 分钟前
langchain+vlm示例
windows·python·langchain
勇气要爆发15 分钟前
LangGraph 实战:10分钟打造带“人工审批”的智能体流水线 (Python + LangChain)
开发语言·python·langchain
jz_ddk21 分钟前
[实战] 从冲击响应函数计算 FIR 系数
python·fpga开发·信号处理·fir·根升余弦·信号成形
醒醒该学习了!1 小时前
如何将json文件转成csv文件(python代码实操)
服务器·python·json
忘忧记1 小时前
pythonQT版本的图书管理系统
python·fastapi
一只理智恩1 小时前
AI 实战应用:从“搜索式问答“到“理解式助教“
人工智能·python·语言模型·golang
Katecat996631 小时前
输液泵设备检测与识别基于改进YOLO11模型的实现详解_ETB
python
Sirius.z1 小时前
第T7周:咖啡豆识别
python
yao12497364732 小时前
【无标题】
python·synergy·deskflow·键鼠共享·hyprland·niri
workflower2 小时前
原子需求的属性
python·测试用例·需求分析·软件需求