What Will I Learn?
- You will learn Python
- You will learn OpenCV
- You will learn Digital Imaging
Requirements
- PYCharm
- OpenCV Package
- NumPY Package
Difficulty
- Intermediate
Tutorial Contents
What Is Blur Effect?
The blur effect is an effect that results in a blurred image, out of focus, blur, and things like that.
But with OpenCV, we can recognize the code structure to make the image blur, and there are several techniques to blur an image that we will discuss in this part include:
- Averaging
- Gaussian Blur
- Median Blur
- Edge Detection
Averaging
The first technique is called averaging because the process of blurring the image is done by taking the average of the entire image and changing it to a new value.
To get a blur effect with this technique, we can use the function blur()
Example Program:
Create new Python file on your PYCharm with name averaging.
Copy an image to folder project.
Write the source code below.
import numpy as np
import cv2 as cv
img = cv.imread('test.jpg',1)
blur = cv.blur(img, (10,10))
cv.imshow('Old Image', img)
cv.imshow('Image After Blur', blur)
cv.waitKey(0)
cv.destroyAllWindows()
4 . Run it, and see output.
Explanation Code:
blur = cv.blur(img, (10,10))
In the above code, two arguments are (10,10) where the intent of 2 argument that is to regulate how high rate of bleeding to be obtained. The high value will result in the higher blur effect.
Gaussian Blur
Another blur effect that can be used is Gaussian Blur which in OpenCV can be present through the GaussianBlur () function. The most basic Gaussian Blur structure is as follows:
cv.GaussianBlur (src (sigmaX, sigmaY), border)
- src = image to be blurred.
- sigmaX = sets the number of standard deviations based on the gaussian blur filter on the X-axis.
- sigmaY = sets the number of standard deviations based on the Gaussian blur filter on the Y-axis.
- border = set the resulting border due to using this filter. The default is in condition 0.
Example Program:
Create new Python file on your PYCharm with name gaussian-blur.
Copy an image to folder project.
Write the source code below.
import numpy as np
import cv2 as cv
img = cv.imread('test.jpg',1)
blur = cv.GaussianBlur(img, (5,5),0)
cv.imshow('Old Image', img)
cv.imshow('Image After Gaussian Blur', blur)
cv.waitKey(0)
cv.destroyAllWindows()
4 . Run it, and see output.
Explanation Code:
blur = cv.GaussianBlur(img, (5,5),0)
the same explanation as above just this function add border function which set default 0
Median Blur
Median Blur can be used to minimize noise effects on the image. This blur technique can be used using the medianBlur () function.
Example Program:
Create new Python file on your PYCharm with name median-blur.
Copy an image to folder project. Attention! use noise-effect images for this technique.
Write the source code below.
import numpy as np
import cv2 as cv
img = cv.imread('noise.jpg',1)
blur = cv.medianBlur(img,5)
cv.imshow('Old Image', img)
cv.imshow('Image After Gaussian Blur', blur)
cv.waitKey(0)
cv.destroyAllWindows()
4 . Run it, and see output.
Explanation Code:
blur = cv.medianBlur(img,5)
Just use the two arguments src as img and its value is 5 to set the strength of the resulting blur.
Edge Detection
Edge Detection is an algorithm that serves to detect the object line. One of the functions that can be used for Edge Detection is Canny ().
The structure for this Edge Detection function itself as follows:
cv.Canny (src, minVal, maxVal)
- src = image file that will be used for Edge Detection function.
- minVal = the value that governs the intensity of the gradient on the object.
- maxVal = value that regulates the intensity of the gradient on the object.
If you enter a low number in minVal or maxVal, the edge that will be in shape is much more detailed in than with the high inserted value.
Example Program:
Create new Python file on your PYCharm with name edge-detection.
Copy an image to folder project. Attention! use noise-effect images for this technique.
Write the source code below.
import numpy as np
import cv2 as cv
img = cv.imread('test.jpg',1)
blur = cv.Canny(img,50,60)
cv.imshow('Old Image', img)
cv.imshow('Image After Edge Detection', blur)
cv.waitKey(0)
cv.destroyAllWindows()
4 . Run it, and see output.
Explanation Code:
blur = cv.Canny(img,50,60)
Value 50 as minVal.
Value 60 as maxVal.
If the value in fox becomes smaller, then the edge will be much more detail.
Okay until here first learning about Python Digital Imaging with OpenCV [Blur Effect - Part 2]. For the third part will discuss how to draw with python language using OpenCV package.
Curriculum
- Learn Python Digital Imaging with OpenCV [Resize - Part 1]
- How To install and Open Image with OpenCV Package
Posted on Utopian.io - Rewarding Open Source Contributors
This is cool - looks like a better option for me than Imagemagick :)
Your contribution cannot be approved because it does not follow the Utopian Rules.
You can contact us on Discord.
[utopian-moderator]