What is the Image Processing Toolbox Convention for Input to freqz2() ?
6 views (last 30 days)
Show older comments
I was trying to learn how to use the function freqz2 in the Image Processing Toolbox. That doc page says that the FIR filter input, h, is "in the form of a computational molecule." What is a "computational molecule?" A quick internet search didn't help much. Is that a term commonly used in image processing?
Anyway, because I don't know what that means, I searched around some more and found on What Is Image Filtering in the Spatial Domain? the statement that "The Image Processing Toolbox™ filter design functions return correlation kernels." So I assumed that h should be a correlation kernel (don't know why the doc page for freqz2 doesn't just say that if that is, in fact, the case).
Aside: the linked doc page on "What is Image Filtering ...." has a mistake. The graphic immediately under the line "Computing the (2, 4) Output of Convolution" says "value of rotated convolution kernel" but it should say "value of rotated correlation kernel."
Define an input FIR filter
rng(100);
h = rand(5);
If that's a correlation kernel with the origin at the center and the upper left corner corresponding to [-2,-2], then it seems to me that its 5x5 frequency response would be
H0 = fftshift(fft2(ifftshift(h)));
In order to get the same result from freqz2(), the input has to be rotated 180 deg
H1 = freqz2(rot90(h,-2),5,5,[1 1]);
isequal(H1,H0)
Inside the code of freqz2 is the comment "% Unrotate filter since FIR filters are rotated." Rotated relative to what?
Can anyone explain why that rot90 is needed on input to freqz2 or where the anlaysis has an incorrect assumption or calculation, or any other misunderstanding on my part?
3 Comments
Walter Roberson
on 4 Aug 2024
I've heard that the plural of computational molecule, is computational polycule.
Accepted Answer
Steve Eddins
on 5 Aug 2024
The convention for Image Processing Toolbox filter design functions is for the filter to be specified as a correlation kernel. This convention dates back to early 1993, when version 1.0 of the toolbox was under development. (I joined MathWorks a bit later, at the end of 1993.) I believe that the convention was chosen to be consistent with the MATLAB function filter2, which computed correlation instead of convolution. I don’t know this for sure, but I suspect that filter2 was originally implemented by someone with a computer vision background, instead of a signal processing background, and computer vision papers and textbooks in the 1980s tended to be pretty loose in their definition of 2-D filtering.
Anyway, when I joined MathWorks and took over Image Processing Toolbox development following its initial release, I strongly disliked this convention, but I felt at the time that I couldn’t change it, and so related toolbox functions continued to use correlation as the default convention.
The term “computational molecule” was initially used in the toolbox version 1.0 documentation, and it just means correlation kernel. I don’t know where the term originated. I have never seen it used elsewhere.
I believe you are correct that there is an error in the description of convolution on the What Is Image Filtering in the Spatial Domain documentation page. I would probably recommend the following edits:
- Change “and the correlation kernel is” to “and the convolution kernel is”.
- Change step 1 to “Rotate the convolution kernel 180 degrees about its center element.”
- Change step 2 to “Slide the center element of the rotated convolution kernel …”
Regarding the code comment you found inside freqz2 — the frequency response of a filter is the Fourier transform of the impulse response, which for an FIR filter is the convolution kernel. So, the “unrotate the filter” comment refers to the fact that the input is expected to be correlation kernel, which is a rotation of the convolution kernel.
This is all pretty messy, conceptually, and I really wish I had been sufficiently bold in 1993 to go ahead and change the convention.
I retired from MathWorks in March, so I can no longer just stroll down the hall to talk with one of the writers on the toolbox team. But I will submit two service requests:
- Get rid of the “computational molecule” terminology in the toolbox documentation.
- Fix the convolution description on the What Is Image Filtering in the Spatial Domain page.
2 Comments
Steve Eddins
on 5 Aug 2024
Edited: Steve Eddins
on 5 Aug 2024
Hi Paul,
Your 1-D example and code all look good to me.
Interesting that filter2() and filter() are fundamentally different, though maybe that just reflects a difference between the image processing and signal processing communities.
I came to image processing through a graduate department program specializing in digital signal processing, and so I consider myself to be a member of both communities. I do not associate correlation-based 2-D filtering with the image processing community. I do not know for sure why correlation was chosen for filter2. I speculated in my answer that perhaps someone with a computer vision background was involved in the design of filter2. In the 1980s and 1990s, a lot of work in computer vision was being done by people with computer science backgrounds, and so the terminology and concepts were not always consistent with the EE-based signals and systems domain. In particular, I had the impression that filtering was not as precisely defined in the computer vision literature back then as it was in the signal processing community.
Seems like there would be a lot of confusion if the Signal Processing Toolbox were ever to be extended to 2D.
Extending Signal Processing Toolbox to 2-D seems unlikely to me, based on the history of product development of MATLAB, Signal Processing Toolbox, and Image Processing Toolbox. However, there has been an effort over some time (the past 10 years or so?) to move basic 1-D signal analysis and filtering capability from the toolbox into MATLAB. In some cases, 2-D versions of those capabilities have also been added to MATLAB. In most cases, the associated functional designs (function names, syntaxes, behaviors, etc.) have been new. Check out the release notes for MATLAB in the math and data analysis areas going back several years.
... non-causal ...
The term "non-causal" is not commonly used in image processing or computer vision because the filtering domain is space, not time. In image processing and computer vision, it almost always makes the most sense to avoid introducing a phase shift in the filtering process. You wouldn't want a smoothing filter, for example, to shift a picture to the right. That is why filter2 (and the Image Processing Toolbox function imfilter) assume that the filter's origin is the center element, not the upper-left element.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!