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.

  • 相关阅读:
    帮人“解封微信”犯法?全国首例!判刑!
    热乎的校招面经试题解析——百度篇
    字节跳动入局在线教育:烧钱、亏钱
    TF-IDF 算法介绍
    Django ORM 常见查询条件
    Django中render和render_to_response的区别
    Python异步操作MongoDB --Motor的使用
    Java 如何抛出异常、自定义异常
    java项目中的classpath到底是什么
    maven里的modelVersion
  • 原文地址:https://www.cnblogs.com/lancelod/p/4089778.html
Copyright © 2011-2022 走看看