OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 06 Feb 2020 01:53:55 -0600Does OpenCV uses GEMM for convolution?http://answers.opencv.org/question/225888/does-opencv-uses-gemm-for-convolution/ Hello
I've read that convolution can be accelerated by using im2col + GEMM. I just wanted to know how exactly separable convolutions are implemented in OpenCV. The source code seems a little bit complicated. Does it use GEMM? Zana Zakaryaie NejadThu, 06 Feb 2020 01:53:55 -0600http://answers.opencv.org/question/225888/I can't get different convolution results when calling the function successively with different parameters.http://answers.opencv.org/question/214108/i-cant-get-different-convolution-results-when-calling-the-function-successively-with-different-parameters/ void Gauss_conv(const cv::cuda::GpuMat& src,
cv::cuda::GpuMat& conv,
int gauss_size,
bool convert_to_8bit = true)
{
cv::Mat gauss_kernel;
cv::cuda::GpuMat gf_src, gf_gauss, gf_conv;
std::vector <cv::cuda::GpuMat> gfv_src, gfv_gauss, gfv_conv;
src.convertTo(gf_src, CV_32FC3);
//Split the source image on the GPU
cv::cuda::split(gf_src, gfv_conv);
cv::cuda::split(gf_src, gfv_src);
//Prepare the input image for Gauss filtering
cv::cuda::copyMakeBorder(gf_src, gf_gauss, 0.5 * gauss_size,
0.5 * gauss_size, 0.5 * gauss_size, 0.5 * gauss_size,
cv::BORDER_REPLICATE);
cv::cuda::split(gf_gauss, gfv_gauss);
//Create the Gauss kernel and upload it to the GPU
cv::Ptr < cv::cuda::Convolution> c1 = cv::cuda::createConvolution();
cv::mulTransposed(cv::getGaussianKernel(gauss_size, -1, CV_32FC1),
gauss_kernel, false);
gf_gauss.upload(gauss_kernel);
//Apply Gaussian blur to all channels independently, also compute subtraction image
for (int i = 0; i < src.channels(); i++)
{
c1->convolve(gfv_gauss[i], gf_gauss, gfv_conv[i], true);
}
cv::cuda::merge(gfv_conv, conv);
}
//in main
Gauss_conv(gpu_src_uchar, conv1, 131, true); //131
Gauss_conv(gpu_src_uchar, conv2, 531, true); //531
Gauss_conv(gpu_src_uchar, conv3, 1631, true); //1631
I want three different convolution results: conv1, conv2, and conv3. But I got the same result of conv1 for all cases. Can someone help me fix this problem.
huaMon, 10 Jun 2019 06:54:13 -0500http://answers.opencv.org/question/214108/filter2d vs conv2http://answers.opencv.org/question/201072/filter2d-vs-conv2/The result I get from conversion of conv2 function in matlab is very different from the result I get in opencv using filter2d function.
In opencv in the position of (0,0) I get 1.1175871e-08 while the result I get in matlab is -0.9639
In Matlab:
A = [1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6; 1,2,3,4,5,6];
k = [-0.014, -0.45, 0, 0.45, 0.014];
conv2(A, k, 'same');
In opencv/c++:
cv::Mat dst;
float K[5] = {-0.014, -0.45, 0, 0.45, 0.014};
cv::Mat kernel(1, 5, CV_32F, K);
Mat kernel-p(1,5, CV_32F);
cv::flip(kernel, kernel-p, -1);
float test[6][6] = {{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6},{1, 2, 3, 4,5,6}};
cv::Mat myMat(3, 4, CV_32FC1, &test);
filter2D(myMat, dst, -1 , kernel , Point( -1, -1 ), 0, BORDER_DEFAULT);
What am I doing wrong in the opencv code?tinahomSat, 13 Oct 2018 05:49:45 -0500http://answers.opencv.org/question/201072/OpenCV Python GPU support ? or faster variance convolutionhttp://answers.opencv.org/question/165502/opencv-python-gpu-support-or-faster-variance-convolution/Hi,
I was wondering if the current OpenCV Python had GPU support yet ?
OR is there a faster way to calculate convolved variance ?
MaxFrom3DArray = numpy.amax(imgArray, axis=0) # where imgArray is a 3D array
Back2ImMax = Image.fromarray(MaxFrom3DArray, 'P')
Back2ImMax.save(os.path.join(MaxFromMulti, filename), "TIFF")
ForVariance = cv2.imread((MaxFromMulti + filename), cv2.IMREAD_UNCHANGED)
wlen = 40
def winVar(img, wlen):
wmean, wsqrmean = (cv2.boxFilter(x, -1, (wlen, wlen),
borderType=cv2.BORDER_REFLECT) for x in (img, img*img))
return wsqrmean - wmean*wmean
windowVar = winVar(ForVariance, wlen)
numpy.set_printoptions(threshold='nan')
print windowVar
This takes hours in Python, and ages using python multi-threading, with CPU cores maxed out.
It takes a fraction of a second and hardly any cpu usage when serialised in c sharp.
Doesn't something seem a bit off about that ?
Thanks in advance
TWPTimWebPhoenixTue, 11 Jul 2017 06:53:18 -0500http://answers.opencv.org/question/165502/Cuda Convolve VS filter2D openCV 3.1.0http://answers.opencv.org/question/120598/cuda-convolve-vs-filter2d-opencv-310/Hello, I'm using OpenCV 3.1.0 with CUDA on Intel Xeon 5110 @ 1.60 Ghz x2 CPU + Nvidia Quadro 600 + 4GB RAM with Qt on Fedora 23 OS and I'm concerned about convolution speed.
What I've got from my test code is that filter2D convolution of an image with a 3x3 kernel is much faster than cuda Convolve as far as the image size is not too big (threshold around 1280x1024) and surprisingly always faster than separate convolution (first with 3x1 then 1x3 kernels), I was expecting from theory 2/3 processing time (3+3 rather than 3x3). Moreover the output image size with cuda convolve is smaller than the original one, I was expecting same size from documentation.
Is there anything wrong in what I'm doing? Any suggestion to speed up convolution for images around 640x480?
You can find below the test code I used:
cv::cuda::GpuMat temp2; // ---- is a B/W image different size
//-----fill up the temp2 image
....
//---------------------------
Mat dst_x;
Mat dst_x1;
Mat dst_x2;
Mat tmp_2;
cv::cuda::GpuMat fx;
Mat kernel_x = (Mat_<double>(3,3) << 2, 0, -2, 4, 0, -4, 2, 0, -2);
Mat kernel_x1 = (Mat_<double>(3,1) << 2, 4, 2); //----separate x convolution
Mat kernel_x2 = (Mat_<double>(1,3) << 1, 0, -1);
temp2.download(tmp_2);
int64 t1 = getTickCount();
cv::filter2D(tmp_2, dst_x1, -1,kernel_x1);
cv::filter2D(dst_x1, dst_x2, -1,kernel_x2);
int64 t2 = getTick();
std::cout << "Time passed in ms: " << (((t2 - t1) / 1e9)*1000.) << std::endl;
//int64 t1 = getTickCount();
cv::filter2D(tmp_2, dst_x, -1,kernel_x);
//int64 t2 = getTick();
//std::cout << "Time passed in ms: " << (((t2 - t1) / 1e9)*1000.) << std::endl;
//----CUDA convolution---------
kernel_x.convertTo(kernel_x,CV_32FC1);
//int64 t1 = getTickCount();
Ptr<cuda::Convolution> convolver = cuda::createConvolution(Size(3, 3));
convolver->convolve(temp2, kernel_x, fx);
//int64 t2 = getTick();
//std::cout << "Time passed in ms: " << (((t2 - t1) / 1e9)*1000.) << std::endl;
//----END CUDA convolution---------
I can sum up the results as follows:
Image size (30,40) (rows,cols)
Time passed in ms: 0.083827filter2D convolution with kernel size (3,3)output image same size
Time passed in ms: 0.044761filter2D separated convolution with kernel size (1,3) and (3,1)output image same size
Time passed in ms: 5.95849CUDA convolve convolution with kernel size (3,3)output image size (28,38);
Image size (118,158)
Time passed in ms: 0.204968filter2D convolution with kernel size (3,3)output image same size
Time passed in ms: 0.27658filter2D separated convolution with kernel size (1,3) and (3,1)output image same size
Time passed in ms: 7.03869CUDA convolve convolution with kernel size (3,3)output image size (116,156);
Image size (469,629)
Time passed in ms: 2.51682filter2D convolution with kernel size (3,3)output image same size
Time passed in ms: 5.72645filter2D separated convolution with kernel size (1,3) and (3,1)output image same size
Time passed in ms: 9.31991CUDA convolve convolution with kernel size (3,3)output image size (467,627);
federocchiThu, 29 Dec 2016 12:22:23 -0600http://answers.opencv.org/question/120598/is it possible to apply convolution in multiple dimensions?http://answers.opencv.org/question/82821/is-it-possible-to-apply-convolution-in-multiple-dimensions/I would like to know if there is any function to apply convolution in more than 2 dimensions in Opencv. I know that there is the `filter2d()` but this one is limited in 2d right? So I was wondering if there is something similar to [`convn`](http://www.mathworks.com/help/matlab/ref/convn.html) in matlab which allows you to apply convolution in more dimensions.theodoreFri, 08 Jan 2016 05:20:57 -0600http://answers.opencv.org/question/82821/