0

我有一个用 C 编写的去噪算法,我需要使用 weave.inline 来执行代码以去除图像中的噪声。该算法无法正常工作。它说 Weave 处理一维数组,这就是为什么我必须将二维数组转换为一维数组。这就是我所做的,但是当我将一个一维数组发送到 C 算法中时,似乎我要么只得到零,这取决于迭代次数,要么我得到的东西看起来与我发送的一维完全相同。 (但是当我进行相等性测试时,如果它们相同,就会发现它们不是)。谁能看到我的算法有什么问题?这是代码:

#!/usr/bin/env python
from PIL import Image
import numpy as np
from scipy.weave import inline
from scipy.weave import converters

def weave_iso_difusion_denoising(data0, m, n, kappa, iters):
    """
    Function of removing noise from pictures
   """

    c_code = r"""
     // Fill top and bottom edge
    for (int i=0; i<m; i++)
    {
        data1[m*i*0] = data0[m*i*0];
        data1[i*(n-1)] = data0[i*(n-1)];
    }

    // Fill left and right edges
    for (int i=1; i<n-1; i++) // skip corners
    {
        data1[n*0*i] = data0[n*0*i];
        data1[(n-1)*i] = data0[(n-1)*i];
    }

    for (int iter=0; iter<iters; iter++)
    {
        // Fill interior
        for (int i=1; i<m-1; i++)
        {
            for (int j=1; j<n-1; j++)
            {
                data1[n*i+j] = data0[n*i+j] +kappa*(data0[n*(i-1)+j]
                        +data0[n*i+(j-1)] -4*data0[n*i+j] +
                    data0[n*i+(j+1)] + data0[n*(i+1)+j]);
            }
        }
       tmp = data0;
       data0 = data1;
       data1 = tmp;
    }
    """
    data1 = np.zeros(data0.shape, dtype=data0.dtype)
    data1 = data1.ravel()
    tmp =  np.zeros(data0.shape, dtype=data0.dtype)
    tmp = tmp.ravel()
    var = ['data0', 'm', 'n', 'kappa', 'iters','data1','tmp']
    inline(c_code,var,type_converters=converters.blitz, compiler='gcc')
    return data1

if __name__ == '__main__':

    # from image-file to Numpy
    img = Image.open("disaster_before.jpg") # the noisy picture
    data0 = np.array(img.getdata()) # 1D-array
    print 'data0:',data0

    # executing the denoise-program
    kappa = 0.05; iters = 11; n = 375; m = 500
    data2 = weave_iso_difusion_denoising(data0, m, n,kappa, iters)
    print 'data:',data2

    print 'data2 - data0:',np.sum(abs(data2 - data0))

    # from Numpy to image-file
    img1 = Image.new('L', (m,n))
    img1.putdata(data2)
    img1.save("disaster_after_weave_v1.jpg")

output
"""
data0: [156 181 177 ..., 124  35  90]
data: [156 181 177 ..., 124  35  90]
data2 - data0: 18776776 # data0 and data1 is not the same even though they look the same
"""
4

1 回答 1

0

有了这个satement

print 'data2 - data0:',np.sum(abs(data2 - data0))

您可以测试data0anddata1是否相同或不同。关键是看算法是否能完成这项工作。

于 2014-11-08T18:33:06.550 回答