简介

数字图像处理大作业,相位图去噪,相位图解包裹,最后得到无包裹相位图。数据集及代码都有,有需要的可以联系我。这是第一步去噪。
用自编码器对相位图去噪,效果:
在这里插入图片描述

加载数据

# -*- coding:utf-8 -*-
# author:zzm
# datetime:2022/1/5 12:52
import numpy as np
import cv2
import tensorflow as tf
import torch

def load_data():
    train_datax=np.ones([4000,256,256,1])
    train_datay=np.ones([4000,256,256,1])
    test_datax=np.ones([1000,256,256,1])
    test_datay=np.ones([1000,256,256,1])
    for i in range(4000):
             fn = "data/wrapped_phase_with_noise/wrapphase_{}.jpg".format(i+1)
             np1 = torch.tensor(cv2.imread(fn, cv2.IMREAD_GRAYSCALE)).numpy()
             train_datax[i]=np1.reshape([256,256,1])

    #print(train_datax)
    for j in range(4000):
            fn1 = "data/wrapped_phase/wrap_{}.jpg".format(j+1)
            np1=torch.tensor(cv2.imread(fn1, cv2.IMREAD_GRAYSCALE)).numpy()
            train_datay[j] =np1.reshape([256,256,1])
    for m in range(4000, 5000):
            fn2 = "data/wrapped_phase_with_noise/wrapphase_{}.jpg".format(m+1)
            np1=torch.tensor(cv2.imread(fn2, cv2.IMREAD_GRAYSCALE)).numpy()
            test_datax[m-4000] = np1.reshape([256,256,1])
    for n in range(4000, 5000):
            fn3 = "data/wrapped_phase/wrap_{}.jpg".format(n+1)
            np1== torch.tensor(cv2.imread(fn3, cv2.IMREAD_GRAYSCALE)).numpy()
            test_datay[n - 4000]=np1.reshape([256,256,1])
    #train_datax=tf.convert_to_tensor(train_datax,dtype=tf.float32)
   # train_datay=tf.convert_to_tensor(train_datay,dtype=tf.float32)
    #test_datax=tf.convert_to_tensor(test_datax,dtype=tf.float32)
    #test_datay=tf.convert_to_tensor(test_datay,dtype=tf.float32)

    train_datax=train_datax.astype('float32')/255
    train_datay = train_datay.astype('float32')/255
    test_datax = test_datax.astype('float32')/255
    test_datay = test_datay.astype('float32')/255
    return  train_datax,train_datay,test_datax,test_datay

训练数据

import matplotlib.pyplot as plt
from keras.layers import Conv2D,MaxPooling2D,Input,UpSampling2D
from keras.models import Model
from keras.callbacks import TensorBoard
import tensorflow as tf
from load_data import load_data
import pickle
#加载数据,其中训练维度为4000x256x256,测试数据维度1000x256x256
train_datax,train_datay,test_datax,test_datay=load_data()
input_img = Input(shape=(256,256,1))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
tf.keras.layers.BatchNormalization()
x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
tf.keras.layers.BatchNormalization()
x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
tf.keras.layers.BatchNormalization()
x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
tf.keras.layers.BatchNormalization()
x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
tf.keras.layers.BatchNormalization()
decoded = MaxPooling2D((2, 2), padding='same')(x)


# at this point the representation is (7, 7, 32)

x = Conv2D(128, (3, 3), activation='relu', padding='same')(decoded)
tf.keras.layers.BatchNormalization()
x = UpSampling2D((2, 2))(x)

x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
tf.keras.layers.BatchNormalization()
x = UpSampling2D((2, 2))(x)

x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
tf.keras.layers.BatchNormalization()
x = UpSampling2D((2, 2))(x)

x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
tf.keras.layers.BatchNormalization()
x = UpSampling2D((2, 2))(x)

x = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
tf.keras.layers.BatchNormalization()
encode = UpSampling2D((2, 2))(x)

 
autoencoder = Model(input_img, encode)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
 
autoencoder.fit(train_datax, train_datay,
                epochs=200,
                batch_size=32,
                shuffle=True,
                validation_data=(test_datax, test_datay),
                callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0, write_graph=False)])

obj1=pickle.dumps(autoencoder)
with open("userinfo","ab")as f:
    f.write(obj1)

预测数据

# -*- coding:utf-8 -*-
# author:zzm
# datetime:2022/1/5 19:15
import matplotlib.pyplot as plt
from keras.layers import Conv2D,MaxPooling2D,Input,UpSampling2D
from keras.models import Model
from keras.callbacks import TensorBoard
import tensorflow as tf
from load_data import load_data
import pickle
#加载数据,其中训练维度为4000x256x256,测试数据维度1000x256x256
train_datax,train_datay,test_datax,test_datay=load_data()

f=open("userinfo","rb")
autoencoder = pickle.load(f)

new_imgs = autoencoder.predict(test_datax[0:10])
#print(new_imgs)
new_imgs=new_imgs.squeeze()
test_datax_squeeze=test_datax.squeeze()
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(test_datax_squeeze[i])
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    ax = plt.subplot(2, n, i + n + 1)
    plt.imshow(new_imgs[i])
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

结语

联系我q1010798057

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐