admin管理员组

文章数量:1405170

I am currently studying convolution in MATLAB and have encountered an issue. The code below is an attempt to manually implement the conv2 function using a for loop.

However, when I compare the convolution result of an image and an HPF (High-Pass Filter) using conv2 with the result from my manually implemented code, there is a discrepancy.

I am not sure where the mistake is. Interestingly, when I implemented an LPF (Low-Pass Filter) in the same way, the difference was zero.

Why does the HPF produce different values?

HPF = [-1 -1 -1;
       -1  8 -1;
       -1 -1 -1];
%--------------------------using function---------------------------------

HPFimage1 = conv2(grayfun,HPF,'same');

%--------------------------without funtion---------------------------------


%--------------------------zero padding---------------------------------

for i = 1 : row
    for j = 1 : col
        padding(i+1,j+1) = grayman(i,j);
    end
end

% ------------------------------HPF-------------------------
i = 2;
j = 2;

HPFimage2 = zeros(row,col);

for x = 0 : row-1
    for y = 0 : col-1

        % Extracting and calculating a 3x3 matrix
            sumval = 0;

        for m = 0 : 2
            for n = 0 : 2
               sumval = sumval + (padding(x+m+i-1,y+n+j-1)*HPF(m+1,n+1));
            end
        end
        HPFimage2(x+1,y+1) = sumval;

    end
end

HPFsubresult = abs(HPFimage1) - abs(HPFimage2);

本文标签: How to Apply the HPF FilterStack Overflow