Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何查找图像修补程序/窗口之间的相似性_Python_Tensorflow - Fatal编程技术网

Python 如何查找图像修补程序/窗口之间的相似性

Python 如何查找图像修补程序/窗口之间的相似性,python,tensorflow,Python,Tensorflow,我想比较一个参考窗口(补丁)和从图像中获取的所有其他窗口(补丁)之间的相似性。我的代码如下 有人能帮我评估“ref”(参考窗口)和变量“test”给出的所有其他10000个窗口之间的相似性吗?多谢各位 详细说明: 我试着使用for循环。这很费时。我试图使用内置函数“ssim”,但它表示张量的维数不匹配。请建议进行此批处理的任何方法 # Read grayscale image from file. Im = Image.open("cameraman.png") #Resize it

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

有人能帮我评估“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\u fn()
对它们进行迭代

此外,需要展开最后一个维度,bc
tf.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\u mean


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

非常感谢您令人信服的解释和解决方案代码。我只是tensorflow的初学者。所以map函数对我来说是新的。