如何利用weave算法对c码图像进行去噪

本文关键字:图像 何利用 weave 算法 | 更新日期: 2023-09-27 18:29:45

我有一个用C编写的去噪算法,我需要使用weave.inline来执行代码,以便从图像中去除噪声。算法无法正常工作。上面说Weave在1D阵列上工作,所以这就是为什么我必须将2D阵列转换为1D阵列。这就是我所做的,但当我将一个1D数组发送到C算法中时,我似乎要么只得到了零,这取决于迭代次数,要么得到了与我发送的1D完全相同的东西。有人能看到我的算法出了什么问题吗?这是代码:

#!/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
"""

如何利用weave算法对c码图像进行去噪

有了这个卫星

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

您可以测试data0data1是相同还是不同。重点是看算法是否能完成任务。