conv2 'valid' implementation
이전 댓글 표시
I need to implement the conv2 function for a the HDL coder since it's not supported, I believe that I have the main function down, what I'm struggling with is the implementation of conv2 'valid' which is described in the documentation with — Returns only parts of the convolution that are computed without zero-padded edges-
The code for conv2 is the one I'm sharing, any help would be appreciated.
function B = convolve(A, k)
[r, c] = size(A);
[m, n] = size(k);
h = rot90(k, 2);
center = floor((size(h)+1)/2);
left = center(2) - 1;
right = n - center(2);
top = center(1) - 1;
bottom = m - center(1);
Rep = zeros(r + top + bottom, c + left + right);
for x = 1 + top : r + top
for y = 1 + left : c + left
Rep(x,y) = A(x - top, y - left);
end
end
B = zeros(r , c);
for x = 1 : r
for y = 1 : c
for i = 1 : m
for j = 1 : n
q = x - 1;
w = y -1;
B(x, y) = B(x, y) + (Rep(i + q, j + w) * h(i, j));
end
end
end
end
댓글 수: 2
Juan Pereira
2019년 10월 7일
Useful, thank you.
Savannah Quinn
2020년 9월 13일
Hello, I am trying to use this code to implement various image filters however I keep getting the an index out of bounds error due to h(i,j). Any ideas?
채택된 답변
추가 답변 (2개)
Bharath Venkataraman
2018년 11월 2일
편집: Bharath Venkataraman
2018년 11월 2일
0 개 추천
If you are looking for conv2 for image filtering, you can use the ImageFilter block and System object in Vision HDL Toolbox.
Bharath Venkataraman
2018년 11월 20일
0 개 추천
댓글 수: 2
Alla
2018년 11월 29일
Bharath Venkataraman
2018년 11월 29일
conv2 with 'valid' returns a smaller image (without padding), so please make sure you are only comparing the central part of the image.
You may find it closer to matching if you use 'same' instead of 'valid'.
카테고리
도움말 센터 및 File Exchange에서 Code Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!