Lecture3-2 in Computer Vision
Lecture3-2 in Computer Vision
Computer Vision
MATCH
Lecture-Three
Morphological
Operations
Thresholding
• Thresholding basic functions
• Sobel Filter
image = cv.imread("sudoko.jpg",0)
cv.imshow("Image",image)
cv.waitKey(0)
sobel_x_filtered_image = cv.Sobel(image,cv.CV_64F, 1, 0, ksize=3)
sobel_y_filtered_image = cv.Sobel(image,cv.CV_64F, 0, 1, ksize=3) Vertical Edges
sobel_x_filtered_image =cv.convertScaleAbs(sobel_x_filtered_image)
sobel_y_filtered_image =cv.convertScaleAbs(sobel_y_filtered_image)
cv.imshow("sobel_x_filtered_image",sobel_x_filtered_image);
cv.imshow("sobel_y_filtered_image",sobel_y_filtered_image);
cv.waitKey(0)
Horizontal Edges
Filtering in Spatial Domain
• The Results
• Canny Filter
img= cv.imread('home.jpg')
image_gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
filtered_image =
cv.Canny(image_gray,threshold1=100, threshold2=200)
cv.imshow('image_original',img);
cv.imshow('filtered_image',filtered_image);
cv.waitKey(0)
Image thresholding is a simple, yet effective, way of
partitioning an image into a foreground and
background
Thresholding
Thresholding
img = cv.imread('numbers.jpg', 0)
ret, thresh1 = cv.threshold(img, 127,255,
cv.THRESH_BINARY)
ret, thresh2 = cv.threshold(img, 127,255,
cv.THRESH_BINARY_INV)
ret, thresh3 = cv.threshold(img, 127,255,
cv.THRESH_TRUNC)
ret, thresh4 = cv.threshold(img, 127,255,
cv.THRESH_TOZERO)
ret, thresh5 = cv.threshold(img, 127,255,
cv.THRESH_TOZERO_INV)
Morphological transformations are some simple
operations based on the image shape.
It is normally performed on binary images.
Morphological
Operations
Erosion
Original
img = cv.imread('circles.png', 0)
kernel = np.ones((5,5),np.uint8)
#Erosion
erosion = cv.erode(img,kernel,iterations= 1)
cv.imshow("erosion",erosion)
cv.waitKey(0)
Iteration = 1
img = cv.imread('circles.png', 0)
kernel = np.ones((5,5),np.uint8)
#Dilation
dilation = cv.dilate(img,kernel,iterations= 1)
cv.imshow(“dilation", dilation)
cv.waitKey(0)
Iteration = 1
img = cv.imread('circles.png', 0)
kernel = np.ones((5,5),np.uint8)
#Opening
Opening=cv.morphologyEx(img,cv.MORPH_OPEN,kernel) Opening
cv.imshow("opening",opening)
cv.waitKey(0)
Closing
Original
Closing = Dilation + Erosion
It is useful in closing small holes inside the
foreground objects, or small black points on the
object.
img = cv.imread(j.png', 0)
kernel = np.ones((5,5),np.uint8)
#Closing Closing
Closing=cv.morphologyEx(img,cv.MORPH_CLOSE,kernel)
cv.imshow(“closing",Closing)
cv.waitKey(0)
Morphological Gradient
Original
It is the difference between dilation and erosion
of an image.
Gradient = Dilation - Erosion
The result will look like the outline of the object.
img = cv.imread('j.png', 0)
kernel = np.ones((5,5),np.uint8)
#Gradient Gradient
Gradient=cv.morphologyEx(img,cv.MORPH_GRADIENT,kernel)
cv.imshow(“Gradient ", Gradient)
cv.waitKey(0)
Homework