# conv2 'valid' implementation

31 Ansichten (letzte 30 Tage)
Alla am 2 Nov. 2018
Kommentiert: Savannah Quinn am 13 Sep. 2020
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 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Savannah Quinn am 13 Sep. 2020
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?

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Lin Bai am 9 Jan. 2019
as mentioned by Bharath, it is more efficient to use Simulink model instead of the MATLAB code to implement the conv2d function. Here I post the link of the Simulink model, which uses the image filer from Vision HDL toolbox to implement conv2 function.
In this model, image filter uses 'same' instead since it is more widely used in deep learning.
##### 7 Kommentare6 ältere Kommentare anzeigen6 ältere Kommentare ausblenden
Alla am 21 Jan. 2019
Tremendous thanks to you Lin Bai, you have been a great help. there are not may people here who can help with these things specefically so your input is immensly valuable.

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

Bharath Venkataraman am 2 Nov. 2018
Bearbeitet: Bharath Venkataraman am 2 Nov. 2018
If you are looking for conv2 for image filtering, you can use the ImageFilter block and System object in Vision HDL Toolbox.
The Image Processing Toolbox has an example that shows how to do this .
##### 1 KommentarKeine anzeigenKeine ausblenden
Alla am 20 Nov. 2018
Bearbeitet: Alla am 20 Nov. 2018
Bharath Venkataraman Thank you for your answer, I'm working with matlab code and not simulink, and I plan on using conv2 for operations in a convolutional neural netowrk.

Melden Sie sich an, um zu kommentieren.

Bharath Venkataraman am 20 Nov. 2018
You can use the MATLAB workflow in the example for image filtering.
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Bharath Venkataraman am 29 Nov. 2018
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'.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on HDL Code Generation and Deployment in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by