当前位置:网站首页>【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()
边栏推荐
- Safety 20220709
- C# 实现PLC的定时器
- MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
- type_traits metaprogramming library learning
- Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
- (四)递归、可变参数、访问修饰符、理解 main 方法、代码块
- MySQL based operations
- interprocess communication
- Safety 20220722
- [shell basics] determine whether the directory is empty
猜你喜欢
Postgresql 15 source code analysis (5) - pg_control
MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
The third is the code to achieve
递归实现汉诺塔问题
C language from entry to such as soil, the data store
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Summary of Huawei Distributed Storage FusionStorage Knowledge Points [Interview]
随机推荐
MySQL based operations
Learning DAVID Database (1)
Hand in hand to realize the picture preview plug-in (3)
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
HCIP Day 10_BGP Route Summary Experiment
VScode+ESP32快速安装ESP-IDF插件
【wpf】wpf中的那些模板之深度解析
Safety 20220718
The third is the code to achieve
idea工程明明有依赖但是文件就是显示没有,Cannot resolve symbol ‘XXX‘
Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
微信小程序使用云函数更新和添加云数据库嵌套数组元素
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
Basic knowledge of mysql (2)
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
From scratch, a mirror to the end, a pure system builds a grasscutter (Grasscutter)
进程间通信
重磅 | 开放原子校源行活动正式启动
ClickHouse: Setting up remote connections