Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implemet 1D convolution in opencv?

Is there any way to implement convolution of 1D signal in OpenCV?

As I can see there is only filter2D, but I'm looking for something like Matlab's convn.

like image 678
mrgloom Avatar asked Nov 09 '22 08:11

mrgloom


2 Answers

For 1-D convolutions, you might want to look at np.convolve.

See here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html

Python OpenCV programs that need a 1-D convolution can use it readily.

like image 58
M. Mortazavi Avatar answered Nov 15 '22 05:11

M. Mortazavi


You can always view a 1D vector as a 2D mat, and thus simply calling the opencv build-it functions resolves the problem.

Below is a snippet that I use to smooth an image histogram.

# Inputs: 
#     gray = a gray scale image
#     smoothing_nbins = int, the width of 1D filter
# Outputs:
#     hist_sm = the smoothed image histogram

hist = cv2.calcHist([gray],[0],None,[256],[0,256])
hist_sm = cv2.blur(hist, (1, smoothing_nbins))

As you can see, the only trick you need here is to set one filter dimension to 1.

like image 33
pitfall Avatar answered Nov 15 '22 07:11

pitfall