I'm trying to create a filter that uses vectors as coefficients.
A typical kernel used in imfilter
looks like:
0 1 0
1 -4 1
0 1 0
Where each element is a scalar, and the filtering basically takes all windows of 3x3 of the image (MxN), multiplies the pixel values by the matching coefficients and sums all products to place the output pixel.
I would like to extend this, so that each element in the kernel is not a scalar, but actually a vector (Px1). This makes the kernel something like 3x3xP. The rest of the functionality should be the same: Multiply the coefficient (vector) by the image pixel (scalar * vector), sum all products (sum of vectors of same dimensions), and the output matrix should have the dimensions of (MxNxP).
I know that imfilter
doesn't support that and produces a result of (MxN).
Is there another Matlab function that does this, or do I need to implement from scratch?
Some comments: The image is usually logical (0 or 1), and some of the filter elements can be zero, and not contribute to the sum.
Without another solution, I would brute-force it. If each vector in your 3x3 filter has P elements, I'd apply each element independently and sum the result.
%load my image
im = imread(fname);
%define my kernel
h = zeros(3,3,P); % P elements deep
h(:,:,1) = [0 1 0; 1 -4 1; 0 1 0]; %first element my kernel
h(:,:,2) = [0.2 1.5 0.2; 1.5 -6 1.5; 0.2 1.5 0.2]; %second element
%keep adding elements until you get your P elements
%loop over each element in your kernel
output = zeros(size(im))
for I=1:size(h,3)
output = output + imfilter(im,h(:,:,I)); %apply filter and accumulate
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.