当前位置:网站首页>【py脚本】批量二值化处理图像
【py脚本】批量二值化处理图像
2022-07-31 04:36:00 【jingzilideniu】
注意上面博客中阈值类型表的介绍:
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
单个图片处理:
import cv2
img = cv2.imread("166dian.jpg")
print(img)
# 先进行灰度化处理,再进行二值化
Grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 127是二值化阈值,大于255的像素值都置为0
ret, thresh = cv2.threshold(Grayimg, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite('166dian1.jpg', thresh)
输入一个输出六个结果:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img=cv2.imread('166dian.jpg')
# 中值滤波
# img = cv2.medianBlur(img, 5)
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh1=cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY)
ret,thresh2=cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3=cv2.threshold(GrayImage,127,255,cv2.THRESH_TRUNC)
ret,thresh4=cv2.threshold(GrayImage,127,255,cv2.THRESH_TOZERO)
ret,thresh5=cv2.threshold(GrayImage,127,255,cv2.THRESH_TOZERO_INV)
titles = ['Gray Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [GrayImage, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray') #两行,三列,序号 出图
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
某文件夹中图片批量处理:
注意:
1.路径最好要全英文
2. 根据原博客稍微改动
import os
import cv2
from PIL import Image
def binarization():
# 获取目录下所有图片名
filename = os.listdir(r"F:\lianxi\lianxi\py\input")#F:\python_Demo\DeepLearning\tools3\shapes\cmutestGT
print(filename)
# os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表。
base_dir = r"F:\lianxi\lianxi\py\input" # input
new_dir = r"F:\lianxi\lianxi\py\output" # output
for img in filename:
name = img
path1 = os.path.join(base_dir, img)
print(name)
img = cv2.imread(path1)
#print(img)
Grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(Grayimg, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite('name.jpg', thresh)
image = Image.open('name.jpg')
# 有需要可对图像进行大小调整
# image = image.resize((350, 350),Image.ANTIALIAS)
path = os.path.join(new_dir, name)
image.save(path)
binarization()
边栏推荐
- (六)枚举、注解
- 【SemiDrive源码分析】【MailBox核间通信】44 - 基于Mailbox IPCC RPC 实现核间通信(RTOS侧 IPCC_RPC Server 消息接收及回复 原理分析篇)
- ClickHouse:设置远程连接
- 扫雷游戏(c语言写)
- Exsl file preview, word file preview web page method
- Explanation of
- Redis counts new and retained users
- MySQL数据库必会的增删查改操作(CRUD)
- STM32HAL库修改Hal_Delay为us级延时
- Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
猜你喜欢
MySQL数据库必会的增删查改操作(CRUD)
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
WPF WPF 】 【 the depth resolution of the template
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
XSS shooting range (3) prompt to win
BUG消灭者!!实用调试技巧超全整理
两个地址池r2负责管地址池r1负责管dhcp中继
随机推荐
MySQL模糊查询可以使用INSTR替代LIKE
重磅 | 开放原子校源行活动正式启动
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
关于出现大量close_wait状态的理解
简易网络文件拷贝的C实现
Unity Fighter
open failed: EACCES (Permission denied)
微信小程序使用云函数更新和添加云数据库嵌套数组元素
[Linear Neural Network] softmax regression
ENSP,划分VLAN、静态路由,三层交换机综合配置
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
How Zotero removes auto-generated tags
Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held
[shell basics] determine whether the directory is empty
问题7:列表的拼接
慧通编程第4关 - 魔法学院第6课
The BP neural network
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
On Governance and Innovation | 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum was successfully held
type_traits metaprogramming library learning