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)

相关推荐
一方热衷.4 小时前
YOLO26-Seg ONNXruntime C++/python推理
开发语言·c++·python
YMWM_5 小时前
如何将包路径添加到conda环境lerobot的python路径中呢?
人工智能·python·conda
田里的水稻5 小时前
ubuntu22.04_openclaw_ROS2
人工智能·python·机器人
梁正雄6 小时前
Python前端-2-css练习
前端·css·python
wefly20176 小时前
开发者效率神器!jsontop.cn一站式工具集,覆盖开发全流程高频需求
前端·后端·python·django·flask·前端开发工具·后端开发工具
6+h6 小时前
【java】基本数据类型与包装类:拆箱装箱机制
java·开发语言·python
GDAL7 小时前
MANIFEST.in简介
linux·服务器·前端·python
MoRanzhi12037 小时前
pillow 图像合成、透明叠加与蒙版处理
python·计算机视觉·pillow·图片处理·图像合成·透明叠加·多图层叠加
双叶8367 小时前
(Python)Python爬虫入门教程:从零开始学习网页抓取(爬虫教学)(Python教学)
后端·爬虫·python·学习
泥壳AI7 小时前
[特殊字符] OpenClaw + 飞书集成超详细教程
人工智能·python·深度学习·阿里云·飞书