tensorflow实验十-----图片翻转与缩放

本文通过使用 TensorFlow 和 Matplotlib 等库展示了图像处理的基本操作,包括读取图像、调整大小、裁剪、填充、随机裁剪、翻转、对比度调整及标准化等。此外还介绍了如何利用自定义卷积核实现图像边缘检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入库

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np

读取照片
这里的路径是自己存放图片的路径

image_data=tf.keras.preprocessing.image.load_img(r'E:\10\1.jpg','rb')

可视化

plt .imshow(image_data)
plt.show()

在这里插入图片描述

img_data=tf.keras.preprocessing.image.img_to_array(image_data)
resized=tf.image.resize(img_data,[256,256])
print("Digital type:",resized.dtype)
resized1=np.asarray(resized,dtype='uint8')
plt.imshow(resized1)
plt.show()
print("Digital type:",resized1.dtype)

在这里插入图片描述

resized2=tf.image.resize(img_data,[256,512],method='nearest')
resized2=np.asarray(resized2,dtype='uint8')
plt.imshow(resized2)
plt.show()

在这里插入图片描述

resized3=tf.image.resize(img_data,[256,256],method='bicubic')
resized3=np.asarray(resized3,dtype='uint8')
plt.imshow(resized3)
plt.show()

在这里插入图片描述

resized3=tf.image.resize(img_data,[256,256],method='area')
resized3=np.asarray(resized3,dtype='uint8')
plt.imshow(resized3)
plt.show()

在这里插入图片描述

croped=tf.image.resize_with_crop_or_pad(img_data,100,100)
croped=np.asarray(croped,dtype='uint8')
plt.imshow(croped)
plt.show()

在这里插入图片描述

padded=tf.image.resize_with_crop_or_pad(img_data,600,600)
padded=np.asarray(padded,dtype='uint8')
plt.imshow(padded)
plt.show()

在这里插入图片描述

random_croped1=tf.image.random_crop(img_data,[200,200,3])
random_croped1=np.asarray(random_croped1,dtype='uint8')
plt.imshow(random_croped1)
plt.show()

在这里插入图片描述

img_data=np.asarray(img_data,dtype='uint8')
plt.imshow(img_data)
plt.show()
flip_left_right=tf.image.flip_left_right(img_data)
flip_left_right=np.asarray(flip_left_right,dtype='uint8')
plt.imshow(flip_left_right)
plt.show()

在这里插入图片描述

img_data=np.asarray(img_data,dtype='uint8')
plt.imshow(img_data)
plt.show()
flip_up_down=tf.image.flip_up_down(img_data)
img_data=np.asarray(img_data,dtype='uint8')
plt.imshow(flip_up_down)
plt.show()

在这里插入图片描述

img_data=np.asarray(img_data,dtype='uint8')
plt.imshow(img_data)
plt.show()
contrast=tf.image.random_contrast(img_data,lower=0.2,upper=3)
contrast=np.asarray(contrast,dtype='uint8')
plt.imshow(contrast)
plt.show()

在这里插入图片描述

img_data=np.asarray(img_data,dtype='uint8')
plt.imshow(img_data)
plt.show()
standardization=tf.image.per_image_standardization(img_data)
standardization=np.asarray(standardization,dtype='uint8')
plt.imshow(np.asarray(standardization,dtype='uint8'))
plt.show()

在这里插入图片描述

import numpy as np
from PIL import Image

def imgConvolve(image_array,kernel):
    '''参数说明:
       image_array:原灰度图像矩阵
       kernel :卷积核
       返回值:原图像与算子进行卷积后的结果
    '''
    image_arr = image_array.copy()
    img_dim1,img_dim2 = image_arr.shape
    k_dim1,k_dim2 = kernel.shape
    AddW = int((k_dim1-1)/2)
    AddH = int((k_dim2-1)/2)
    
    temp = np.zeros([img_dim1 + AddW*2,img_dim2 + AddH*2])
    temp[AddW:AddW+img_dim1,AddH:AddH+img_dim2]= image_arr[:,:]
    output = np.zeros_like(a=temp)
    
    for i in range(AddW,AddW+img_dim1):
        for j in range(AddH,AddH+img_dim2):
            output[i][j]= int(np.sum(temp[i-AddW:i+AddW+1,j-AddW:j+AddW+1]*kernel))
            
    return output[AddW:AddW+img_dim1,AddH:AddH+img_dim2]
kernel_1=np.array(
[[-1,0,1],
[-1,9,2],
[-1,0,1]])
kernel_2=np.array(
[[-1,-2,-1],
[0,0,0],
[1,2,1]])
kernel_3=np.array(
[[1,1,1],
[1,-8,1],
[1,1,1]])
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

%matplotlib inline

image = Image.open("E:/10/22.png").convert("L")

image_array = np.array(image)

sobel_x = imgConvolve(image_array,kernel_1)
sobel_y = imgConvolve(image_array,kernel_2)
laplace = imgConvolve(image_array,kernel_3)

plt.imshow(image_array,cmap=cm.gray)
plt.axis("off")
plt.show()

plt.imshow(sobel_x,cmap=cm.gray)
plt.axis("off")
plt.show()       


plt.imshow(sobel_y,cmap=cm.gray)
plt.axis("off")
plt.show()       


plt.imshow(laplace,cmap=cm.gray)
plt.axis("off")
plt.show()     

在这里插入图片描述
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值