You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
675 B

  1. import cv2
  2. #reading the image
  3. #image = cv2.imread("demo.jpeg")
  4. def detectObjects(image):
  5. edged = cv2.Canny(image, 10, 250)
  6. cv2.imshow("Edges", edged)
  7. cv2.waitKey(0)
  8. #applying closing function
  9. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
  10. closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
  11. cv2.imshow("Closed", closed)
  12. cv2.waitKey(0)
  13. #finding_contours
  14. (_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  15. for c in cnts:
  16. peri = cv2.arcLength(c, True)
  17. approx = cv2.approxPolyDP(c, 0.02 * peri, True)
  18. cv2.drawContours(image, [approx], -1, (0, 255, 0), 2)
  19. cv2.imshow("Output", image)
  20. cv2.waitKey(0)