Advertisement
jarod_reynolds

contours

May 13th, 2021
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import cv2
  2.  
  3. def find_curved_contour(img):
  4. # Convert image to hsv colour space
  5. img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  6. # cv2.imshow("hsv", img_hsv)
  7. # cv2.waitKey(1000)
  8.  
  9. # Apply threshold to filter out the red background
  10. thresh_min = (100, 0.000, 0.000)
  11. thresh_max = (109, 255, 255)
  12. thresh_img = cv2.inRange(img_hsv, thresh_min, thresh_max)
  13. cv2.imshow("thresh", thresh_img)
  14. cv2.waitKey(1000)
  15.  
  16. # Find contours
  17. _, contours, hierarchy = cv2.findContours(thresh_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
  18.  
  19. # Display the contour with the biggest area
  20. if contours is not None and len(contours) > 0:
  21. contour = max(contours, key=cv2.contourArea)
  22. cv2.drawContours(img, contour, -1, (0,255,0), 2)
  23.  
  24. cv2.imshow("contour", img)
  25. cv2.waitKey(10000)
  26.  
  27.  
  28. if __name__ == '__main__':
  29. img_path = "/home/jarod/Pictures/curved_image.png"
  30. img = cv2.imread(img_path)
  31. cv2.imshow("raw", img)
  32. cv2.waitKey(1000)
  33.  
  34. # Find contours
  35. find_curved_contour(img)
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement