zoukankan      html  css  js  c++  java
  • [CLPR] 卷积还是相关?

    I am doing something about convolving images in Python and for sake of speed I chose opencv 2.4.9.

    Opencv offers a way called filter2D to do this and here's its docs:http://docs.opencv.org/modules/imgproc/doc/filtering.html?highlight=filter2d#filter2d

    In docs, it says:

    Convolves an image with the kernel.

    But I have doubts(caused by something else) so I make some experiments on it:

    First, I make a normal 3x3 matrix a using numpy as:

      [[ 1.,  5.,  0.], 
       [ 7.,  2.,  9.], 
       [ 2.,  3.,  4.]]
    

    Then, I make a 2x2 matrix b as the cornel as:

    >>> b

      [[ 1.,  2.],
       [ 3.,  4.]]
    

    Finally, in order to make it clear to see difference between convolve and correlate, rotate b by 180 degree and b will look like:

      [[ 4.,  3.],
       [ 2.,  1.]]
    

    Now, All pre-work is done. We could begin the experiment.

    Step 1. Use scipy.ndimage.convolvendconv = ndimage.convolve(a, b, mode = 'constant')and ndconv is:

      [[ 35.,  33.,  18.],
       [ 41.,  45.,  44.],
       [ 17.,  24.,  16.]]
    

    Convolution op will rotate b by 180 degree and do correlation using b on a. So ndconv[0][0] = 4*1+3*5+2*7+1*2 = 35, and ndconv[2][2] = 4*4+3*0+2*0+1*0 = 16

    This result is correct.

    Step 2. Use scipy.ndimage.correlatendcorr = ndimage.correlate(a, b, mode = 'constant')and ndcorr is:

      [[  4.,  23.,  15.],
       [ 30.,  40.,  47.],
       [ 22.,  29.,  45.]]
    

    According to correlation's definition, ndcorr[0][0] = 1*0+2*0+3*0+4*1 = 4 because the border will expand by 0.

    (Someone may be confused by the expandation's difference between conv and corr. It seems convolveexpand image in directions right and down while correlate in directions left and up.)

    But this is not the point.

    Step 3. Use cv2.filter2Dcvfilter = cv2.filter2D(a, -1, b) and cvfilter is:

      [[ 35.,  34.,  35.],
       [ 41.,  40.,  47.],
       [ 33.,  29.,  45.]]
    

    If we ignore the border cases, we will find that what cv2.filter2D did is actually a correlation other than aconvolution! How could I say that?

    because cvfilter[1..2][1..2] == ndcorr[1..2][1..2].

    WEIRD, isn't it?

    Could anyone be able to tell the real thing that cv2.filter2D do? Thanks a lot.

  • 相关阅读:
    【NX二次开发】难点清单
    【NX二次开发】Block UI 目录
    Open C
    【NX二次开发】缝合片体例子UF_MODL_create_sew
    【NX二次开发】拉伸的偏置方向猜想与验证
    【NX二次开发】拉伸面、拉伸封闭曲线成片体UF_MODL_create_extrusion
    【NX二次开发】创建有界平面UF_MODL_create_bplane
    【VBA】一些判断
    【VBA】日期时间
    【VBA】单元格插入图片,单元格删除图片
  • 原文地址:https://www.cnblogs.com/lancelod/p/4089778.html
Copyright © 2011-2022 走看看