How to use cuda Gaussian Blur in opencv [duplicate] - opencv

I understood that in OpenCV 3.0 the module GPU has been replaced by module CUDA, or better it has been split into several modules.
So cv::gpu::GpuMat has been replaced by cv::cuda::GpuMat, fine.
But what about the functions?
Where for example have the following moved to:
cv::gpu::GaussianBlurr ?
cv::gpu::Stream stream;
stream.enqueueConvert(...)
Apparently they are not under cuda module (eg. no cv::cuda::GaussianBlurr). Where can this functionality be found in OpenCV 3.0?

All CUDA-accelerated filters (Blur, GaussianBlur, Sobel, etc.) are located in cudafilters module: https://github.com/Itseez/opencv/blob/master/modules/cudafilters/include/opencv2/cudafilters.hpp
New API uses Algorthim-base approach:
cv::Ptr<cv::cuda::Filter> filter = cv::cuda::createGaussianFilter(src.type(), dst.type(), ksize, sigma);
filter->apply(src, dst);
The new approach helps to reduce memory allocations for internal buffers and reduce overhead from filter initialization stage.

Related

Whats the right way of using openCV with openVINO?

Dislcaimer: I have never used openCV or openVINO or for the fact anything even close to ML before. However I've been slamming my head studying neural-networks(reading material online) because I've to work with intel's openVINO on an edge device.
Here's what the official documentation says about using openCV with openVINO(using openVINO's inference engine with openCV).
->Optimize the pretrained model with openVINO's model optimizer(creating the IR file pair)
use these IR files with
openCV's dnn.readnet() //this is where the inference engine gets set?
https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_raspbian.html
Tried digging more and found a third party reference. Here a difference approach is taken.
->Intermediatte files (bin/xml are not created. Instead caffe model file is used)
->the inference engine is defined explicitly with the following line
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
https://www.learnopencv.com/using-openvino-with-opencv/
Now I know to utilize openCV we have to use it's inference engine with pretrained models. I want to know which of the two approach is the correct(or preferred) one, and if rather I'm missing out no something.
You can get started using OpenVino from: https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_windows.html
You would require a set of pre-requsites to run your sample. OpenCV is your Computer Vision package which can used for Image processing.
Openvino inference requires you to convert any of your trained models(.caffemodel,.pb,etc.) to Intermediate representations(.xml,.bin) files.
For a better understanding and sample demos on OpenVino, watch the videos/subscribe to the OpenVino Youtube channel: https://www.youtube.com/channel/UCkN8KINLvP1rMkL4trkNgTg
If the topology that you are using is supported by OpenVino,the best way to use is the opencv that comes with openvino. For that you need to
1.Initialize the openvino environment by running the setupvars.bat in your openvino path(C:\Program Files (x86)\IntelSWTools\openvino\bin)
2.Generate the IR file (xml&bin)for your model using model optimizer.
3.Run using inference engine samples in the path /inference_engine_samples_build/
If the topology is not supported, then you can go for the other procedure that you mentioned.
The most common issues I ran into:
setupvars.bat must be run within the same terminal, or use os.environ["varname"] = varvalue
OpenCV needs to be built with support for the inference engines (ie DLDT). There are pre-built binaries here: https://github.com/opencv/opencv/wiki/Intel%27s-Deep-Learning-Inference-Engine-backend
Target inference engine: net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
Target NCS2: net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
The OpenCV pre-built binary located in the OpenVino directory already has IE support and is also an option.
Note that the Neural Compute Stick 2 AKA NCS2 (OpenVino IE/VPU/MYRIAD) requires FP16 model formats (float16). Also try to keep you image in this format to avoid conversion penalties. You can input images as any of these formats though: FP32, FP16, U8
I found this guide helpful: https://learnopencv.com/using-openvino-with-opencv/
Here's an example targetting the NCS2 from https://medium.com/sclable/intel-openvino-with-opencv-f5ad03363a38:
# Load the model.
net = cv2.dnn.readNet(ARCH_FPATH, MODEL_FPATH)
# Specify target device.
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_INFERENCE_ENGINE)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD) # NCS 2
# Read an image.
print("Processing input image...")
img = cv2.imread(IMG_FPATH)
if img is None:
raise Exception(f'Image not found here: {IMG_FPATH}')
# Prepare input blob and perform inference
blob = cv2.dnn.blobFromImage(img, size=(672, 384), ddepth=cv2.CV_8U)
net.setInput(blob)
out = net.forward()
# Draw detected faces
for detect in out.reshape(-1, 7):
conf = float(detect[2])
xmin = int(detect[3] * frame.shape[1])
ymin = int(detect[4] * frame.shape[0])
xmax = int(detect[5] * frame.shape[1])
ymax = int(detect[6] * frame.shape[0])
if conf > CONF_THRESH:
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), color=(0, 255, 0))
There are more samples here (jupyter notebook/python): https://github.com/sclable/openvino_opencv

Is there any equivalent function in the opencv gpu namespace to the function cvInvert from the cv namespace?

I'm trying to port an openCV application from the cv to the gpu namespace to take advantage of GPU optimizations and I can't find an equivalent function to cvInvert in the docs. Could you please tell me if such a function exists?
Opencv does not have an equivalent GPU invert function.
It would be in the gpu operations on matrices page but that page does not contain any functions that invert matrices.

OpenCV GPU Primitives

Are the OpenCV primitives based on the CUDA Nvidia Performance Primitives (NPP)?.
By primitives I mean the same ones implemented in the NPP library, for example: boxFilter, Mirror, Convolution...
I would like to know about this issue as I'm planning use the NPP library. However, OpenCV has more functions that could help me for example in border treatment for image processing.
OpenCV uses NPP library for some functions. But it is hard to create a compelete list of such functions.
Some functions uses only NPP implemetation (boxFilter, graphcut, histEven).
Other functions uses different implemetations for different input parameters. For example, cv::gpu::resize uses NPP for some input parameters (CV_8UC1 and CV_8UC3 types, INTER_NEAREST and INTER_LINEAR interpolation mode) and for other parameters it uses own implementation.
Great webinar about OpenCV on a GPU using CUDA
Video - http://on-demand.gputechconf.com/gtc/2013/webinar/opencv.mp4
Slides PDF - http://on-demand.gputechconf.com/gtc/2013/webinar/opencv-gtc-express-shalini-gupta.pdf

Compressed Histogram of Gradients (CHoG)

I am trying to understand the implementation of low bitrate descriptor Compressed Histogram of Gradients (CHoG) from Stanford Mobile Visual Search publication. Is there any open-source code available in OpenCV?
Actually I contacted the Authors of CHoG they say its a copyrighted code.
I don't think OpenCV has an implementation of Compressed HOG. However, OpenCV does provide the traditional HOG implementation from the Dalal-Triggs 2005 paper.
Here's how to use OpenCV's HOGDescriptor, with the default parameters from the Dalal-Triggs paper:
cv::HOGDescriptor d();
vector<float> descriptorsValues; //this is the useful output
vector<cv::Point> locations;
d.compute(img, descriptorsValues, cv::Size(0,0), cv::Size(0,0), locations);
If you want to customize the HOG setup, you can use the HOGDescriptor constructor that takes custom parameters:
cv::HOGDescriptor d(win_size, block_size, block_stride, cell_size, nOri, 1, -1, cv::HOGDescriptor::L2Hys, 0.2, gamma_corr, nLevels);
There's also a GPU version of HOGDescriptor in OpenCV.

How to do a Gaussian filtering in 3D

How do i do a gaussi smoothing in the 3th dimension?
I have this detection pyramid, votes accumulated at four scales. Objects are found at each peak.
I already smoothed each of them in 2d, and reading in my papers that i need to filter the third dimension with a \sigma = 1, which i havent tried before, i am not even sure what it means.
I Figured out how to do it in Matlab, and need something simular in opencv/c++.
Matlab Raw Values:
Matlab Smoothen with M0 = smooth3(M0,'gaussian'); :
Gaussian filters are separable. You apply 1D filter at each dimension as follows:
for (dim = 0; dim < D; dim++)
tensor = gaussian_filter(tensor, dim);
I would recommend OpenCV for an implementation of a gaussian filter (and image processing in general) in C++.
Note that this assumes that your pyramid levels are all of the same size.
You can have your own functions that sample your scale-space pyramid on the fly while convolving the third dimension, but if you have enough memory I believe that it would be faster to scale up your coarser level to have the same size of the finest level.
Long ago (in 2008-2009) I have developed a small C++ template lib to apply some simple transformations and convolution filters. The library's source can be found in the Linderdaum Engine - it has nothing to do with the rest of the engine and does not use any of the engine's features. The license is MIT, so do whatever you want with it.
Take a look into the Linderdaum's source code (http://www.linderdaum.com) at Src/Linderdaum/Images/VolumeLib.*
The function to prepare the kernel is PrepareGaussianFilter() and MakeScalarVolumeConvolution() applies the filter. It is easy to adapt the library for the different data sources because the I/O is implemented using callback functions.

Resources