def test_ssim_multichannel():
    N = 100
    X = (np.random.rand(N, N) * 255).astype(np.uint8)
    Y = (np.random.rand(N, N) * 255).astype(np.uint8)

    S1 = ssim(X, Y, win_size=3)

    # replicate across three channels.  should get identical value
    Xc = np.tile(X[..., np.newaxis], (1, 1, 3))
    Yc = np.tile(Y[..., np.newaxis], (1, 1, 3))
    S2 = ssim(Xc, Yc, multichannel=True, win_size=3)
    assert_almost_equal(S1, S2)

    # full case should return an image as well
    m, S3 = ssim(Xc, Yc, multichannel=True, full=True)
    assert_equal(S3.shape, Xc.shape)

    # gradient case
    m, grad = ssim(Xc, Yc, multichannel=True, gradient=True)
    assert_equal(grad.shape, Xc.shape)

    # full and gradient case
    m, grad, S3 = ssim(Xc, Yc, multichannel=True, full=True, gradient=True)
    assert_equal(grad.shape, Xc.shape)
    assert_equal(S3.shape, Xc.shape)

    # fail if win_size exceeds any non-channel dimension
    assert_raises(ValueError, ssim, Xc, Yc, win_size=7, multichannel=False)
def test_ssim_patch_range():
    N = 51
    X = (np.random.rand(N, N) * 255).astype(np.uint8)
    Y = (np.random.rand(N, N) * 255).astype(np.uint8)

    assert(ssim(X, Y, win_size=N) < 0.1)
    assert_equal(ssim(X, X, win_size=N), 1)
Example #3
0
def get_distance_matrix(img_dir):
    f_list, IS_IMG = get_f_list(img_dir)

    os.chdir(img_dir)
    N = len(f_list)
    dm = np.ones((N, N))
    if IS_IMG:
        # distance matrix, n by n init to zeros
        for i_tuple in itertools.combinations(range(len(f_list)), 2):
            i, j = i_tuple
            img1 = cv2.imread(f_list[i])
            img2 = cv2.imread(f_list[j])
            # to grey scale
            img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
            img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

            s = 1 - ssim(img1, img2)    # so that distance makes sense

            # symmetric matrix!
            # not sparse anymore!!!!
            dm[i][j] = s
            dm[j][i] = s
    else:
        for i_tuple in itertools.combinations(range(len(f_list)), 2):
            i, j = i_tuple
            i_dat = get_csv_array(f_list[i])
            j_dat = get_csv_array(f_list[j])

            s = 1-ssim(i_dat, j_dat)

            dm[i][j] = s
            dm[j][i] = s
    return dm
def svd_compress_ssim(img, target_ss):
	"""Compress image by finding k that is closest to target ssim.
	Since rank and ssim relationship is linear, we do a 
	binary search, followed by finer grained linear search"""
	rank = min(img.shape[0], img.shape[1])
	left = 1
	right = rank
	last_ss = 100
	k = 1
	compressed = None
	U, singular_vals, V = linalg.svd(img)
	# binary search
	while left < right:	
		k = (left + right) / 2
		S_p = np.zeros((k, k), img.dtype)
		for i in range(k):
			S_p[i][i] = singular_vals[i]
		compressed = combine(U[:,:k], S_p, V[:k,:])
		ss = ssim(img, compressed,
			dynamic_range=compressed.max()-compressed.min())
		if abs(ss - target_ss) < abs(last_ss - target_ss):
			last_ss = ss
			if ss > target_ss:
				right = k
			else:
				left = k
		else:
			break
	# more fine grained linear search
	if last_ss < target_ss:
		while 1:
			S_p = np.zeros((k + 1, k + 1), img.dtype)
			for i in range(k + 1):
				S_p[i][i] = singular_vals[i]
			compressed = combine(U[:,:k+1], S_p, V[:k+1,:])
			ss = ssim(img, compressed,
				dynamic_range=compressed.max()-compressed.min())
			if abs(ss - target_ss) < abs(last_ss - target_ss):
				last_ss = ss
				k += 1	
			else:
				break
	else:
		while 1:
			S_p = np.zeros((k - 1, k - 1), img.dtype)
			for i in range(k - 1):
				S_p[i][i] = singular_vals[i]
			compressed = combine(U[:,:k-1], S_p, V[:k-1,:])
			ss = ssim(img, compressed,
				dynamic_range=compressed.max()-compressed.min())
			if abs(ss - target_ss) < abs(last_ss - target_ss):
				last_ss = ss
				k -= 1
			else:
				break	
	print "Best k found %r with ssim %r" % (k, last_ss)
	return compressed
def test_mssim_mixed_dtype():
    mssim = ssim(cam, cam_noisy)
    with expected_warnings(['Inputs have mismatched dtype']):
        mssim_mixed = ssim(cam, cam_noisy.astype(np.float32))
    assert_almost_equal(mssim, mssim_mixed)

    # no warning when user supplies data_range
    mssim_mixed = ssim(cam, cam_noisy.astype(np.float32), data_range=255)
    assert_almost_equal(mssim, mssim_mixed)
def test_ssim_image():
    N = 100
    X = (np.random.random((N, N)) * 255).astype(np.uint8)
    Y = (np.random.random((N, N)) * 255).astype(np.uint8)

    S0 = ssim(X, X, win_size=3)
    assert_equal(S0, 1)

    S1 = ssim(X, Y, win_size=3)
    assert(S1 < 0.3)
def test_ssim_grad():
    N = 30
    X = np.random.random((N, N)) * 255
    Y = np.random.random((N, N)) * 255

    f = ssim(X, Y, dynamic_range=255)
    g = ssim(X, Y, dynamic_range=255, gradient=True)

    assert f < 0.05
    assert g[0] < 0.05
    assert np.all(g[1] < 0.05)
    def find_flat(self, image, flat):
        best = [0, 0]
        for f in range(flat.shape[2]):
            if cut:
                rms = ssim(image, flat[:, :, f])
            else:
                rms = ssim(image[a:c, b:d], flat[a:c, b:d, f])

            if rms > best[0]:
                best = [rms, f]

        arr = image / flat[:, :, best[1]]
        return arr
Example #9
0
def do_all_metrics(comparison_dict):
    
    pairs_lvl0 = [k for k in comparison_dict.iterkeys()]
    
    for p in pairs_lvl0:
        
        data_keys = [j for j in comparison_dict[p].iterkeys()]
        regex = re.compile('2')
        snow = [string for string in data_keys if re.match(regex, string)]
        
        comparison_dict[p]['MSE'] = round(mse(comparison_dict[p][data_keys[0]],
                                        comparison_dict[p][data_keys[1]]),3)
                                        
        comparison_dict[p]['SSIM'] = round(ssim(comparison_dict[p][data_keys[0]],
                                        comparison_dict[p][data_keys[1]]),3)
                                        
        comparison_dict[p]['MSE Map'] = (comparison_dict[p][data_keys[0]] - 
                                          comparison_dict[p][data_keys[1]])**2
                                          
        comparison_dict[p]['SSIM Map'] = ssim(comparison_dict[p][data_keys[0]], 
                                          comparison_dict[p][data_keys[1]],
                                            full = True)[1]
    
        comparison_dict[p]['CW-SSIM'] = cw_ssim(comparison_dict[p][data_keys[0]],
                                        comparison_dict[p][data_keys[1]], 40)[0]
                                        
        comparison_dict[p]['CW-SSIM Map'] = cw_ssim(comparison_dict[p][data_keys[0]],
                                        comparison_dict[p][data_keys[1]], 40)[1]
                                        
        comparison_dict[p]['GMS'] = gmsd(comparison_dict[p][snow[0]],
                                        comparison_dict[p][snow[1]])[0]
                                        
        comparison_dict[p]['GMS Map'] = gmsd(comparison_dict[p][snow[0]],
                                        comparison_dict[p][snow[1]])[1]

    
        comparison_dict[p][snow[0]+' DCT Map'] = discrete_cosine(comparison_dict[p][snow[0]],
                                        comparison_dict[p][snow[1]])[0]
                                        
        comparison_dict[p][snow[1]+' DCT Map'] = discrete_cosine(comparison_dict[p][snow[0]],
                                        comparison_dict[p][snow[1]])[1]
                                        
        comparison_dict[p][snow[0]+' DCT Curve'] = discrete_cosine(comparison_dict[p][snow[0]],
                                        comparison_dict[p][snow[1]])[2]
                                        
        comparison_dict[p][snow[1]+' DCT Curve'] = discrete_cosine(comparison_dict[p][snow[0]],
                                        comparison_dict[p][snow[1]])[3]
                                        
                                        
        comparison_dict[p]['FSIM'] = feature_sim(comparison_dict[p][snow[0]],
                                        comparison_dict[p][snow[1]])
def test_ssim_multichannel_chelsea():
    # color image example
    Xc = data.chelsea()
    sigma = 15.0
    Yc = np.clip(Xc + sigma * np.random.randn(*Xc.shape), 0, 255)
    Yc = Yc.astype(Xc.dtype)

    # multichannel result should be mean of the individual channel results
    mssim = ssim(Xc, Yc, multichannel=True)
    mssim_sep = [ssim(Yc[..., c], Xc[..., c]) for c in range(Xc.shape[-1])]
    assert_almost_equal(mssim, np.mean(mssim_sep))

    # ssim of image with itself should be 1.0
    assert_equal(ssim(Xc, Xc, multichannel=True), 1.0)
def test_ssim_dtype():
    N = 30
    X = np.random.rand(N, N)
    Y = np.random.rand(N, N)

    S1 = ssim(X, Y)

    X = (X * 255).astype(np.uint8)
    Y = (X * 255).astype(np.uint8)

    S2 = ssim(X, Y)

    assert S1 < 0.1
    assert S2 < 0.1
def test_ssim_dynamic_range_and_data_range():
    # Tests deprecation of "dynamic_range" in favor of "data_range"
    N = 30
    X = np.random.rand(N, N) * 255
    Y = np.random.rand(N, N) * 255

    with expected_warnings(
            '`dynamic_range` has been deprecated in favor of '
            '`data_range`. The `dynamic_range` keyword argument '
            'will be removed in v0.14'):
        out2 = ssim(X, Y, dynamic_range=255)

    out1 = ssim(X, Y, data_range=255)

    assert_equal(out1, out2)
def test_ssim_grad():
    N = 30
    X = np.random.rand(N, N) * 255
    Y = np.random.rand(N, N) * 255

    f = ssim(X, Y, data_range=255)
    g = ssim(X, Y, data_range=255, gradient=True)

    assert f < 0.05

    assert g[0] < 0.05
    assert np.all(g[1] < 0.05)

    mssim, grad, s = ssim(X, Y, data_range=255, gradient=True, full=True)
    assert np.all(grad < 0.05)
Example #14
0
def compare_images(imageA, imageB, title):
    # compute the mean squared error and structural similarity
    # index for the images
    m = mse(imageA, imageB)
    print title
    print m
    print "-----"
    s = ssim(imageA, imageB)

    # setup the figure
    fig = plt.figure(title)
    plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))
    #plt.suptitle("MSE: %.2f, SSIM: ??" % (m))
    # show first image
    ax = fig.add_subplot(1, 2, 1)
    plt.imshow(imageA, cmap=plt.cm.gray)
    plt.axis("off")

    # show the second image
    ax = fig.add_subplot(1, 2, 2)
    plt.imshow(imageB, cmap=plt.cm.gray)
    plt.axis("off")

    # show the images
    plt.show()
def compare_images(imageA, imageB, title, show_plot=True):
    # compute the mean squared error and structural similarity
    # index for the images
    m = mse(imageA, imageB)
    s = ssim(imageA, imageB)

    if show_plot:
        import matplotlib.pyplot as plt

        # setup the figure
        fig, ax = plt.subplots(1, 2)
        fig.suptitle("%s\nMSE: %.5f, SSIM: %.5f" % (title, m, s))

        # show first image
        ax[0].imshow(imageA, cmap=plt.cm.gray)
        ax[0].axis("off")

        # show the second image
        ax[1].imshow(imageB, cmap=plt.cm.gray)
        ax[1].axis("off")

        # show the images
        plt.show()

    return m, s
Example #16
0
def compare_in_path(path):
	filenames = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]

	# shuffle because I started the script several times without finishing the process
	random.shuffle(filenames)

	counter = 0
	for idx, f1 in enumerate(filenames):

		# don't compare two files twice
		for f2 in filenames[(idx+1):]:
			a1 = io.imread(os.path.join(path, f1))
			a2 = io.imread(os.path.join(path, f2))

			s = ssim(a1, a2, multichannel=True)

			# 0.5 seems reasonable, but one could try around here (maybe 0.4)
			if s > 0.5:
				print(f1 + '   ' + f2 + '\t\t' + str(s))

				# remove the second file
				os.remove(os.path.join(path, f2))

				filenames.remove(f2)

		
		counter += 1
		print ('  * ' + str(counter) + ' *  ' )
		if counter > 100: break
def compare_images(imageA, imageB, title, plot=True):
	'''
        Compute the mean squared error and structural similarity
	index for the images
        '''
	m,p = mse(imageA, imageB)
	s = ssim(imageA, imageB)
        if DEBUG:
            print '\tMSE: {0} PNSR: {1} SSIM {2}'.format(m,p,s)
        if not plot:
            return m,p,s
 
	# setup the figure
	fig = plt.figure(title)
	plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))
 
	# show first image
	ax = fig.add_subplot(1, 2, 1)
	plt.imshow(imageA, cmap = plt.cm.gray)
	plt.axis("off")
 
	# show the second image
	ax = fig.add_subplot(1, 2, 2)
	plt.imshow(imageB, cmap = plt.cm.gray)
	plt.axis("off")
 
	# show the images
	plt.show()
        return m,p,s
Example #18
0
def cal_similarity(image_name, image_files, directory):
    # rdb.set_trace()
    original = io.imread(os.path.join(directory, image_name))
    original = rgb2gray(original)
    similarity = {}
    for image in image_files:
        if image_name != image:
            compare = rgb2gray(io.imread(os.path.join(directory, image)))
            sim = ssim(original, compare)
            if len(similarity) >= 2:
                min_ssim = min(similarity, key=similarity.get)
                if sim > similarity[min_ssim]:
                    del similarity[min_ssim]
                else:
                    continue
            similarity[image] = sim

            # update the cache
            if image in redis_cache.keys():
                image_similarity = pickle.loads(redis_cache.get(image))
                if len(image_similarity) < 2:
                    image_similarity[image_name] = sim
                    redis_cache.set(image, pickle.dumps(image_similarity, pickle.HIGHEST_PROTOCOL))
                min_ssim = min(image_similarity, key=image_similarity.get)
                if sim > image_similarity[min_ssim]:
                    del image_similarity[min_ssim]
                    image_similarity[image_name] = sim
                    redis_cache.set(image, pickle.dumps(image_similarity, pickle.HIGHEST_PROTOCOL))
    return similarity
def test_invalid_input():
    # size mismatch
    X = np.zeros((9, 9), dtype=np.double)
    Y = np.zeros((8, 8), dtype=np.double)
    with testing.raises(ValueError):
        ssim(X, Y)
    # win_size exceeds image extent
    with testing.raises(ValueError):
        ssim(X, X, win_size=X.shape[0] + 1)
    # some kwarg inputs must be non-negative
    with testing.raises(ValueError):
        ssim(X, X, K1=-0.1)
    with testing.raises(ValueError):
        ssim(X, X, K2=-0.1)
    with testing.raises(ValueError):
        ssim(X, X, sigma=-1.0)
def compare_images(imageA, imageB):
    # compute the mean squared error and structural similarity
    # index for the images
    m = mse(imageA, imageB)
    s = ssim(color.rgb2gray(imageA), color.rgb2gray(imageB))
    if(m>500):print(m)
    if(s<0.85): print(s)
Example #21
0
def calc_statistics(suffix_to_nifti):
    """
    Get dictionary - suffix to nifti, return statistics
    :param suffix_to_nifti:
    :return:
    """
    reg_img = suffix_to_nifti[''].get_data()
    mse = {}
    ssim_ = {}
    num_of_cls = len(CLASSES)
    for (ver_name, ver_nifti) in suffix_to_nifti.iteritems():
        if ver_name == '':
            mse[ver_name] = num_of_cls*[0.0]
            ssim_[ver_name] = num_of_cls*[1.0]
            continue
        data = ver_nifti.get_data()
        mse_temp = []
        mse_cls = np.mean(np.sum(((data - reg_img))**2, axis=(0,1)))
        mse_temp.append(mse_cls)

        ssim_temp = ssim(X=reg_img, Y=data, data_range=data.max() - data.min())
        mse[ver_name] = mse_temp
        ssim_[ver_name] = ssim_temp

    return mse, ssim_
def test_gaussian_mssim_vs_IPOL():
    # Tests vs. imdiff result from the following IPOL article and code:
    # https://www.ipol.im/pub/art/2011/g_lmii/
    mssim_IPOL = 0.327309966087341
    mssim = ssim(cam, cam_noisy, gaussian_weights=True,
                 use_sample_covariance=False)
    assert_almost_equal(mssim, mssim_IPOL, decimal=3)
Example #23
0
def compute_loss(reconstructed_output, original, loss_type):
    """
    Computes the loss associated with an MR image slice 
    and a reconstruction of the slice after subsampling. 
    The loss function is specified by `loss_type`
    
    Parameters
    ------------
    reconstructed_output : np.ndarray
        The reconstructed MR image slice, represented as a 
        numpy array with datatype `np.float32`
    original : np.ndarray
        The original MR image slice (before subsampling),
        represented as a numpy array with datatype `np.float32`
    loss_type : str
        The type of loss to compute (either 'mse' or 'mae')

    Returns
    ------------
    float
        The specified loss computed between the
        reconstructed slice and the original slice
    """

    output = np.array(reconstructed_output, dtype=np.float64) / 255.0
    original = np.array(original, dtype=np.float64) / 255.0
    if loss_type == LOSS_TYPE_MSE:
        return np.mean((reconstructed_output - original)**2)
    elif loss_type == LOSS_TYPE_SSIM:
        return ssim(reconstructed_output, original)
    else:
        raise Exception("Attempted to compute an invalid loss!")
def compare(file1, file2):
    image1 = io.imread(file1, as_grey = True)
    image2 = io.imread(file2, as_grey = True)
    image1 = feature.canny(image1)
    image2 = feature.canny(image2)

    return ssim(image1, image2)
Example #25
0
def compare_images(imageA, imageB):
    # compute the mean squared error and structural similarity
    # index for the images
    m = mse(imageA, imageB)
    s = ssim(imageA, imageB)

    return round(m,2), round(s,2)
Example #26
0
def matchFindNonORB(TestImName, RefImName,TestIm,RefIm, Method):
	""" Inputs:  two strings: an image name, TestImName, and the name of a reference image, RefImName,
				  a string containing the specified Method used to compare the images
				  an optional parameter: the number of distances, NumDist, which will perform the operations on a subset of the information provided
						Any number greater  than 0 for NumDist will truncate the distances
		Outputs: a number, answer, that comes from the comparison method used between the two images """

	if TestImName == RefImName: #if an image is tested against itself, let's save time by returning a value eliminating it from contention...
		if Method == 'ssim': #for these methods, higher is better, so we return a huge value
			return 1000000
		else: #otherwise, return -1, a number lower than any value we should see...
			return -1

	answer = 0	
	"""Now, the user chooses the method, and the answer gets returned accordingly """	
	if Method == 'mse': #mean squared error
		answer = mse(TestIm,RefIm)
	elif Method == 'ssim': #structural similarity index
		answer = ssim(TestIm,RefIm)
	elif Method == 'rms': #root means squared difference between pixels
		answer = rms(TestIm,RefIm)
	elif Method == 'ccv2':
		answer = ccv2.main(TestImName,RefImName)


	#print TestIm, answer 
	return answer
def compare_images(imageA, imageB, title, show_plot=True):
    """
    computes the mean squared error and structural similarity
    """

    # index values for mean squared error
    if VERBOSE: print("comparing mean squared error...")
    m = mse(imageA, imageB)

    # convert the images to grayscale
    if VERBOSE: print("converting to greyscale...")
    imageA_grey = rgb2grey(imageA)
    imageB_grey = rgb2grey(imageB)

    # uses image copies to avoid runtime warning for ssim computation
    img1_grey = np.copy(imageA_grey)
    img2_grey = np.copy(imageB_grey)

    # index values for structural similarity
    if VERBOSE: print("comparing structural similarity...")
    s = ssim(img1_grey, img2_grey)

    if show_plot:
        if VERBOSE: print("plotting images...")
        try:
            import matplotlib.pyplot as plt
        except:
            print("Error importing pyplot from matplotlib, please install matplotlib package first...")
            sys.tracebacklimit=0
            raise Exception("Importing matplotlib failed")

        # setup the figure
        fig, ax = plt.subplots(2, 2)
        fig.suptitle("%s\nMSE: %.5f, SSIM: %.5f" % (title, m, s))

        ax[0][0].text(-10, -10, 'MSE: %.5f' %(m))

        # show first image
        ax[0][0].imshow(imageA, cmap=plt.cm.gray)
        ax[0][0].axis("off")

        # show the second image
        ax[0][1].imshow(imageB, cmap=plt.cm.gray)
        ax[0][1].axis("off")

        ax[1][0].text(-10, -10, 'SSIM: %.5f' %(s))

        # show first grey image
        ax[1][0].imshow(img1_grey, cmap=plt.cm.gray)
        ax[1][0].axis("off")

        # show the second grey image
        ax[1][1].imshow(img2_grey, cmap=plt.cm.gray)
        ax[1][1].axis("off")

        # show the images
        plt.show()

    return m, s
Example #28
0
def compare_to_last_info(img):
    global temp_img
    if temp_img is not None:
        s = ssim(img, temp_img)
    else:
        s = 0
    temp_img = img
    return s
Example #29
0
def Compare_images(imageA, imageB):
	# compute the mean squared error and structural similarity
	# index for the images
    imageA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
    imageB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)
    m = mse(imageA, imageB)
    s = ssim(imageA, imageB)
    return m,s
Example #30
0
def build_score(label,_list):
    model = get_model(label)
    _score = []
    for im in _list:
        #print 'im shape', im.shape
        #print 'model shape', model.shape
        _score.append(ssim(im,model))
    return _score
    }
    harm = sess.run(harmnization, feed_dict=feed_dict)
    harm_rgb = np.squeeze(harm)
    harm_rgb = np.multiply(harm_rgb, np.array(127.5))
    harm_rgb += np.array((127.5, 127.5, 127.5))
    harm_rgb = harm_rgb[:, :, ::-1]
    neg_idx = harm_rgb < 0.0
    harm_rgb[neg_idx] = 0.0
    pos_idx = harm_rgb > 255.0
    harm_rgb[pos_idx] = 255.0
    name = get_name(path_img[i])

    truth1 = Image.open(os.path.join(FLAGS.data_dir, path_truth[i]))
    truth1 = truth1.resize([256, 256], Image.BICUBIC)
    truth1 = np.array(truth1, dtype=np.float32)

    mse_score = mse(harm_rgb, truth1)
    psnr_score = psnr(truth1,
                      harm_rgb,
                      data_range=harm_rgb.max() - harm_rgb.min())
    ssim_score = ssim(truth1,
                      harm_rgb,
                      data_range=harm_rgb.max() - harm_rgb.min(),
                      multichannel=True)

    file.writelines('%s\t%f\t%f\t%f\n' %
                    (name, mse_score, psnr_score, ssim_score))
    print(i, name, mse_score, psnr_score, ssim_score)

sess.close()
print('Done!')
Example #32
0
 def cmpSSIM(self, img1, img2):
     #对相同分辨率的图进行结构相似性评价,表示失真程度,值越大,失真越小, 取值范围[0,1]
     #0.5作为失真的阈值,即两张图变化较大
     return ssim(img1, img2, multichannel=True)
Example #33
0
    return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))


# base_name = "reference"
# ref_img = cv.imread(base_name+".png")
# in_img = cv.imread(base_name+"_cubic.png")
# out_img = cv.imread(base_name+"_out.png")

base_name = "jk9"
ref_img = cv.imread(base_name + ".jpg")
in_img = cv.imread(base_name + "_cubic.png")
out_img = cv.imread(base_name + "_out.png")

# calculate psnr
test_gray = cv.cvtColor(ref_img, cv.COLOR_RGB2GRAY)
cubic_gray = cv.cvtColor(in_img, cv.COLOR_RGB2GRAY)
out_gray = cv.cvtColor(out_img, cv.COLOR_RGB2GRAY)

print test_gray.shape, cubic_gray.shape, out_gray.shape

psnr_cubic = psnr(test_gray, cubic_gray)
psnr_output = psnr(test_gray, out_gray)

ssim_cubic = ssim(test_gray, cubic_gray)
ssim_output = ssim(test_gray, out_gray)

print("cubic interpolation psnr is:" + str(psnr_cubic) + ";     " +
      "predict psnr is:" + str(psnr_output))
print("cubic interpolation ssim is:" + str(ssim_cubic) + ";     " +
      "predict ssim is:" + str(ssim_output))
Example #34
0
picAnm = cv2.imread(picpath + "screenshotnomarkersA.png")
picA = cv2.imread(picpath + "screenshotA.png")
picBnm = cv2.imread(picpath + "screenshotnomarkersB.png")
picB = cv2.imread(picpath + "screenshotB.png")
picCnm = cv2.imread(picpath + "screenshotnomarkersC.png")
picC = cv2.imread(picpath + "screenshotC.png")

picA = cv2.cvtColor(picA, cv2.COLOR_BGR2GRAY)
picAnm = cv2.cvtColor(picAnm, cv2.COLOR_BGR2GRAY)
picB = cv2.cvtColor(picB, cv2.COLOR_BGR2GRAY)
picBnm = cv2.cvtColor(picBnm, cv2.COLOR_BGR2GRAY)
picC = cv2.cvtColor(picC, cv2.COLOR_BGR2GRAY)
picCnm = cv2.cvtColor(picCnm, cv2.COLOR_BGR2GRAY)

difvalue = ssim(picA, picAnm)  #1 is same, -1 is totally different
print("dif between A and Anm:")
print(difvalue)

print("pic A vs pic B:    ", ssim(picA, picB))
print("pic A vs pic C:    ", ssim(picA, picC))
print("pic C vs pic B:    ", ssim(picC, picB))

print("pic Anm vs pic Bnm:", ssim(picAnm, picBnm))
print("pic Anm vs pic Cnm:", ssim(picAnm, picCnm))
print("pic Cnm vs pic Bnm:", ssim(picCnm, picBnm))

print("pic A vs pic Bnm:  ", ssim(picA, picBnm))
print("pic A vs pic Cnm:  ", ssim(picA, picCnm))
print("pic C vs pic Bnm:  ", ssim(picC, picBnm))
Example #35
0
        sigma11 = signal.fftconvolve(img1 * img1, window, mode='valid')
        sigma22 = signal.fftconvolve(img2 * img2, window, mode='valid')
        sigma12 = signal.fftconvolve(img1 * img2, window, mode='valid')
    else:
        # Empty blur kernel so no need to convolve.
        mu1, mu2 = img1, img2
        sigma11 = img1 * img1
        sigma22 = img2 * img2
        sigma12 = img1 * img2

    mu11 = mu1 * mu1
    mu22 = mu2 * mu2
    mu12 = mu1 * mu2
    sigma11 -= mu11
    sigma22 -= mu22
    sigma12 -= mu12

    # Calculate intermediate values used by both ssim and cs_map.
    c1 = (k1 * max_val) ** 2
    c2 = (k2 * max_val) ** 2
    v1 = 2.0 * sigma12 + c2
    v2 = sigma11 + sigma22 + c2
    ssim = np.mean((((2.0 * mu12 + c1) * v1) / ((mu11 + mu22 + c1) * v2)))
    cs = np.mean(v1 / v2)
    return ssim, cs


print('ms-ssim: {}'.format(MultiScaleSSIM(image1_1,image2_1)))
print('ssim: {}'.format(1-ssim(image1, image2)))
print('mse: {}'.format(mse(image1,image2)))
Example #36
0
 def calculate_fitness_ssim(self, goal):
     # calculates structural similarity index image difference (higher = more similar)
     ssim_index = ssim(self.array, goal, multichannel=True)
     self.fitness = ssim_index
# Choose a starting point
x = fbp(sinogram_data)

with odl.util.Timer('runtime of solver'):
    # Run the algorithm
    x = admm_system_solve.admm_solver(x,
                                      f,
                                      g,
                                      radon_transform_operator,
                                      tau=tau,
                                      sigma=sigma,
                                      niter=niter,
                                      data=sinogram_data,
                                      space=space,
                                      solver="gmres")

print('ssim = {}'.format(ssim(phantom.asarray(), x.asarray())))
print('psnr = {}'.format(
    psnr(phantom.asarray(),
         x.asarray(),
         data_range=np.max(phantom) - np.min(phantom))))

# Display images
figure_folder = 'Results'

x.show('', clim=[0, 1], saveto='Results/' + 'shepp_logan_tv_solver')
x.show('',
       clim=[0.1, 0.4],
       saveto='Results/' + 'shepp_logan_tv_windowed_solver')
Example #38
0
def test_sampling(conf, training_result):
    """
    Sampling rate vs Reconstruction Quality test.

    Parameters
    ----------
    conf : conf_loader.Conf
        Experiment parameters
    training_result : touple
        Retrun values of function `training`

    Returns
    -------
    srange : np.array
        sampling rate used
    (bk_mse, fa_mse, rc_mse) : (np.array, np.array, np.array)
        mse for `k-best`, `f_avg` and `LSC` methods at different sampling rate
    (bk_ssim, fa_ssim, rc_ssim) : (np.array, np.array, np.array)
        ssim for `k-best`, `f_avg` and `LSC` methods at different saapling rate
    """
    # matrxi to vector (m2v) and vector to matrix (v2m) functions
    m2v, v2m = conf.vect_functions()
    (sort, tros), (energy, comulated, bands), codebooks = training_result
    n_bands = conf.nbands
    subcfg = conf.testing['reconstruction']

    # Load testing set
    testing, Testing = common.load_dataset(conf.testingset_path(),
                                           conf.fformat, conf.size())
    FlatTst = m2v(Testing)[:, sort]

    # f_avg sampling pattern
    Omega = energy.argsort()[::-1]

    # lsc sampling pattern
    Omegas = [c.sampling_pattern() for c in codebooks]

    shape = testing[0].shape
    n = np.prod(shape)
    N = len(testing)

    # sampling rate range
    srange = np.logspace(*subcfg['sampling_range'])

    # results accumulator
    bk_mse = np.zeros(len(srange))
    fa_mse = np.zeros(len(srange))
    rc_mse = np.zeros(len(srange))

    bk_ssim = np.zeros(len(srange))
    fa_ssim = np.zeros(len(srange))
    rc_ssim = np.zeros(len(srange))

    print('Sampling Rate vs Reconstruction Quality Test:')
    for i, rate in enumerate(srange):
        print(f'\r {i+1:3d}/{len(srange)}', flush=True, end='')
        M = int(round(n * rate))
        m = int(round(M / n_bands))
        ms = lsc.num_samples(bands, m)
        M = np.sum(ms)

        smalls = [omega[:y] for omega, y in zip(Omegas, ms)]

        for idx in range(N):
            reference = common.norm(testing[idx])
            X = FlatTst[idx]

            Xsbs = lsc.split(X, bands)
            Ysbs = lsc.sub_sample(Xsbs, Omegas, m)
            recovered = [
                codebooks[b].reconstruct(Ysbs[b], smalls[b])
                for b in range(len(bands))
            ]
            Y = v2m((lsc.union(recovered))[tros], shape)
            y = common.norm(common.pos(common.ifft2(Y).real))

            BK = X.copy()[tros]
            # BK sampling pattern
            O = np.abs(BK).argsort()[::-1]
            BK[O[M:]] = 0
            BK = v2m(BK, shape)
            bK = common.norm(common.pos(common.ifft2(BK).real))

            FA = X.copy()[tros]
            FA[Omega[M:]] = 0
            FA = v2m(FA, shape)
            fA = common.norm(common.pos(common.ifft2(FA).real))

            fa_mse[i] += mse(reference, fA) / N
            bk_mse[i] += mse(reference, bK) / N
            rc_mse[i] += mse(reference, y) / N

            fa_ssim[i] += ssim(reference, fA, gaussian_weights=True) / N
            bk_ssim[i] += ssim(reference, bK, gaussian_weights=True) / N
            rc_ssim[i] += ssim(reference, y, gaussian_weights=True) / N
    print('\t[done]')
    return srange, (bk_mse, fa_mse, rc_mse), (bk_ssim, fa_ssim, rc_ssim)
def call_detect():
    global finalImage
    global avgX
    global avgY
    global detected

    h, w = template.shape

    while True:
        #start = time.time()
        clone = image
        gray = cv2.cvtColor(clone, cv2.COLOR_BGR2GRAY)
        found = None
        # loop over the scales of the image
        for scale in np.linspace(2.4, 0.2, 10)[::-1]:
            # resize the image according to the scale, and keep track
            # of the ratio of the resizing
            resized = imutils.resize(gray, width=int(gray.shape[1] * scale))
            r = gray.shape[1] / float(resized.shape[1])
            # if the resized image is smaller than the template, then break
            # from the loop
            if resized.shape[0] < tH or resized.shape[1] < tW:
                break
            # detect edges in the resized, grayscale image and apply template
            # matching to find the template in the image

            edged = cv2.Canny(resized, 50, 200)
            result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
            (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

            # if we have found a new maximum correlation value, then ipdate
            # the bookkeeping variable
            if found is None or maxVal > found[0]:
                found = (maxVal, maxLoc, r)

                (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
                (endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))

                resized = gray[startY:endY, startX:endX]

                resized = cv2.resize(resized, (w, h))

                if ssim(original, resized) > 0.2:
                    break

        # unpack the bookkeeping varaible and compute the (x, y) coordinates
        # of the bounding box based on the resized ratio

        (_, maxLoc, r) = found
        (startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
        (endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))

        resized = gray[startY:endY, startX:endX]

        resized = cv2.resize(resized, (w, h))
        if ssim(original, resized) > 0.2:
            # draw a bounding box around the detected result and display the image
            cv2.rectangle(clone, (startX, startY), (endX, endY), (0, 0, 255), 2)
            finalImage = clone
            avgX = (startX + endX) / 2
            avgY = (startY + endY) / 2
            detected = True
        else:
            detected = False
        cv2.imwrite(FaceFileName, resized)  #saving captured faces

    cv2.imshow('Video', frame)  #playing the video in new window

    if cv2.waitKey(1) & 0xFF == ord('q'):  #for quitting
        break

print(time.ctime())
print("face count is", count)
cap.release()
cv2.destroyAllWindows()
for i in range(count):
    for j in range(i + 1, count):
        a = list1[i]
        b = list1[j]
        c = cv2.imread(a)  #reading an image
        d = cv2.imread(b)
        c = cv2.cvtColor(c, cv2.COLOR_BGR2GRAY)  #for changing colorspaces
        d = cv2.cvtColor(d, cv2.COLOR_BGR2GRAY)
        s = ssim(
            c, d)  #structural similarity index for pixel by pixel comparision
        if s > 0.5:
            list2.append(b)  #appending similar faces
for k in range(len(list2) + 1):
    try:
        os.remove(list2[k])  #removing similar faces
    except FileNotFoundError:
        continue
    except IndexError:
        print("Done")
def ssimf(img, img2):
    ssim_v = ssim(img, img2, data_range=img2.max() - img2.min() + e)

    return ssim_v
Example #42
0
psnr = 20 * np.log10((np.max(x_test_org) - np.min(x_test_org)) / np.sqrt(mse))

print(psnr)

ssim_mnist = np.zeros(x_test_org.shape[0])
for i in range(0, x_test_org.shape[0]):
    img1 = x_test_org[i]
    img2 = x_rec[i]
    if nc == 3:
        img_true = np.zeros((img1.shape[1], img1.shape[2], img1.shape[0]))
        img_rec = np.zeros((img2.shape[1], img2.shape[2], img2.shape[0]))
        for chan in range(0, nc):
            img_true[:, :, chan] = img1[chan, :, :]
            img_rec[:, :, chan] = img2[chan, :, :]
        ssim_mnist[i] = ssim(img_true,
                             img_rec,
                             data_range=img_rec.max() - img_rec.min(),
                             multichannel=True)
    elif nc == 1:
        img_true = img1.reshape(ngf, ndf * multiple)
        img_rec = img2.reshape(ngf, ndf * multiple)
        ssim_mnist[i] = ssim(img_true,
                             img_rec,
                             data_range=img_rec.max() - img_rec.min())

print(np.mean(ssim_mnist))
sub_dim_ssim.append(np.mean(ssim_mnist))
sub_dim_mse.append(mse)
sub_dim_psnr.append(psnr)
sub_dim_rec.append(x_rec)
#        print(subtype)
#        print(test_alpha)
import cv2
import numpy as np
from skimage.measure import compare_ssim as ssim

x0 = cv2.imread("test_images/testmap0.png")
x1 = cv2.imread("test_images/parking.png")
# x2 = cv2.imread("test_images/tleft.png")
# x3 = cv2.imread("test_images/tright.png")
x4 = cv2.imread("test_images/tnormal.png")
x5 = cv2.imread("test_images/x.png")

s1 = ssim(x0, x1, multichannel=True)
# s2 = ssim(x0, x2, multichannel=True)
# s3 = ssim(x0, x3, multichannel=True)
s4 = ssim(x0, x4, multichannel=True)
s5 = ssim(x0, x5, multichannel=True)

# sm = min(s2, s3, s4, s5)
sm = min(s4, s5)

print("   ")

# if (sm == s2) and s1 != 1 and s3 != 1 and s4 != 1 and s5 != 1:
#     print("Image is T-Left")
# if (sm == s3) and s1 != 1 and s2 != 1 and s4 != 1 and s5 != 1:
#     print("Image is T-Right")
# if sm == s4 and s1 != 1 and s3 != 1 and s2 != 1 and s5 != 1:
#     print("Image is T-Normal")
# if sm == s5 and s1 != 1 and s3 != 1 and s4 != 1 and s2 != 1:
#     print("Image is Cross-X")
## Compute PSNR and SSIM en test set
  
out = model.predict(x_test)*255  
outIllusions = model.predict(illusions)  

performance = np.zeros((x_test.shape[0],2))

# PSNR
for i in range(x_test.shape[0]):    
    
    performance[i,0] = psnr(y_test[i,:,:,:]*255,out[i,:,:,:],data_range=255)
    
# SSIM
for i in range(x_test.shape[0]):    
    
    performance[i,1] = ssim(y_test[i,:,:,:]*255, out[i,:,:,:], data_range=255, gaussian_weights=True
    , sigma = 1.5, use_sample_covariance=False,multichannel=True)


## Save results
file = open(saveDir+'Illusions_r'+str(kernel_s)+'_nk'+str(numKernels)+'_p'+str(poolSize)+'.txt','w')
file.write('Mean PSNR'+str(np.mean(performance[:,0]))+'-'+str(np.std(performance[:,0]))) 
file.write('Mean SSIM'+str(np.mean(performance[:,1]))+'-'+str(np.std(performance[:,1]))) 
file.write(chkpt)
file.close() 

#np.save(saveDir+'jean_denoise_results',out) 
print('Saving Results ...')
np.save(saveDir+'Illusions_r'+str(kernel_s)+'_nk'+str(numKernels)+'_p'+str(poolSize),outIllusions) 
#np.save(saveDir+'jean_denoise_inputs',x_test) 
#np.save(saveDir+'jean_denoise_labels',y_test) 
Example #45
0
def evaluate_quality(image_a, ref_image):
    ssim_value = ssim(img_a, ref_image, data_range=img_a.max() - img_a.min())
    return ssim_value
Example #46
0
File: e.py Project: renatotnk/UDESC
fig, axes = plt.subplots(nrows=1,
                         ncols=5,
                         figsize=(10, 4),
                         sharex=True,
                         sharey=True)
ax = axes.ravel()


def mse(x, y):
    return np.linalg.norm(x - y)


best = cv2.imread('fourier_1_BestEnhanced.jpg', 0)

mse_none = mse(best, best)
ssim_none = ssim(best, best, data_range=best.max() - best.min())

mse_eq = mse(best, equ)
ssim_eq = ssim(best, equ, data_range=equ.max() - equ.min())

mse_log = mse(best, img_log)
ssim_log = ssim(best, img_log, data_range=img_log.max() - img_log.min())

mse_g1 = mse(best, adjusted)
ssim_g1 = ssim(best, adjusted, data_range=adjusted.max() - adjusted.min())

mse_g2 = mse(best, adjusted_2)
ssim_g2 = ssim(best,
               adjusted_2,
               data_range=adjusted_2.max() - adjusted_2.min())
Example #47
0
File: one.py Project: eloymg/one
TEST_IMAGE = TEST_IMAGE[:, :, 1]
TEST_IMAGE = scipy.misc.imresize(TEST_IMAGE, [64, 64])
TEST_IMAGE = TEST_IMAGE.astype("float64")

nn = 4**7
M = 4**7
HADAMARD_MASKS = generate_hadamard(TEST_IMAGE.shape[0], nn)
np.random.shuffle(HADAMARD_MASKS)

result = np.ones(TEST_IMAGE.shape)

for i in range(0, M):
    mask = HADAMARD_MASKS[i] == 1
    mask = mask * 1
    masked = TEST_IMAGE * HADAMARD_MASKS[i]
    intensity = masked.sum(dtype="float64")
    pixels = HADAMARD_MASKS[i].sum(dtype="float64")
    result += intensity * HADAMARD_MASKS[i]

result = skimage.exposure.rescale_intensity(result, out_range=(0, 255))

print ssim(result, TEST_IMAGE)

fig = plt.figure()
fig.add_subplot(1, 2, 1)
plt.gray()
plt.imshow(TEST_IMAGE)
fig.add_subplot(1, 2, 2)
plt.imshow(result)
plt.show()
Example #48
0
            h_dot = grad(l, counts[mm], dark[mm], flat[mm])
            L_dot = fast_transp[mm](h_dot)
            x = x - M * L_dot / d_star
            #subiter_time[mm] = time.time()-subiter_start
            #subseth[mm] = np.sum(hreg(l,counts[mm],dark[mm],flat[mm]))

        #Store time immediately after iteration
        #iteration_time = np.sum(subiter_time)
        iteration_time = time.time() - iter_begin
        if n == 0:
            T[n, 0] = iteration_time - start_time + iter_begin
        else:
            T[n, 0] = iteration_time + T[n - 1, 0]
        itr += 1
        print 'Iteration:', itr
        SSIM[n, 0] = ssim(IMAGE, x)
        #compute elapsed time
        #Compute and store objective function
        obj[n, 0] = np.sum(hreg(ffast_radon(x), tmp_counts, tmp_dark,
                                tmp_flat))
        #Keep track of itreations

    # Save objective function and time values
    sd.saveme(obj, T, SSIM, N, M, 'OSTR')

    #Display Time and Objective function vectors.
    #    print(T)
    #    print(obj)
    #    print('end outputs')

    #Compute objective function decrease
Example #49
0
        PhiTb = FFT_Mask_ForBack()(batch_x, mask)

        [x_output, loss_layers_sym] = model(PhiTb, mask)

        end = time()

        initial_result = PhiTb.cpu().data.numpy().reshape(256, 256)

        Prediction_value = x_output.cpu().data.numpy().reshape(256, 256)

        X_init = np.clip(initial_result, 0, 1).astype(np.float64)
        X_rec = np.clip(Prediction_value, 0, 1).astype(np.float64)

        init_PSNR = psnr(X_init * 255, Iorg.astype(np.float64))
        init_SSIM = ssim(X_init * 255, Iorg.astype(np.float64), data_range=255)

        rec_PSNR = psnr(X_rec * 255., Iorg.astype(np.float64))
        rec_SSIM = ssim(X_rec * 255., Iorg.astype(np.float64), data_range=255)

        print(
            "[%02d/%02d] Run time for %s is %.4f, Initial  PSNR is %.2f, Initial  SSIM is %.4f"
            % (img_no, ImgNum, imgName, (end - start), init_PSNR, init_SSIM))
        print(
            "[%02d/%02d] Run time for %s is %.4f, Proposed PSNR is %.2f, Proposed SSIM is %.4f"
            % (img_no, ImgNum, imgName, (end - start), rec_PSNR, rec_SSIM))

        im_rec_rgb = np.clip(X_rec * 255, 0, 255).astype(np.uint8)

        resultName = imgName.replace(args.data_dir, args.result_dir)
        cv2.imwrite(
        list_image_parth.append(image_path)
    return np.array(list_of_images)


def psnrr(image1, image2):
    mse = np.mean((image1 - image2)**2)
    if mse == 0:
        return 100
    MAX = 255.0
    return 20 * math.log10(MAX / math.sqrt(mse))


inp = load_dataset(
    "/home/jim/Desktop/Ptuxiaki/TensorFlow/Dataset/MNIST/inpainting/autoencoder/predicted(half)/"
)
labels = load_dataset(
    "/home/jim/Desktop/Ptuxiaki/TensorFlow/Dataset/MNIST/testing_labels2/")
total_psnr = 0
total_ssim = 0

for i in range(TEST_NUMBER):
    image = labels[i].reshape((28, 28), order='C')
    image_noisy = inp[i].reshape((28, 28), order='C')
    total_psnr += psnrr(image, image_noisy)
    total_ssim += ssim(image,
                       image_noisy,
                       data_range=image_noisy.max() - image_noisy.min())
    #imageio.imwrite("/home/jim/Desktop/Ptuxiaki/TensorFlow/Dataset/MNIST/testing_labels2/"+str(i+1)+".png",labels[i].reshape((28,28),order='C'))

print(total_psnr / TEST_NUMBER)
print(total_ssim / TEST_NUMBER)
Example #51
0
                if args.size:
                    if H == 720:
                        H = 704
                        W = 1280
                    else:
                        H = 1280
                        W = 704
                out = out[:H, :W, :]
                GT = cv2.imread(GTpath)[:H, :W, :]

                OR = cv2.imread(Bpath)[:H, :W, :]
                #print(out.shape, GT.shape, OR.shape)

                tempPSNR = psnr(out, GT)
                tempSSIM = ssim(out, GT, multichannel=True)
                oPSNR = psnr(OR, GT)
                oSSIM = ssim(OR, GT, multichannel=True)
                #print('output', tempPSNR, tempSSIM)
                #print('input', oPSNR, oSSIM)
                fsence.write(
                    str(index) + ':' + image + '  psnr:' + str(oPSNR) + '/' +
                    str(tempPSNR) + '/' + str(tempPSNR - oPSNR) + '  ssim:' +
                    str(oSSIM) + '/' + str(tempSSIM) + '/' +
                    str(tempSSIM - oSSIM) + '\n')
                fsence.flush()
                '''
                cv2.imshow('output', out)
                cv2.imshow('GT',GT)
                cv2.imshow('OR',OR)
                cv2.waitKey(0)
Example #52
0
            D = D/pj
            x = x - lam*D*g
            print(np.min(x))
        lam = lam0/(n+1)**0.25
        
            #subiter_time[mm] = time.time()-subiter_start
            #subseth[mm] = np.sum(hreg(l,counts[mm],dark[mm],flat[mm]))
            
        #Store time immediately after iteration
        #iteration_time = np.sum(subiter_time)
        iteration_time = time.time() - iter_begin
        if n==0 :
            T[n,0] = iteration_time - start_time + iter_begin
        else:
            T[n,0] = iteration_time + T[n-1,0]
        SSIM[n,0] = ssim(IMAGE,x/np.max(x))
        #Compute and store objective function
        obj[n,0] = np.sum(hreg(ffast_radon(x),tmp_counts,tmp_dark,tmp_flat))
        
        itr += 1
           
            

    # Save objective function and time values
    sd.saveme(obj,T,SSIM,N,M)

    #Display Time and Objective function vectors.
#    print(T)
#    print(obj)
#    print('end outputs')
    
fill_data = real_img_data_k.ravel()[filling_idx_k]
obs_point = xy_cor[filling_idx_k, :]
ax[1].scatter(obs_point[:, 0], obs_point[:, 1], c=fill_data, cmap=plt.cm.Blues)
plt.colorbar(gci, ax=ax[1], shrink=0.8)
ax[1].set_xlim(0, Lx)
ax[1].set_ylim(0, Ly)
ax[1].set_title('Input data ({0}% grid data)'.format(
    np.int32(k_obs_percent * 100)))

gci = ax[2].contourf(X_dis, Y_dis, blend_k[0, :, :, 0], cmap=plt.cm.Blues)
plt.colorbar(gci, ax=ax[2], shrink=0.8)
ax[2].set_title('Generated data')
plt.savefig(output_figure_save_dir + f'{repeat_num}_k_comparisons.jpg')

ssim_score = ssim(real_img_data_k[0, :, :, 0], blend_k[0, :, :, 0])
print('SSIM score is: ', ssim_score)

# 也可计算PSNR指标

## 绘制用于保存的图
fig, ax = plt.subplots(figsize=(6, 5))
gci = ax.contourf(X_dis, Y_dis, real_img_data_k[0, :, :, 0], cmap=plt.cm.Blues)
plt.colorbar(gci, ax=ax, shrink=0.8)
ax.set_title('Ground truth')
plt.tight_layout()
plt.savefig(output_figure_save_dir + 'Ground_truth.jpg')

fig, ax = plt.subplots(figsize=(6, 5))
fill_data = real_img_data_k.ravel()[filling_idx_k]
obs_point = xy_cor[filling_idx_k, :]
Example #54
0
 def ssim_compare(self, vecor1, vector2):
     ssim_compare = ssim(vecor1, vector2)
     return ssim_compare
    with odl.util.Timer(reco_method):
        odl.solvers.conjugate_gradient_normal(sum_ray_trafo,
                                              reco,
                                              noisy_data,
                                              niter=10,
                                              callback=callback)
else:
    odl.solvers.conjugate_gradient_normal(sum_ray_trafo,
                                          reco,
                                          noisy_data,
                                          niter=10,
                                          callback=callback)
multigrid.graphics.show_both(*reco)

# %% SSIM
from skimage.measure import compare_ssim as ssim

full_reco = fine_discr.zero()
odl.solvers.conjugate_gradient_normal(fine_ray_trafo,
                                      full_reco,
                                      noisy_data,
                                      niter=10,
                                      callback=callback)

full_reco_insert = resizing_operator(full_reco)
SSIM = []
Eucl = []
SSIM.append(ssim(reco[1], full_reco_insert))
Eucl.append(np.sqrt(np.sum((full_reco_insert - reco[1])**2)))
print("SSIM: %f, Eucl: %f\n" % (SSIM[0], Eucl[0]))
Example #56
0
    image = cv2.imread(imagePath)

    if image is not None:

        #format the image and convert to PIL
        contrast1 = Image.fromarray(image)

        # resize the file
        contrast1 = contrast1.resize((new_width, new_height), Image.ANTIALIAS)

        #convert back to opencv format
        contrast = np.array(contrast1)

        #convert to grayscale
        contrast = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)

        # compare_images(original, contrast, "Original vs. Contrast")
        m = mse(original, contrast)

        s = ssim(original, contrast)
        print(s)

        # Remember, as the MSE increases the images are less similar
        # as opposed to the SSIM where smaller values indicate less similarity
        if m < 350 or s > 0.4:
            # if s > 0.3:
            cv2.imshow("Result", image)
            cv2.waitKey(0)

#cv2.imshow("Query", query)
def train(upscaling_factor,
          residual_blocks,
          feature_size,
          path_prediction,
          checkpoint_dir,
          img_width,
          img_height,
          img_depth,
          subpixel_NN,
          nn,
          restore,
          batch_size=1,
          div_patches=4,
          epochs=10):
    traindataset = Train_dataset(batch_size)
    iterations_train = math.ceil(
        (len(traindataset.subject_list) * 0.8) / batch_size)
    num_patches = traindataset.num_patches

    # ##========================== DEFINE MODEL ============================##
    t_input_gen = tf.placeholder(
        'float32',
        [int((batch_size * num_patches) / div_patches), None, None, None, 1],
        name='t_image_input_to_SRGAN_generator')
    t_target_image = tf.placeholder('float32', [
        int((batch_size * num_patches) / div_patches), img_width, img_height,
        img_depth, 1
    ],
                                    name='t_target_image')
    t_input_mask = tf.placeholder('float32', [
        int((batch_size * num_patches) / div_patches), img_width, img_height,
        img_depth, 1
    ],
                                  name='t_image_input_mask')

    net_gen = generator(input_gen=t_input_gen,
                        kernel=3,
                        nb=residual_blocks,
                        upscaling_factor=upscaling_factor,
                        img_height=img_height,
                        img_width=img_width,
                        img_depth=img_depth,
                        subpixel_NN=subpixel_NN,
                        nn=nn,
                        feature_size=feature_size,
                        is_train=True,
                        reuse=False)
    net_d, disc_out_real = discriminator(input_disc=t_target_image,
                                         kernel=3,
                                         is_train=True,
                                         reuse=False)
    _, disc_out_fake = discriminator(input_disc=net_gen.outputs,
                                     kernel=3,
                                     is_train=True,
                                     reuse=True)

    # test
    gen_test = generator(t_input_gen,
                         kernel=3,
                         nb=residual_blocks,
                         upscaling_factor=upscaling_factor,
                         img_height=img_height,
                         img_width=img_width,
                         img_depth=img_depth,
                         subpixel_NN=subpixel_NN,
                         nn=nn,
                         feature_size=feature_size,
                         is_train=True,
                         reuse=True)

    # ###========================== DEFINE TRAIN OPS ==========================###

    if np.random.uniform() > 0.1:
        # give correct classifications
        y_gan_real = tf.ones_like(disc_out_real)
        y_gan_fake = tf.zeros_like(disc_out_real)
    else:
        # give wrong classifications (noisy labels)
        y_gan_real = tf.zeros_like(disc_out_real)
        y_gan_fake = tf.ones_like(disc_out_real)

    d_loss_real = tf.reduce_mean(tf.square(disc_out_real -
                                           smooth_gan_labels(y_gan_real)),
                                 name='d_loss_real')
    d_loss_fake = tf.reduce_mean(tf.square(disc_out_fake -
                                           smooth_gan_labels(y_gan_fake)),
                                 name='d_loss_fake')
    d_loss = d_loss_real + d_loss_fake

    mse_loss = tf.reduce_sum(tf.square(net_gen.outputs - t_target_image),
                             axis=[0, 1, 2, 3, 4],
                             name='g_loss_mse')

    dx_real = t_target_image[:, 1:, :, :, :] - t_target_image[:, :-1, :, :, :]
    dy_real = t_target_image[:, :, 1:, :, :] - t_target_image[:, :, :-1, :, :]
    dz_real = t_target_image[:, :, :, 1:, :] - t_target_image[:, :, :, :-1, :]
    dx_fake = net_gen.outputs[:,
                              1:, :, :, :] - net_gen.outputs[:, :-1, :, :, :]
    dy_fake = net_gen.outputs[:, :,
                              1:, :, :] - net_gen.outputs[:, :, :-1, :, :]
    dz_fake = net_gen.outputs[:, :, :,
                              1:, :] - net_gen.outputs[:, :, :, :-1, :]

    gd_loss = tf.reduce_sum(tf.square(tf.abs(dx_real) - tf.abs(dx_fake))) + \
              tf.reduce_sum(tf.square(tf.abs(dy_real) - tf.abs(dy_fake))) + \
              tf.reduce_sum(tf.square(tf.abs(dz_real) - tf.abs(dz_fake)))

    g_gan_loss = 10e-2 * tf.reduce_mean(
        tf.square(disc_out_fake -
                  smooth_gan_labels(tf.ones_like(disc_out_real))),
        name='g_loss_gan')

    g_loss = mse_loss + g_gan_loss + gd_loss

    g_vars = tl.layers.get_variables_with_name('SRGAN_g', True, True)
    d_vars = tl.layers.get_variables_with_name('SRGAN_d', True, True)

    with tf.variable_scope('learning_rate'):
        lr_v = tf.Variable(1e-4, trainable=False)
    global_step = tf.Variable(0, trainable=False)
    decay_rate = 0.5
    decay_steps = 4920  # every 2 epochs (more or less)
    learning_rate = tf.train.inverse_time_decay(lr_v,
                                                global_step=global_step,
                                                decay_rate=decay_rate,
                                                decay_steps=decay_steps)

    # Optimizers
    g_optim = tf.train.AdamOptimizer(learning_rate).minimize(g_loss,
                                                             var_list=g_vars)
    d_optim = tf.train.AdamOptimizer(learning_rate).minimize(d_loss,
                                                             var_list=d_vars)

    session = tf.Session()
    tl.layers.initialize_global_variables(session)

    step = 0
    saver = tf.train.Saver()

    if restore is not None:
        saver.restore(session, tf.train.latest_checkpoint(restore))
        val_restore = 0 * epochs
    else:
        val_restore = 0

    array_psnr = []
    array_ssim = []

    for j in range(val_restore, epochs + val_restore):
        for i in range(0, iterations_train):
            # ====================== LOAD DATA =========================== #
            xt_total = traindataset.patches_true(i)
            xm_total = traindataset.mask(i)
            for k in range(0, div_patches):
                print('{}'.format(k))
                xt = xt_total[k *
                              int((batch_size * num_patches) / div_patches):
                              (int((batch_size * num_patches) / div_patches) *
                               k) +
                              int((batch_size * num_patches) / div_patches)]
                xm = xm_total[k *
                              int((batch_size * num_patches) / div_patches):
                              (int((batch_size * num_patches) / div_patches) *
                               k) +
                              int((batch_size * num_patches) / div_patches)]

                # NORMALIZING
                for t in range(0, xt.shape[0]):
                    normfactor = (np.amax(xt[t])) / 2
                    if normfactor != 0:
                        xt[t] = ((xt[t] - normfactor) / normfactor)

                x_generator = gaussian_filter(xt, sigma=1)
                x_generator = zoom(x_generator, [
                    1, (1 / upscaling_factor), (1 / upscaling_factor),
                    (1 / upscaling_factor), 1
                ],
                                   prefilter=False,
                                   order=0)
                xgenin = x_generator

                # ========================= train SRGAN ========================= #
                # update D
                errd, _ = session.run([d_loss, d_optim], {
                    t_target_image: xt,
                    t_input_gen: xgenin
                })
                # update G
                errg, errmse, errgan, errgd, _ = session.run(
                    [g_loss, mse_loss, g_gan_loss, gd_loss, g_optim], {
                        t_input_gen: xgenin,
                        t_target_image: xt,
                        t_input_mask: xm
                    })
                print(
                    "Epoch [%2d/%2d] [%4d/%4d] [%4d/%4d]: d_loss: %.8f g_loss: %.8f (mse: %.6f gdl: %.6f adv: %.6f)"
                    % (j, epochs + val_restore, i, iterations_train, k,
                       div_patches - 1, errd, errg, errmse, errgd, errgan))

                # ========================= evaluate & save model ========================= #

                if k == 1 and i % 20 == 0:
                    if j - val_restore == 0:
                        x_true_img = xt[0]
                        if normfactor != 0:
                            x_true_img = (
                                (x_true_img + 1) * normfactor)  # denormalize
                        img_true = nib.Nifti1Image(x_true_img, np.eye(4))
                        img_true.to_filename(
                            os.path.join(path_prediction,
                                         str(j) + str(i) + 'true.nii.gz'))

                        x_gen_img = xgenin[0]
                        if normfactor != 0:
                            x_gen_img = (
                                (x_gen_img + 1) * normfactor)  # denormalize
                        img_gen = nib.Nifti1Image(x_gen_img, np.eye(4))
                        img_gen.to_filename(
                            os.path.join(path_prediction,
                                         str(j) + str(i) + 'gen.nii.gz'))

                    x_pred = session.run(gen_test.outputs,
                                         {t_input_gen: xgenin})
                    x_pred_img = x_pred[0]
                    if normfactor != 0:
                        x_pred_img = (
                            (x_pred_img + 1) * normfactor)  # denormalize
                    img_pred = nib.Nifti1Image(x_pred_img, np.eye(4))
                    img_pred.to_filename(
                        os.path.join(path_prediction,
                                     str(j) + str(i) + '.nii.gz'))

                    max_gen = np.amax(x_pred_img)
                    max_real = np.amax(x_true_img)
                    if max_gen > max_real:
                        val_max = max_gen
                    else:
                        val_max = max_real
                    min_gen = np.amin(x_pred_img)
                    min_real = np.amin(x_true_img)
                    if min_gen < min_real:
                        val_min = min_gen
                    else:
                        val_min = min_real
                    val_psnr = psnr(np.multiply(x_true_img, xm[0]),
                                    np.multiply(x_pred_img, xm[0]),
                                    dynamic_range=val_max - val_min)
                    val_ssim = ssim(np.multiply(x_true_img, xm[0]),
                                    np.multiply(x_pred_img, xm[0]),
                                    dynamic_range=val_max - val_min,
                                    multichannel=True)

        saver.save(sess=session, save_path=checkpoint_dir, global_step=step)
        print("Saved step: [%2d]" % step)
        step = step + 1
Example #58
0
def compare_images(target, ref):
    scores = []
    scores.append(psnr(target, ref))
    scores.append(mse(target, ref))
    scores.append(ssim(target, ref, multichannel=True))
    return scores
Example #59
0
    def inference(self, testset):

        np.random.seed(seed=0)  #### for reproduce

        total_psnr = 0
        total_ssim = 0
        test_total_count = 0
        start = time.time()
        im_list = glob('TestData/%s/*.png' % (testset))
        im_list = sorted(im_list)
        for i in range(len(im_list)):
            ###### convert to float [0, 1]
            im_path = im_list[i]
            im_gt = imageio.imread(im_path) / 255.0  ###### range[0,1]
            im_noise = im_gt + np.random.normal(0, self.args.sigma / 255.0,
                                                im_gt.shape)

            ############################ test patch by patch
            def get_patch_start(length, patch_size, step_size):
                start_list = [
                    x for x in range(0, length - patch_size, step_size)
                ]
                start_list.append(length - patch_size)
                return start_list

            h, w = im_noise.shape
            patch_size = self.args.test_patch_size  ### 144
            step_size = self.args.test_step_size  ### 100
            temp_out = np.zeros((h, w), dtype=np.float64)
            temp_count = np.zeros((h, w), dtype=np.float64)
            for h0 in get_patch_start(h, patch_size, step_size):
                for w0 in get_patch_start(w, patch_size, step_size):
                    batch_images = im_noise[np.newaxis, h0:h0 + patch_size,
                                            w0:w0 + patch_size, np.newaxis]
                    test_output_eval = self.sess.run(
                        self.test_output,
                        feed_dict={self.test_input:
                                   batch_images})  ## range [0,1]
                    test_output_eval = test_output_eval[0, :, :, 0]
                    test_output_eval = np.clip(test_output_eval, 0, 1)
                    temp_out[
                        h0:h0 + patch_size, w0:w0 +
                        patch_size] = temp_out[h0:h0 + patch_size, w0:w0 +
                                               patch_size] + test_output_eval
                    temp_count[h0:h0 + patch_size, w0:w0 +
                               patch_size] = temp_count[h0:h0 + patch_size,
                                                        w0:w0 + patch_size] + 1
            im_out = temp_out / temp_count

            ###### convert back to uint8 [0 255]
            im_noise = np.uint8(np.clip(im_noise, 0, 1) * 255)
            im_gt = np.uint8(im_gt * 255)
            im_out = np.uint8(im_out * 255)

            ### save noise
            temp_dir = '%s/%s' % (self.args.noise_dir, testset)
            if not os.path.exists(temp_dir):
                os.makedirs(temp_dir)
            save_path = '%s/%s' % (temp_dir, os.path.basename(im_path))
            imageio.imsave(save_path, im_noise)
            #### save output
            temp_dir = '%s/%s' % (self.args.results_dir, testset)
            if not os.path.exists(temp_dir):
                os.makedirs(temp_dir)
            save_path = '%s/%s' % (temp_dir, os.path.basename(im_path))
            imageio.imsave(save_path, im_out)

            total_psnr = total_psnr + psnr(im_gt, im_out)
            total_ssim = total_ssim + ssim(im_gt, im_out)

        print("average run time: ", (time.time() - start) / len(im_list))
        print('%s, %d ,psnr: %.2f, ssim: %.4f' %
              (testset, self.args.sigma, total_psnr / len(im_list),
               total_ssim / len(im_list)))
def evaluate(upsampling_factor, residual_blocks, feature_size,
             checkpoint_dir_restore, path_volumes, nn, subpixel_NN, img_height,
             img_width, img_depth):
    traindataset = Train_dataset(1)
    iterations = math.ceil((len(traindataset.subject_list) * 0.2))
    print(len(traindataset.subject_list))
    print(iterations)
    totalpsnr = 0
    totalssim = 0
    array_psnr = np.empty(iterations)
    array_ssim = np.empty(iterations)
    batch_size = 1
    div_patches = 4
    num_patches = traindataset.num_patches

    # define model
    t_input_gen = tf.placeholder('float32', [1, None, None, None, 1],
                                 name='t_image_input_to_SRGAN_generator')
    srgan_network = generator(input_gen=t_input_gen,
                              kernel=3,
                              nb=residual_blocks,
                              upscaling_factor=upsampling_factor,
                              feature_size=feature_size,
                              subpixel_NN=subpixel_NN,
                              img_height=img_height,
                              img_width=img_width,
                              img_depth=img_depth,
                              nn=nn,
                              is_train=False,
                              reuse=False)

    # restore g
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                            log_device_placement=False))

    saver = tf.train.Saver(
        tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="SRGAN_g"))
    saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir_restore))

    for i in range(0, iterations):
        # extract volumes
        xt_total = traindataset.data_true(654 + i)
        xt_mask = traindataset.mask(654 + i)
        normfactor = (np.amax(xt_total[0])) / 2
        x_generator = ((xt_total[0] - normfactor) / normfactor)
        res = 1 / upsampling_factor
        x_generator = x_generator[:, :, :, np.newaxis]
        x_generator = gaussian_filter(x_generator, sigma=1)
        x_generator = zoom(x_generator, [res, res, res, 1], prefilter=False)
        xg_generated = sess.run(srgan_network.outputs,
                                {t_input_gen: x_generator[np.newaxis, :]})
        xg_generated = ((xg_generated + 1) * normfactor)
        volume_real = xt_total[0]
        volume_real = volume_real[:, :, :, np.newaxis]
        volume_generated = xg_generated[0]
        volume_mask = aggregate(xt_mask)
        # compute metrics
        max_gen = np.amax(volume_generated)
        max_real = np.amax(volume_real)
        if max_gen > max_real:
            val_max = max_gen
        else:
            val_max = max_real
        min_gen = np.amin(volume_generated)
        min_real = np.amin(volume_real)
        if min_gen < min_real:
            val_min = min_gen
        else:
            val_min = min_real
        val_psnr = psnr(np.multiply(volume_real, volume_mask),
                        np.multiply(volume_generated, volume_mask),
                        dynamic_range=val_max - val_min)
        array_psnr[i] = val_psnr

        totalpsnr += val_psnr
        val_ssim = ssim(np.multiply(volume_real, volume_mask),
                        np.multiply(volume_generated, volume_mask),
                        dynamic_range=val_max - val_min,
                        multichannel=True)
        array_ssim[i] = val_ssim
        totalssim += val_ssim
        print(val_psnr)
        print(val_ssim)
        # save volumes
        filename_gen = os.path.join(path_volumes, str(i) + 'gen.nii.gz')
        img_volume_gen = nib.Nifti1Image(volume_generated, np.eye(4))
        img_volume_gen.to_filename(filename_gen)
        filename_real = os.path.join(path_volumes, str(i) + 'real.nii.gz')
        img_volume_real = nib.Nifti1Image(volume_real, np.eye(4))
        img_volume_real.to_filename(filename_real)

    print('{}{}'.format('PSNR: ', array_psnr))
    print('{}{}'.format('SSIM: ', array_ssim))
    print('{}{}'.format('Mean PSNR: ', array_psnr.mean()))
    print('{}{}'.format('Mean SSIM: ', array_ssim.mean()))
    print('{}{}'.format('Variance PSNR: ', array_psnr.var()))
    print('{}{}'.format('Variance SSIM: ', array_ssim.var()))
    print('{}{}'.format('Max PSNR: ', array_psnr.max()))
    print('{}{}'.format('Min PSNR: ', array_psnr.min()))
    print('{}{}'.format('Max SSIM: ', array_ssim.max()))
    print('{}{}'.format('Min SSIM: ', array_ssim.min()))
    print('{}{}'.format('Median PSNR: ', np.median(array_psnr)))
    print('{}{}'.format('Median SSIM: ', np.median(array_ssim)))