Advanced Image Processing Using Opencv
Advanced Image Processing Using Opencv
Advanced Image
Processing
Using OpenCV
Now that we have looked at the basic image processing techniques using
the Scikit Image library, we can move on to its more advanced aspects.
In this chapter, we use one of the most comprehensive computer vision
libraries: OpenCV and examine the following concepts:
• Smoothing images
#Read image 1
img1 = cv2.imread('cat_1.jpg')
#Read image 2
img2 = cv2.imread('cat_2.jpg')
#Blend images
final_image = cv2.addWeighted(img1, alpha, img2, beta, 0.0)
#Show image
io.imshow(final_image)
64
Chapter 4 Advanced Image Processing Using OpenCV
65
Chapter 4 Advanced Image Processing Using OpenCV
Let’s look at the code and the output, to see the difference between
contrast and brightness.
#Read image
image = cv2.imread("cat_1.jpg")
66
Chapter 4 Advanced Image Processing Using OpenCV
figure(0)
io.imshow(image)
figure(1)
io.imshow(new_image)
In this code, we did not use any cv2 functions to change the brightness
or contrast. We used the numpy library and a slicing concept to change
the parameters. The first thing we did was define the parameters. We gave
contrast a value of 3 and brightness a value of 2. The first for loop gave
the image width, the second gave the image height, and the third gave the
image channels. Therefore, the first loop runs width a number of times,
the second loop runs height a number of times, and the last loop runs the
number of color channels a number of times. If the RGB image is there,
then loop runs three times for the three channels.
np.clip() limits the values in a particular range. In the previous
code, the range is 0 to 255, which is nothing but the pixel values for each
channel. So, a formula is derived:
Using the this formula, we can change each and every pixel value,
and np.clip() makes sure the output value doesn’t go beyond 0 to 255.
Hence, the loops traverse through each and every pixel, for each and every
channel, and does the transformation.
67
Chapter 4 Advanced Image Processing Using OpenCV
68
Chapter 4 Advanced Image Processing Using OpenCV
• Font type
• Font scale
• Thickness of text
As you can see in the code that follows, the font used is FONT_HERSHEY_
SIMPLEX. cv2 also supports following fonts:
• FONT_HERSHEY_SIMPLEX
• FONT_HERSHEY_PLAIN
• FONT_HERSHEY_DUPLEX
• FONT_HERSHEY_COMPLEX
• FONT_HERSHEY_TRIPLEX
• FONT_HERSHEY_COMPLEX_SMALL
• FONT_HERSHEY_SCRIPT_SIMPLEX
• FONT_HERSHEY_SCRIPT_COMPLEX
• FONT_ITALIC
The type of line that used in the code is cv2.LINE_AA. Other types of
lines that are supported are
69
Chapter 4 Advanced Image Processing Using OpenCV
You can experiment using all the different arguments and check the
results. Let’s look at the code and its output.
#Read image
image = cv2.imread("cat_1.jpg")
#Define font
font = cv2.FONT_HERSHEY_SIMPLEX
io.imshow(image)
Output:
70
Chapter 4 Advanced Image Processing Using OpenCV
Smoothing Images
In this section we take a look at three filters used to smooth images. These
filters are as follows:
Median Filter
The median filter is one of the most basic image-smoothing filters. It’s a
nonlinear filter that removes black-and-white noise present in an image by
finding the median using neighboring pixels.
To smooth an image using the median filter, we look at the first
3 × 3 matrix, find the median of that matrix, then remove the central value
by that median. Next, we move one step to the right and repeat this process
until all the pixels have been covered. The final image is a smoothed
image. If you want to preserve the edges of your image while blurring, the
median filter is your best option.
cv2.medianBlur is the function used to achieve median blur. It has two
parameters:
Gaussian Filter
The gaussian filter depends on the standard deviation of the image
(distribution) and assumes the mean is zero (we can define a mean
different from zero as well). Gaussian filters do not take care of the edges.
71
Chapter 4 Advanced Image Processing Using OpenCV
Bilateral Filter
If we want to smooth an image and keep the edges intact, we use a bilateral
filter. Its implementation is simple: We replace the pixel value with the
average of its neighbors. This is a nonlinear smoothing approach that takes
the weighted average of neighboring pixels. “Neighbors” are defined in
following ways:
72
Chapter 4 Advanced Image Processing Using OpenCV
3. The sigma value for color (to find the pixels that are
similar)
4. The sigma value for space (to find the pixels that are
closer)
#Blur images
image_MedianBlur=cv2.medianBlur(image_MedianBlur,9)
image_GaussianBlur=cv2.GaussianBlur(image_GaussianBlur,(9,9),10)
image_BilateralBlur=cv2.bilateralFilter(image_BilateralBlur,9,
100,75)
#Show images
figure(0)
io.imshow(image_Original)
figure(1)
io.imshow(image_MedianBlur)
figure(2)
73
Chapter 4 Advanced Image Processing Using OpenCV
io.imshow(image_GaussianBlur)
figure(3)
io.imshow(image_BilateralBlur)
Output:
74
Chapter 4 Advanced Image Processing Using OpenCV
75
Chapter 4 Advanced Image Processing Using OpenCV
240 21 98 21 21 21
• Erosion/dilation type
• Kernel size
76
Chapter 4 Advanced Image Processing Using OpenCV
#DILATION CODE:
#Import package
import cv2
#Read image
image = cv2.imread("cat_1.jpg")
77
Chapter 4 Advanced Image Processing Using OpenCV
figure(1)
io.imshow(final2)
figure(2)
io.imshow(final3)
#EROSION CODE:
#Import packages
import cv2
#Read images
image = cv2.imread("cat_1.jpg")
78
Chapter 4 Advanced Image Processing Using OpenCV
figure(1)
io.imshow(final2)
figure(2)
io.imshow(final3)
Output:
79
Chapter 4 Advanced Image Processing Using OpenCV
80
Chapter 4 Advanced Image Processing Using OpenCV
#Import packages
import cv2
#Read image
image = cv2.imread("cat_1.jpg")
81
Chapter 4 Advanced Image Processing Using OpenCV
2 - Truncated
3 - Threshold To Zero
4 - Threshold To Zero Inverted
"'
82
Chapter 4 Advanced Image Processing Using OpenCV
Output:
83
Chapter 4 Advanced Image Processing Using OpenCV
Calculating Gradients
In this section we look at edge detection using Sobel derivatives. Edges are
found in two directions: the vertical direction and the horizontal direction.
With this algorithm, we emphasize only those regions that have very high
spatial frequency, which may correspond to edges. Spatial frequency is the
level of detail present in an area of importance.
In the following code, we read the image, apply gaussian blur so the
noise is removed, then convert the image to grayscale. We use the
cv2.cvtColor() function to convert the image to grayscale. We can also
use skimage functions to do the same. Last, we give the grayscale output to
the cv2.Sobel() function. Let’s look at Sobel Function’s parameters:
• Input image
84
Chapter 4 Advanced Image Processing Using OpenCV
• cv2.CV_16S
• cv2.CV_32F
• cv2.CV_64F
#Import packages
import cv2
#Read image
src = cv2.imread("cat_1.jpg")
85
Chapter 4 Advanced Image Processing Using OpenCV
Output:
86
Chapter 4 Advanced Image Processing Using OpenCV
#Import packages
import cv2
#Read image
src = cv2.imread("cat_1.jpg")
#Convert to grayscale
src = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
87
Chapter 4 Advanced Image Processing Using OpenCV
Output:
88