当前位置:网站首页>tensorflow图片编码处理基础
tensorflow图片编码处理基础
2022-08-09 13:07:00 【论一个测试的养成】
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
class P:
def __init__(self):
#读取图片
self.imge = tf.gfile.FastGFile(r'D:\bridge.png','rb').read()
# decode_png解码png图片,从而得到图像对应的三维矩阵
self.img_after_decode = tf.image.decode_png(self.imge)
#边框的系数坐标
self.boxes = tf.constant([[[0.05,0.05,0.9,0.7],[0.2,0.3,0.5,0.5]]])
def show(self):
'''tf 显示图片'''
with tf.Session() as sess:
self.decode_image = self.img_after_decode.eval()
#plt 工具可视化的到的图片
self.plt_show(self.decode_image)
def write(self):
#对解码后的图片进行编码
img_after_encode = tf.image.encode_png(self.decode_image)
#保存图片
with tf.gfile.GFile(r'D:\app_encode.png','wb') as f:
f.write(img_after_encode.eval())
def randomimg(self):
#图片左右翻转
self.flipped = tf.image.random_flip_left_right(self.img_after_decode)
with tf.Session() as sess:
self.plt_show(self.flipped.eval())
def color(self):
'''函数会在[-max_delta,max_delta]之间随机调整图像的亮度'''
with tf.Session() as sess:
self.adjusted = tf.image.random_brightness(self.img_after_decode,max_delta=2)
self.plt_show(self.adjusted.eval())
def con(self):
'''函数会在lower upper之间随机调整图像的对比度,这2个值都不能为负 tf.image.adjust_contrast() 指定设置对比度'''
with tf.Session() as sess:
self.contrant = tf.image.random_contrast(self.img_after_decode,0.2,18)
self.plt_show(self.contrant.eval())
def hue(self):
'''调整饱和度'''
with tf.Session() as sess:
self.adjusted_hue = tf.image.adjust_hue(self.img_after_decode,0.1)
self.adjusted_hue = tf.image.adjust_hue(self.img_after_decode,0.3)
self.adjusted_hue = tf.image.adjust_hue(self.img_after_decode,0.6)
self.adjusted_hue = tf.image.adjust_hue(self.img_after_decode,0.9)
self.plt_show(self.adjusted_hue.eval())
def stand(self):
'''图像标准化是将图像的亮度均值变为0,方差变为1'''
with tf.Session() as sess:
self.standar = tf.image.per_image_standardization(self.img_after_decode)
self.plt_show(self.standar.eval())
def size(self):
'''调整图片大小'''
'''method 取值0 双线性插值法 1最近邻居法 2双三次插值法 3面积插值'''
with tf.Session() as sess:
self.resize = tf.image.resize_images(self.img_after_decode,[300,300],method=3)
self.resize = np.asarray(self.resize.eval(),dtype="uint8")
self.plt_show(self.resize)
def crop_pad(self):
'''剪裁'''
with tf.Session() as sess:
'''如果原始图像大于目标,函数以图像的中心为中心对图像进行剪裁,剪裁之后就得到了目标大小的图像'''
self.crop = tf.image.resize_image_with_crop_or_pad(self.img_after_decode,300,300) #剪裁
'''原图的大小小于目标图像函数会在原始图像的四周进行全0填充'''
self.pad = tf.image.resize_image_with_crop_or_pad(self.img_after_decode,1000,1000) #填充
self.centercrop = tf.image.central_crop(self.img_after_decode,0.4) #以中心裁剪到原始图片的4分之一, 这个值在0-1之间
#目标高宽target_height,target_width ;offset_height 距离原始图像的行数 ,offset_width距离原始图像的列数
self.bound = tf.image.crop_to_bounding_box(self.img_after_decode,offset_height=100,offset_width=100,
target_height=300,target_width=300)
self.plt_show(self.crop.eval())
self.plt_show(self.pad.eval())
self.plt_show(self.centercrop.eval())
self.plt_show(self.bound.eval())
def bound(self):
with tf.Session() as sess:
bathed = tf.expand_dims(tf.image.convert_image_dtype(self.img_after_decode,tf.float32),0)
image_boxes = tf.image.draw_bounding_boxes(bathed,self.boxes)
self.plt_show(image_boxes[0].eval())
def plt_show(self,img):
'''plt 显示图片'''
plt.imshow(img)
plt.show()
if __name__ == '__main__':
p = P()
# p.randomimg()
# p.color()
# p.con()
# p.hue()
# p.stand()
# p.size()
# p.crop_pad()
p.bound()
边栏推荐
- puzzle(016.5)逻辑电路
- gin's middleware and routing grouping
- error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 16 Assignment
- 海康设备获取YV12图像-不用rtsp
- NC192 二叉树的后序遍历
- render解析
- 蓝桥历届真题-既约分数
- The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
- NC61 两数之和
猜你喜欢

19、学习MySQL 索引

面试攻略系列(四)-- 你不知道的大厂面试

pytest 之 fixture参数化

The sword refers to the offer, cuts the rope 2

5G China unicom repeater network management protocol real-time requirements

Explanation of RTSP protocol

Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project

Periodic sharing of Alibaba Da Tao system model governance

Unity3d_API_GPS_LocationService

kustomize entry example and basic syntax instructions
随机推荐
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
昇腾AI开发者创享日南京站!一起CANN机器狗+AI机械臂实现硬核智慧救援!燃爆现场~
Oracle Recovery Tools修复空闲坏块
Process/Thread Related in Sandbox - 2
The FPGA - work summary recently
万物皆可柯里化的 Ramda.js
pytest 之 fixture的定义及作用域
glibc memory management model freeing C library memory cache
The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
JS轮播图实现
vim常用命令
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
Time series analysis course lab report
NC193 二叉树的前序遍历
FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)
RTSP协议讲解
Rmarkdown Tutorial
RTP打包发送H.264