-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdraft_2
41 lines (34 loc) · 2.04 KB
/
draft_2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 24 21:36:45 2018
@author: usrp1
"""
from datetime import datetime
import tensorflow as tf
import numpy as np
def ssim_tf(image_1,image_2,c_1 = 0.02,c_2 = 0.03):
image_1 =tf.reshape(image_1,[1,256,256,1])
image_2 =tf.reshape(image_2,[1,256,256,1])
gauss_filter = np.array([1,4,7,4,1,4,16,26,16,4,7,26,41,26,7,4,16,26,16,4,1,4,7,4,1])/273.0
gauss_filter.reshape((5,5,1,1))##我先用的5*5的滤波器###################################
gauss_filter = tf.constant(value =gauss_filter ,shape=[5, 5, 1, 1],dtype=tf.float32,name='gauss_filter')
#gauss_filter=np.array([[1,4,7,4,1],[4,16,26,16,4],[7,26,41,26,7],[4,16,26,16,4],[1,4,7,4,1]])/273.0
#gauss_filter = tf.constant(gauss_filter,name='gauss_filter',dtype=tf.float32)
image_1_u = tf.nn.conv2d(image_1,gauss_filter, [1, 1, 1, 1], padding = 'SAME')
image_1_u2 = tf.multiply(image_1_u,image_1_u)
image_2_u = tf.nn.conv2d(image_2,gauss_filter, [1, 1, 1, 1], padding = 'SAME')
image_2_u2 = tf.multiply(image_2_u,image_2_u)
image_u1_u2 = tf.multiply(image_2_u,image_1_u)
var_image_1 = tf.nn.conv2d(tf.multiply(image_1,image_1),gauss_filter, [1, 1, 1, 1], padding = 'SAME') -image_1_u2
var_image_2 = tf.nn.conv2d(tf.multiply(image_2,image_2),gauss_filter, [1, 1, 1, 1], padding = 'SAME') -image_2_u2
var_image_12 = tf.nn.conv2d(tf.multiply(image_1,image_2),gauss_filter, [1, 1, 1, 1], padding = 'SAME') -image_u1_u2
#ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2));
# c_1 = 0.02
# c_2 = 0.03
ssim_map = tf.multiply(tf.divide((2* image_u1_u2+c_1),(image_1_u2 +image_2_u2 + c_1 )),tf.divide((2*var_image_12+c_2),(var_image_1+var_image_2+c_2)))
ssim_ch=1-tf.reduce_mean(ssim_map)
return ssim_ch
x_img = tf.placeholder(tf.float32,shape=[1, 256, 256,3],name='x_img') #输入的x域图像
y_img = tf.placeholder(tf.float32,shape=[1, 256, 256,3],name='y_img') #输入的y域图像
djj = ssim_tf(x_img[0,:,:,0],y_img[0,:,:,0])