【问题标题】:how to use weave to exeute c-code image denoising algorithm?如何使用 weave 执行 c 代码图像去噪算法?
【发布时间】:2014-11-08 17:55:58
【问题描述】:

我有一个用 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
"""

【问题讨论】:

  • data2 - data0 在没有绝对值和总和的情况下显示什么?
  • 另外,您是否在打印 data0 和打印两个数组之间的差异之间更改 data0?

标签: c# python inline-code noise-reduction


【解决方案1】:

有了这个声明

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

您可以测试data0data1 是相同还是不同。关键是看算法是否能胜任。

【讨论】:

    猜你喜欢
    • 2015-01-09
    • 2010-12-08
    • 2022-10-21
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2011-08-16
    • 2019-08-28
    • 2014-04-25
    相关资源
    最近更新 更多