def similarity(prefix, img1, img2, arr, mode, lock): img1_path = 'subset\subset_images\\' + img1 + '.jpg' img2_path = 'subset\subset_images\\' + img2 + '.jpg' if mode == 'ssim': gaussian_kernel_sigma = 1.5 gaussian_kernel_width = 11 gaussian_kernel_1d = get_gaussian_kernel(gaussian_kernel_width, gaussian_kernel_sigma) size = None ssim = pyssim.SSIM(img1_path, gaussian_kernel_1d, size=size) sim = ssim.ssim_value(img2_path) else: sim = pyssim.SSIM(img1_path).cw_ssim_value(img2_path) with lock: arr.append([img1, img2, sim])
def get_image_similarity(img1, img2, algorithm='SIFT'): """ Returns the normalized similarity value (from 0.0 to 1.0) for the provided pair of images. The following algorithms are supported: * SIFT: Scale-invariant Feature Transform * SSIM: Structural Similarity Index * CW-SSIM: Complex Wavelet Structural Similarity Index * MSE: Mean Squared Error """ # Converting to grayscale and resizing i1 = cv2.resize(cv2.imread(img1, cv2.IMREAD_GRAYSCALE), SIM_IMAGE_SIZE) i2 = cv2.resize(cv2.imread(img2, cv2.IMREAD_GRAYSCALE), SIM_IMAGE_SIZE) similarity = 0.0 if algorithm == 'SIFT': # Using OpenCV for feature detection and matching sift = cv2.xfeatures2d.SIFT_create() k1, d1 = sift.detectAndCompute(i1, None) k2, d2 = sift.detectAndCompute(i2, None) bf = cv2.BFMatcher() matches = bf.knnMatch(d1, d2, k=2) for m, n in matches: if m.distance < SIFT_RATIO * n.distance: similarity += 1.0 # Custom normalization for better variance in the similarity matrix if similarity == len(matches): similarity = 1.0 elif similarity > 1.0: similarity = 1.0 - 1.0 / similarity elif similarity == 1.0: similarity = 0.1 else: similarity = 0.0 elif algorithm == 'CW-SSIM': # FOR EXPERIMENTS ONLY! # Very slow algorithm - up to 50x times slower than SIFT or SSIM. similarity = pyssim.SSIM(img1).cw_ssim_value(img2) elif algorithm == 'SSIM': # Default SSIM implementation of Scikit-Image similarity = ssim(i1, i2) else: # Using MSE algorithm with custom normalization err = np.sum((i1.astype("float") - i2.astype("float"))**2) err /= float(i1.shape[0] * i2.shape[1]) if err > 0.0: similarity = MSE_NUMERATOR / err else: similarity = 1.0 return similarity
def get_image_similarity(img1, img2, algorithm='SIFT'): # Converting to grayscale and resizing i1 = cv2.resize(cv2.imread(img1, cv2.IMREAD_GRAYSCALE), SIM_IMAGE_SIZE) i2 = cv2.resize(cv2.imread(img2, cv2.IMREAD_GRAYSCALE), SIM_IMAGE_SIZE) similarity = 0.0 if algorithm == 'SIFT': # Using OpenCV for feature detection and matching sift = cv2.xfeatures2d.SIFT_create() k1, d1 = sift.detectAndCompute(i1, None) k2, d2 = sift.detectAndCompute(i2, None) bf = cv2.BFMatcher() matches = bf.knnMatch(d1, d2, k=2) for m, n in matches: if m.distance < SIFT_RATIO * n.distance: similarity += 1.0 # Custom normalization for better variance in the similarity matrix if similarity == len(matches): similarity = 1.0 elif similarity > 1.0: similarity = 1.0 - 1.0/similarity elif similarity == 1.0: similarity = 0.1 else: similarity = 0.0 elif algorithm == 'CW-SSIM': # FOR EXPERIMENTS ONLY! # Very slow algorithm - up to 50x times slower than SIFT or SSIM. # Optimization using CUDA or Cython code should be explored in the future. similarity = pyssim.SSIM(img1).cw_ssim_value(img2) elif algorithm == 'SSIM': # Default SSIM implementation of Scikit-Image similarity = ssim(i1, i2) else: # Using MSE algorithm with custom normalization err = np.sum((i1.astype("float") - i2.astype("float")) ** 2) err /= float(i1.shape[0] * i2.shape[1]) if err > 0.0: similarity = MSE_NUMERATOR / err else: similarity = 1.0 return similarity
img_recor1 = cv2.idct(recor_temp) return img_recor1 #dim = 30 similarity_1 = [] MSE = [] for i in range(val_data.shape[0]): image1 = val_data_noise[i].reshape(50, 60, 1)[:, :, 0] image2 = val_data[i].reshape(50, 60, 1)[:, :, 0] image1 = image1.astype('float') image2 = image2.astype('float') p1 = Image.fromarray(image2).convert('L') p2 = Image.fromarray(DCT(image1, dim = 30)).convert('L') similarity_temp_1 = pyssim.SSIM(p1).cw_ssim_value(p2) similarity_1.append([similarity_temp_1]) mse_ = mean_squared_error(image1, DCT(image1, dim = 30)) MSE.append(mse_) print('cw-ssim: %s'%np.mean(similarity_1)) print('MSE: %s'%np.mean(MSE)) #dim = 60 for i in range(val_data.shape[0]): image1 = val_data_noise[i].reshape(50, 60, 1)[:, :, 0] image2 = val_data[i].reshape(50, 60, 1)[:, :, 0] image1 = image1.astype('float') image2 = image2.astype('float')
import ssim.ssimlib as pyssim img1 = 'images/breast_img1.png' img2 = 'images/breast_img3.png' cw_ssim_value = pyssim.SSIM(img1).cw_ssim_value(img2) print(cw_ssim_value)