当前位置:网站首页>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()
原网站

版权声明
本文为[论一个测试的养成]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44517891/article/details/107780338