opencvImage Processing


  1. Gaussian Blur Syntax C++: void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT )


Parameters of Gaussian BlurDetails
srcInput image, the image can have any number of channels, which are processed independently, but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
dstOutput image of the same size and type as src
ksizeGaussian kernel size. ksize.width and ksize.height can differ but they both must be positive and odd. Or, they can be zero’s and then they are computed from sigma* .
sigmaXGaussian kernel standard deviation in X direction.
sigmaYGaussian kernel standard deviation in Y direction. if sigmaY is zero, it is set to be equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width and ksize.height. To fully control the result regardless of possible future modifications of all this semantics, it is recommended to specify all of ksize, sigmaX, and sigmaY.
borderTypePixel extrapolation method.


I don't think it makes sense to put syntax and parameters specific to gaussian blur in this place as the topic is so broad that it should include many other examples

Smoothing Images with Gaussian Blur in C++

Smoothing, also known as blurring, is one of the most commonly used operation in Image Processing.

The most common use of the smoothing operation is to reduce noise in the image for further processing.

There are many algorithms to perform smoothing operation.

We'll look at one of the most commonly used filter for blurring an image, the Gaussian Filter using the OpenCV library function GaussianBlur(). This filter is designed specifically for removing high-frequency noise from images.

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv){

    Mat image , blurredImage;

    // Load the image file
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);

    // Report error if image could not be loaded
        cout<<"Error loading image" << "\n";
        return -1;

    // Apply the Gaussian Blur filter. 
    // The Size object determines the size of the filter (the "range" of the blur)
    GaussianBlur( image, blurredImage, Size( 9, 9 ), 1.0);

    // Show the blurred image in a named window
    imshow("Blurred Image" , blurredImage);

    // Wait indefinitely untill the user presses a key

    return 0;

For the detailed mathematical definition and other types of filters you can check the original documentation.


In Python:

before threshold

import cv2
image_path= 'd:/contour.png'
img = cv2.imread(image_path)

#display image before thresholding
cv2.imshow('I am an image display window',img)

#convert image to gray scale - needed for thresholding
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#apply threshold to gray image to obtain binary image

threshold=150 #value above which pixel values will be set to max_value
max_value=255  #value to which pixels above threshold will be set
threshold_stype=cv2.THRESH_BINARY #default threshold method

ret, img_binary = cv2.threshold(img_gray, threshold, max_value, threshold_stype)

#display image after thresholding
cv2.imshow('image after applying threshold',img_binary)

#save the binary image

after threshold

Bilateral Filtering

In image processing applications, the bilateral filters are a special type of non-linear filters.

There is a trade off between loosing structure and noise removal, because the most popular method to remove noise is Gaussian blurring which is not aware of structure of image; therefore, it also removes the edges. Most of the time, edges contain valuable information about the scene and we don't want to loose it. The bilateral filter is aware of structure of the scene and it tends to act like a classical blurring filter when it is on a area without edges; however, when it sees an edge, it changes its behavior; so that, blurring does not work across the edges, but it works along the edges meaning that they are edge-preserving filters.

#include <opencv2/opencv.hpp>
#include <iostream>

void main(int argc, char* argv[]) {
    if(argc==1) {
        std::cout << argv[0] << " <image>" << endl;

    cv::Mat image, output;
    image = cv::imread(argv[1]);
    if(image.empty()) {
        std::cout << "Unable to load the image: " << argv[1] << endl;

    cv::bilateralFilter(image, output, 3, 5, 3);