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.

  • 相关阅读:
    dede图片横向滚动
    dede各种运用[转]
    PROFIBUS-DP现场总线的结构及应用
    51单片机的中断优先级及中断嵌套
    WPF里面制作圆角文本框
    【转】什么叫51单片机最小系统
    【转】(C#)OPC客户端源码
    路漫漫其修远兮,吾要上下左右前后而求索
    二叉树创建为什么用二级指针
    无向图的邻接表创建
  • 原文地址:https://www.cnblogs.com/lancelod/p/4089778.html
Copyright © 2011-2022 走看看