上QQ阅读APP看书,第一时间看更新
How to do it...
- Import the Computer Vision package - cv2:
import cv2 # Import Numerical Python package - numpy as np import numpy as np
- Read the image using the built-in imread function:
image = cv2.imread('image_5.jpg')
- Display the original image using the built-in imshow function:
cv2.imshow("Original", image)
- Wait until any key is pressed:
cv2.waitKey(0)
- 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)
- Display the edge detected output image using the built-in imshow function:
cv2.imshow("Canny Edge Detection", canny)
- Wait until any key is pressed:
cv2.waitKey(0)
- 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)
- 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
- Show the sketched contour of the image:
# Display contours using imshow built-in function cv2.imshow("Contours", image)
- Wait until any key is pressed:
cv2.waitKey()
- Terminate the program and display the result:
# Close all windows cv2.destroyAllWindows()
- The result obtained after executing the Image_Segmentation.py file is shown here:
Following is the edge detection output: