How to do it...

  1. Import the Computer Vision package - cv2:
import cv2 
# Import Numerical Python package - numpy as np 
import numpy as np 
  1. Read the image using the built-in imread function:
image = cv2.imread('image_5.jpg') 
  1. Display the original image using the built-in imshow function:
cv2.imshow("Original", image) 
  1. Wait until any key is pressed:
cv2.waitKey(0) 
  1. Execute the Canny edge detection system:
# cv2.Canny is the built-in function used to detect edges 
# cv2.Canny(image, threshold_1, threshold_2) 
canny = cv2.Canny(image, 50, 200) 
  1. Display the edge detected output image using the built-in imshow function:
cv2.imshow("Canny Edge Detection", canny) 
  1. Wait until any key is pressed:
cv2.waitKey(0)
  1. Execute the contour detection system:
# cv2.findContours is the built-in function to find contours 
# cv2.findContours(canny, contour retrieval mode, contour approximation mode) 
# contour retrieval mode: cv2.RETR_LIST (retrieves all contours)  
# contour approximation mode: cv2.CHAIN_APPROX_NONE (stores all boundary points) 
contours, hierarchy = cv2.findContours(canny, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) 
  1. Sketch the contour on the image:
# cv2.drawContours is the built-in function to draw contours 
# cv2.drawContours(image, contours, index of contours, color, thickness) 
cv2.drawContours(image, contours, -1, (255,0,0), 10) 
# index of contours = -1 will draw all the contours 
  1. Show the sketched contour of the image:
# Display contours using imshow built-in function 
cv2.imshow("Contours", image) 
  1. Wait until any key is pressed:
cv2.waitKey() 
  1. Terminate the program and display the result:
# Close all windows 
cv2.destroyAllWindows() 

 

  1. The result obtained after executing the Image_Segmentation.py file is shown here:

Following is the edge detection output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.210