如何找到图像补丁/窗口之间的相似性



我想比较一个参考窗口(补丁(和从图像中获取的所有其他窗口(补丁(patch((之间的相似性。我的代码如下。

有人能帮我评估"ref"(参考窗口(和变量"test"给出的所有其他10000个窗口之间的相似性吗?谢谢

详细说明:我试着用for循环。这很耗时。我试着使用内置函数"ssim",但它说张量的维度不匹配。请提出任何方法来进行这个批处理

# Read grayscale image from file.
Im = Image.open("cameraman.png") 
#Resize it to desired shape (h,w)
Im = Im.resize((100,100))
# expand dimensions to get the shape [ no of batches, height, width, channel]
Im = np.expand_dims(Im,axis=0)
Im = np.expand_dims(Im,axis=0)
x = tf.convert_to_tensor(Im)
x=tf.reshape(x,[1,100,100,1])  # this is the required image shape in a tensor
# Break one image into windows of 11x11 (overlapping)
wsize=11
ws=50  # Index of centre window (this window is reference window)
#Extract windows of 11 x 11 around each pixel
p1=tf.extract_image_patches(x,sizes=[1,wsize,wsize,1],strides=[1,1,1,1],rates=[1,1,1,1],padding="SAME")
patches_shape = tf.shape(p1)
test=tf.reshape(p1, [tf.reduce_prod(patches_shape[0:3]), 11, 11, ])  # returns [#window_patches, h, w, c] 
print(test.shape)  #test has shape [ 10000, 11,11]
ref=test[5000,]    # this is the reference window of shape [ 1, 11,11]
ref=tf.reshape(ref,[1,11,11])
print(im1.shape)
The following statement says size mismatch:
ssim1 = tf.image.ssim(ref, test, max_val=255, filter_size=11,filter_sigma=1.5, k1=0.01, k2=0.03) 
**ValueError: Shapes (1, 11, 11) and (10000, 11, 11) are incompatible.**

我希望打印每个窗口和参考之间的距离。

您需要对齐第一个维度。您可以迭代您的10000个图像批次,也可以广播您的原始补丁。但是,从性能的角度来看,建议使用tf.map_fn()对它们进行迭代。

此外,您需要扩展最后一个维度,bctf.image.ssim需要一个三阶张量。下面是一个使用tf 2.0和热切执行测试的工作示例:

arr1 = tf.convert_to_tensor(np.random.random([10000, 11, 11, 1]), dtype=tf.dtypes.float32)
arr2 = tf.convert_to_tensor(np.random.random([1, 11, 11, 1]), dtype=tf.dtypes.float32)
result_tensor = tf.map_fn(lambda x: tf.image.ssim(arr2[1:], x, 1), arr1)

结果张量的形状为[10000,0]。要获得平均值,请调用tf.reduce_mean

然而,请修改11x11补丁的11x11过滤器形状,并在下次提供一个工作示例。

最新更新