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)

相关推荐
江华森1 分钟前
Python 进阶编程实战 — 从多版本环境到百万级登录系统
python
C+-C资深大佬3 分钟前
python while循环
服务器·开发语言·python
2601_9516599915 分钟前
YOLOv11 改进 - 下采样 轻量化突破:ADown 下采样让 YOLOv11 参量减、精度升
深度学习·yolo·计算机视觉
辰尘_星启20 分钟前
【三维重建】概述
计算机视觉·机器人·slam·控制·三维重建·sfm
zh路西法1 小时前
【现代控制理论与卡尔曼滤波】从状态空间到Python仿真实现
开发语言·python
Vodka~2 小时前
WSL2 + RViz GPU渲染机械臂
人工智能·python
林中青木2 小时前
OpenCV 5.0 使用方法及注意事项
人工智能·opencv·计算机视觉
8Qi82 小时前
hello-agents学习笔记--Memory让Agent拥有记忆
人工智能·python·llm·agent·ai编程·vibecoding
Esaka_Forever2 小时前
Python 完整内存管理机制详解
开发语言·python·spring
Weigang3 小时前
用 LlamaIndex 做 RAG 前,先把 Reader、Index、Retriever 的边界写清楚
人工智能·python·开源