Manual convolution of 2D square matrices
6 ビュー (過去 30 日間)
古いコメントを表示
I am trying to convolute the sum of 2D square matrices by zero padding. The zero padding should be equal to the size difference of the two matrices. i.e a matrix with a size of 5x5 and a second matrix with a size of 3x3, therefore the padded array should be equal to the first matrix’s size with addition 2 columns and 2 rows (post padding) (hence equal to 7). My code works only for the difference greater than 1 and 2. I marked my problem with a comment,hope that I clarified enough.
function final M1=rand(6) M2=eye(4)
n=size(M1)-size(M2);%subtract size M2 from size M1 to get the right output padding size m=size(M2)-1; A=padarray(M1,n,"post"); %padding M1 with the padded size Output=zeros(size(M1)); %output matrix,set it firstly to zero
for i=1:size(A,1)-n for j=1:size(A,2)-n if n >= size(M2) temp=A(i:i+m,j:j+m).*M2; Output(i,j)=sum(temp(:)); elseif m>n temp=A(i:i+m,j:j+m).*M2; %Problem: Index in position 2 exceeds array bounds. Index must not exceed 8. Output(i,j)=sum(temp(:)); else temp=A(i:i+n,j:j+n).*M2; Output(i,j)=sum(temp(:)); end end end display(Output) end
4 件のコメント
DGM
2022 年 12 月 2 日
Here's some troubleshooting.
% inputs
M1 = rand(6) % this is 6x6
M2 = eye(4) % this is 4x4
% subtract size M2 from size M1 to get the right output padding size
n = size(M1)-size(M2); % [2 2]
m = size(M2)-1; % [3 3]
% padding M1 with the padded size
A = padarray(M1,n,"post"); % pads the trailing edges with 2px each
% A is now 8x8, but if you're corner-centered,
% then you'd need 3px to hang M2 off the edge of M1
% output matrix,set it firstly to zero
Output = zeros(size(M1));
for i = 1:size(A,1)-n
for j = 1:size(A,2)-n
% loop-invariant conditionals should be outside the loop
% are you trying to test i,j to detect edge-approach?
if n >= size(M2)
% test is only true when M1 is at least twice the size of M2 in both dimensions
temp = A(i:i+m,j:j+m).*M2;
Output(i,j) = sum(temp(:));
elseif m>n
% test is only true when m>n in both dimensions
temp = A(i:i+m,j:j+m).*M2;
% Problem: Index in position 2 exceeds array bounds. Index must not exceed 8.
% see note about size of A
% A is size(M1)+n; 6x6 + 2x2 = 8x8
% loop indexing runs up to size(A)-n, which would be size(M1), i.e. 6x6
% the window indexing in these first two cases extends m pixels, i.e. 9x9
Output(i,j) = sum(temp(:));
else
temp = A(i:i+n,j:j+n).*M2;
Output(i,j) = sum(temp(:));
end
end
end
disp(Output)
I'm not sure why all the conditionals are being used. Sometimes that's done as part of a window-truncation routine near the edges, but I don't really see that happening here. If the notes don't help, you might want to clarify what the conditions are intended to test.
If we're assuming M2 is smaller than M1, and that the "center" or indexing point of the window is the upper left pixel (as implied by the indexing A(i:i+m,j:j+m)), then your padding width would be m, not n.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!