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)

相关推荐
xw-pp7 分钟前
回溯法的小结与概述
java·数据结构·c++·python·算法·递归
回家吃月饼18 分钟前
python中unittest框架和pytest框架区别
开发语言·python·pytest
remandancy.h24 分钟前
PyTorch(五)自动微分
人工智能·pytorch·python
网恋褙骗八万40 分钟前
tensorflow跑手写体实验
人工智能·python·tensorflow
记录&日常1 小时前
安装Anaconda + tensorflow
人工智能·python·tensorflow
小小的木头人1 小时前
使用阿里云效API操作流水线
python·阿里云·云计算
EelBarb1 小时前
python源码:opencv多视频源同屏拼接播放
python·opencv·音视频
~我在北京~2 小时前
完整篇-Python数据类型转换
linux·windows·python
木觞清2 小时前
Django学习第一天
python·学习·django
息流使用宝典2 小时前
TensorFlow 开源的机器学习框架 简介
人工智能·python·tensorflow