The difference between conv2, Filter2 and IMFilter

Source: Internet
Author: User

Today saw the altar master math and the Forum of other cattle tolerance distortion, listen to the report feeling a lot, a word refueling it.

-------------------------------------conv2 function ----------------------------------------
1. Usage

    1. C=conv2 (A,b,shape); % convolution filter
Copy Code

A: Input image, B: Convolution core
Assuming that the input image A is a size of Ma x na and the convolution core b size is MB x NB,
When Shape=full, returns all two-dimensional convolution results, that is, the size of the returned C is (ma+mb-1) x (na+nb-1)
Shape=same, returns the center portion of the convolution that is the same size as a
Shape=valid, the boundary is not considered 0, that is, as long as there is 0 of the bounds of the operation of the computation of all, the size of the return C (ma-mb+1) x (na-nb+1)


2. Implementation steps
Assuming the input image A is a size of Ma x na and the convolution core size is MB x NB, the CONV2 function of MATLAB is implemented as follows:
A, the input image is 0, the first line before and after the last row of mb-1 rows, the first column and after the last column nb-1 column (note CONV2 does not support additional boundary supplement options, the function of the input always 0).
b, about the center of the convolution nucleus, rotating convolution core 180 degrees.
C, sliding convolution core, the center of the convolution nucleus in the image matrix of each element.
D, multiply the rotated convolution nucleus by the corresponding matrix element and then sum.

3. Realization Process Display
Imagine an image a=[4 3 1 2;0 1 1 3;5 2 0 0], convolution core b=[1 2 3;0-1 2;1 1 0]
A, the first is to follow the above steps to fill 0, such as the outer ring red for the complement of 0

<ignore_js_op>

B. Rotate the convolution core 180 degrees
<ignore_js_op>
C, the rotation of the nucleus on a slide, and then the corresponding position multiplied, and finally added, the following is shape=full,same,valid when the output image size, Position 1 indicates that the output image value from the current core computed value (corresponding to the upper left corner of the output image), Position 2 Indicates the end of the position (corresponding to the output image lower right)
<ignore_js_op> <ignore_js_op> <ignore_js_op>


-------------------------------------filter2 function ----------------------------------------
1. Usage

    1. B = Filter2 (H,a,shape); % correlation (correlation) filtering
Copy Code

A: Input image, H: Related core
Assuming that the input image A is a size of Ma x na and the correlation core h size is MB x NB,
When Shape=full, returns all two-dimensional convolution results, that is, the size of return B is (ma+mb-1) x (na+nb-1)
Shape=same, returns the center portion of the convolution that is the same size as a
Shape=valid, the boundary is not considered 0, that is, as long as there is 0 of the bounds of the operation of the computation, the size of the return B is (ma-mb+1) x (na-nb+1)

2. Implementation steps

Assuming that the input image A is a size of Ma x NA, the implementation process for FILTER2 with the associated core h size of MB x Nb,matlab is as follows:A, the input image is 0, the first line before and after the last row of mb-1 rows, the first column and after the last column nb-1 column (note Filter2 does not support additional boundary supplement options, the function of the input always 0).           b, sliding related nuclei, the center of the relevant nucleus is located in each element of the image matrix. c, multiply the correlation nucleus by the corresponding matrix element and then add note that the Filter2 does not rotate the nucleus 180°, the direct corresponding multiplication and the addition, this is different from the Filter2, the following two calculation results can be seen in the comparison.


3. Comparison of results of conv2 (convolution filter) and filter2 (correlated filtering)
a=[4  3   1   2;0   1   1   3;5   2   0   0], convolution coreb=[1   2    3;0  -1   2;1    1   0]
<ignore_js_op> <ignore_js_op>
conv2,shape=full Filter 2,shape=full



-------------------------------------IMFilter----------------------------------------
1. Usage

    1. B=imfilter (A,h,option1,option2,option3);
Copy Code

A: Input image, H: Filter Core
Option1: Boundary option, Optional: Complementary fixed value x (0 by default), Symmetric,replicate,circular
Option2: Output image size option, optional same (default), full
Option3: decided to use the same correlation filter as the filter2 or the same convolution filter as the CONV2



------------------------------------- Final Summary ----------------------------------------

1, Filter2, conv2 convert the input to a double type, the output is double, the input is always 0 (zero padded), does not support other boundary complementary options. 2, IMFilter: does not convert the input to double, the output is only the same type as the input, there are flexible boundary complementary options





The difference between conv2, Filter2 and IMFilter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.