Guest User

Untitled

a guest
Apr 26th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3.  
  4. font = cv2.FONT_HERSHEY_SIMPLEX
  5. lineType = cv2.LINE_AA
  6.  
  7. im = cv2.imread('Photos/test.jpg')
  8. im_ycrcb = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
  9.  
  10. ball_ycrcb_mint = np.array([0, 90, 100],np.uint8)
  11. ball_ycrcb_maxt = np.array([25, 255, 255],np.uint8)
  12. ball_ycrcb = cv2.inRange(im_ycrcb, ball_ycrcb_mint, ball_ycrcb_maxt)
  13. #cv2.imwrite('Photos/output2.jpg', ball_ycrcb) # Second image
  14. areaArray = []
  15. count = 1
  16.  
  17. _, contours, _ = cv2.findContours(ball_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  18. for i, c in enumerate(contours):
  19. area = cv2.contourArea(c)
  20. areaArray.append(area)
  21. areaLargest = np.argmax(areaArray)
  22. areaLargestMax = max(areaArray)
  23. areaLargestCnt = contours[areaLargest]
  24. x, y, w, h = cv2.boundingRect(areaLargestCnt)
  25. if area == areaLargestMax and area > 10000:
  26. cv2.drawContours(im, contours, i, (255, 0, 0), 2)
  27. cv2.rectangle(im, (x, y), (x+w, y+h), (0,255,0), 2)
  28. cv2.imwrite('Photos/output3.jpg', im)
  29.  
  30. import numpy as np
  31. import cv2
  32. im = cv2.imread('Photos/test.jpg')
  33. im_ycrcb = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
  34.  
  35. ball_ycrcb_mint = np.array([0, 90, 100],np.uint8)
  36. ball_ycrcb_maxt = np.array([25, 255, 255],np.uint8)
  37. ball_ycrcb = cv2.inRange(im_ycrcb, ball_ycrcb_mint, ball_ycrcb_maxt)
  38. #cv2.imwrite('Photos/output2.jpg', ball_ycrcb) # Second image
  39. areaArray = []
  40. count = 1
  41.  
  42. contours, _ = cv2.findContours(ball_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  43. for i, c in enumerate(contours):
  44. area = cv2.contourArea(c)
  45. areaArray.append(area)
  46.  
  47. #first sort the array by area
  48. sorteddata = sorted(zip(areaArray, contours), key=lambda x: x[0], reverse=True)
  49.  
  50. #find the nth largest contour [n-1][1], in this case 2
  51. secondlargestcontour = sorteddata[1][1]
  52.  
  53. #draw it
  54. x, y, w, h = cv2.boundingRect(secondlargestcontour)
  55. cv2.drawContours(im, secondlargestcontour, -1, (255, 0, 0), 2)
  56. cv2.rectangle(im, (x, y), (x+w, y+h), (0,255,0), 2)
  57. cv2.imwrite('Photos/output3.jpg', im)
Add Comment
Please, Sign In to add comment