当前位置:网站首页>[py script] batch binarization processing images
[py script] batch binarization processing images
2022-07-31 04:44:00 【jingzilideniu】
参考:pythonImplement batch image binarization
Note in the blog above阈值类型表的介绍:
cv2.THRESH_BINARY
cv2.THRESH_BINARY_INV
cv2.THRESH_TRUNC
cv2.THRESH_TOZERO
cv2.THRESH_TOZERO_INV
single image processing:
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)
Input one output six results:
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()
Batch processing of pictures in a folder:
注意:
1.The route should preferably be in English
2. Slightly modified from the original blog
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()
边栏推荐
- SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
- WeChat applet uses cloud functions to update and add cloud database nested array elements
- (6) Enumeration and annotation
- MySQL模糊查询可以使用INSTR替代LIKE
- Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
- 【云原生】DevOps(五):集成Harbor
- Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
- Can‘t load /home/Iot/.rnd into RNG
- 慧通编程第4关 - 魔法学院第6课
- three.js 制作3D相册
猜你喜欢
The input input box displays the precision of two decimal places
MySQL database must add, delete, search and modify operations (CRUD)
binom二项分布,
Basic knowledge of mysql (2)
MySQL based operations
[Swift] Customize the shortcut that pops up by clicking the APP icon
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
STM32HAL library modifies Hal_Delay to us-level delay
强化学习:从入门到入坑再到拉屎
LocalDate addition and subtraction operations and comparison size
随机推荐
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
PCL 计算点云坐标最值及其索引
"DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction" paper notes
问题1:给你1-10的列表,实现列表输出,单数在左边,双数在右边。
【wpf】wpf中的那些模板之深度解析
Hand in hand to realize the picture preview plug-in (3)
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
HCIP第十天_BGP路由汇总实验
ENSP,划分VLAN、静态路由,三层交换机综合配置
【C语言进阶】文件操作(一)
WeChat applet uses cloud functions to update and add cloud database nested array elements
MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
The input input box displays the precision of two decimal places
prompt.ml/15中<svg>标签使用解释
qlib架构
打造基于ILRuntime热更新的组件化开发
马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
MySQL database must add, delete, search and modify operations (CRUD)
MySQL数据库增删改查(基础操作命令详解)
Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析