Professional Documents
Culture Documents
Dip Day 2 - New
Dip Day 2 - New
WORKSHOP
DAY 2
Topics to be covered
• Image Thresholding
• Image Enhancement techniques
-> Filtering
1. Smoothing
2. Sharpening
• Edge detection
IMAGE THRESHOLDING
Thresholding
• Image thresholding is a simple, yet effective, way of partitioning an
image into a foreground and background.
• Simple Thresholding:
– If pixel value is greater than a threshold value, it is assigned one value (may be
white), else it is assigned another value (may be black).
– The function used is cv.threshold.
– First argument is the source image, which should be a grayscale image.
– Second argument is the threshold value which is used to classify the pixel
values.
– Third argument is the maxVal which represents the value to be given if pixel
value is more than (sometimes less than) the threshold value.
– Different types are:
• cv.THRESH_BINARY
• cv.THRESH_BINARY_INV
• cv.THRESH_TRUNC
• cv.THRESH_TOZERO
• cv.THRESH_TOZERO_INV
Thresholding
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('gradient.png',0)
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
plt.show()
Thresholding
• Adaptive Thresholding:
– In simple thresholding, we used a global value as threshold value. But
image might have different lighting conditions in different areas.
– In that case, we go for adaptive thresholding. We calculate the threshold
for a small regions of the image. So we get different thresholds for
different regions of the same image.
– It has three ‘special’ input parameters and only one output argument:
• Adaptive Method - It decides how thresholding value is calculated.
– cv.ADAPTIVE_THRESH_MEAN_C : threshold value is the mean of neighbourhood area.
– cv.ADAPTIVE_THRESH_GAUSSIAN_C : threshold value is the weighted sum of
neighbourhood values where weights are a gaussian window.
• Block Size - It decides the size of neighbourhood area.
• C - It is just a constant which is subtracted from the mean or weighted mean
calculated.
Thresholding
Value of the resultant operation is assigned to the upper left corner pixel so if we are just
considering a weighted sum between kernel and input image then kernel operation applied on
the I57 pixel of the input image will look like the one given below:
• Filters could be either smoothing, or sharpening.
• Smoothing filters are used in blurring.
• Sharpening filters are used in edge detection.
INTRODUCTION TO SMOOTHING
• Smoothing, also called blurring, is a simple and frequently used
image processing operation. It is usually done to reduce noise or
other fine-scale structures/rapid phenomena.
• Image blurring is achieved by convolving the image with a low-pass
filter kernel. It is useful for removing noise. It actually removes high
frequency content (e.g: noise, edges) from the image resulting in
edges being blurred when this is filter is applied. (Well, there are
blurring techniques which do not blur edges).
Spatial Domain Filtering
• Spatial domain filtering means applying some operation on the
neighbourhood pixels and assigning the result to the pixel
whose neighbourhood was considered.
• Some neighborhood operations work with the values of the
image pixels in the neighborhood and the corresponding
values of a sub image that has the same dimensions as the
neighborhood.
• The sub image is called a filter, mask, kernel, template, or
window, with the first three terms being the most prevalent
terminology.
• The sub image or filter is just a 2D matrix which is hovered
over the whole image pixel by pixel, an operation between
pixel values and matrix values occurs and result is assigned to
the pixel at the centre of that matrix (generally).
• The values in a filter sub-image are referred to as coefficients,
rather than pixels.
Linear Spatial Filtering
• Linear filtering is filtering in which the value of an output pixel
is a linear combination of the values of the pixels in the input
pixel's neighborhood. As in the example of convolution.
• That is, the spatial operation performed by the filter is a linear
combination of the pixel’s neighbourhood.
Frequency components in an Image
• “Frequency” means the rate of change of intensity per pixel.
Let’s say you have some region in your image that changes
from white to black. If it takes many pixels to undergo that
change, it’s low frequency. The fewer the pixels it takes to
represent that intensity variation, the higher the frequency.
cv2.destroyAllWindows()
cap.release ()
Median blur applied on Images
kernel_sharpening = np.array([[-1,-1,-1],
[-1, 9,-1],
[-1,-1,-1]])
# applying the sharpening kernel to the input image & displaying it.
• Looking for these peaks is exactly what gradient based edge detection methods
do. You’re not interested in what the actual colors are.
Relating Edges and Derivatives
• There has to be a certain threshold above which an edge is
classified as a peak else it must be considered part of noise.
• On the left (where the curve is rising), the slope
is positive. On the right, the slope is negative.
• So there must exist a point where there is a
zero crossing. That point is the edge's location.
Edge detectors that are based on this idea
are called Laplacian edge detectors.
• Now, all of this is for 1-D images and holds for 2-D images as well.
Another thing is - these are based on continuous images. But in
reality, that is never the case. So we'll have to approximate these
derivatives based on the pixelated data that we do have. This is
done with the help of convolutions.
• When taking the derivative of an image, you’re actually taking
what’s called a discrete derivative, and it’s more of an
approximation of the derivative. One simple example is that you
can take the derivative in the x-direction at pixel x1 by taking the
difference between the pixel values to the left and right of your
pixel (x0 and x2).
• It’s easiest to see how the image derivative is useful in locating
edges. The derivative of a function tells you the rate of change, and
an edge constitutes a high rate of change.
• The derivative of an image has two dimensions. We can take the
derivative in the x direction and in the y direction, and together
these make up the “gradient vector”:
• Going back to edge detection, the gradient direction gives you the normal to the
edge (it’s perpendicular to the edge), and the gradient magnitude gives you the
strength of the edge.
• To compute the derivative with respect to x at a given pixel, it sounds like in
practice, to reduce the affects of noise, we:
-> Use the “central difference”
-> Average the derivative of the pixel with that of the row above and row below:
• Apply the mask
• Again, we could just use the middle row for fx and the middle
column for fy, but we include the surrounding pixels to help reduce
the affect of noise.
• We can’t apply the mask to the border pixels so we don’t, the
derivative at those pixels is set to 0.
• Y Direction:
Results of sobel edge detection on (a)original image: (b)vertical edges, (c)horizontal edges
Sobel Edge Detector
import cv2
import numpy as np
cv2.imshow(‘Original’, img )
cv2.imshow(‘Sobel horizontal’, sobel_horizontal )
cv2.imshow(‘Sobel vertical’, sobel_vertical )
cv2.waitKey (0)
Sobel and laplacian filter overview
CANNY EDGE DETECTION
Canny Edge Detector
• Overview
– The canny edge detector is a multistage edge detection algorithm.
The steps are:
• Preprocessing
• Calculating gradients
• Nonmaximum suppression
– Thresholding with hysterysis
– The two key parameters of the algorithm are - an upper threshold
and a lower threshold. The upper threshold is used to mark edges
that are definitely edges. The lower threshold is to find faint pixels
that are actually a part of an edge.
• Preprocessing
– Here, Gx and Gy are the X and Y derivatives at the point being considered.
– Once we have the gradient magnitudes and orientations, we can get
started with the actual edge detection.
• Non-maximum suppression
– If a pixel is not a maximum, it is suppressed.
– To do this, you iterate over all pixels.
– Let's say you're at the grey pixel. There are only four possible edges
possible - it could either go from north to south (the green
neighbours), from east to west (the blue ones) or one of the
diagonals (the yellow or red neighbours).
– So using the current pixel's gradient direction, you try and estimate
where the edge is going.
• Thresholding with Hysteresis
– This stage decides which are all edges are really edges and which are not. For this,
we need two threshold values, minVal and maxVal.
– Any edges with intensity gradient more than maxVal are sure to be edges and those
below minVal are sure to be non-edges, so discarded.
– Those who lie between these two thresholds are classified edges or non-edges based
on their connectivity. If they are connected to “sure-edge” pixels, they are
considered to be part of edges. Otherwise, they are also discarded.