当前位置:网站首页>[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()
边栏推荐
- Smartcom Programming Level 4 - Magic Academy Lesson 6
- [C language] General method of base conversion
- XSS shooting range (3) prompt to win
- Postgresql 15 source code analysis (5) - pg_control
- Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
- STM32HAL库修改Hal_Delay为us级延时
- (5) final, abstract class, interface, inner class
- Can't load /home/Iot/.rnd into RNG
- 【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
- (4) Recursion, variable parameters, access modifiers, understanding main method, code block
猜你喜欢

No qualifying bean of type question

BUG消灭者!!实用调试技巧超全整理

Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held

VScode+ESP32快速安装ESP-IDF插件

MySQL数据库备份

【线性神经网络】softmax回归

已解决:不小心卸载pip后(手动安装pip的两种方式)

Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held

mysql数据库安装(详细)

C language confession code?
随机推荐
XSS shooting range (3) prompt to win
open failed: EACCES (Permission denied)
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
$parent/$children and ref
The third is the code to achieve
prompt.ml/15中<svg>标签使用解释
Understanding of the presence of a large number of close_wait states
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
打造基于ILRuntime热更新的组件化开发
关于出现大量close_wait状态的理解
MySQL fuzzy query can use INSTR instead of LIKE
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
LocalDate addition and subtraction operations and comparison size
Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
input输入框展示两位小数之precision
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
log level and print log note
强化学习:从入门到入坑再到拉屎
(Line segment tree) Summary of common problems of basic line segment tree