SHARE
TWEET

Untitled

a guest Mar 20th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import numpy as np
  3.  
  4. # If you're wondering why only two dimensions, well this is a grayscale image,
  5. # if we doing a colored image, we'd use
  6. # rectangle = np.zeros((300, 300, 3),np.uint8)
  7.  
  8. # Making a sqare
  9. square = np.zeros((300, 300), np.uint8)
  10. cv2.rectangle(square, (50, 50), (250, 250), 255, -2)
  11. cv2.imshow("Square", square)
  12. cv2.waitKey(0)
  13.  
  14. # Making a ellipse
  15. ellipse = np.zeros((300, 300), np.uint8)
  16. cv2.ellipse(ellipse, (150, 150), (150, 150), 30, 0, 180, 255, -1)
  17. cv2.imshow("Ellipse", ellipse)
  18. cv2.waitKey(0)
  19.  
  20. cv2.destroyAllWindows()
  21.  
  22.  
  23. # Shows only where they intersect
  24. And = cv2.bitwise_and(square, ellipse)
  25. cv2.imshow("AND", And)
  26. cv2.waitKey(0)
  27.  
  28. # Shows where either square or ellipse is
  29. bitwiseOr = cv2.bitwise_or(square, ellipse)
  30. cv2.imshow("OR", bitwiseOr)
  31. cv2.waitKey(0)
  32.  
  33. # Shows where either exist by itself
  34. bitwiseXor = cv2.bitwise_xor(square, ellipse)
  35. cv2.imshow("XOR", bitwiseXor)
  36. cv2.waitKey(0)
  37.  
  38. # Shows everything that isn't part of the square
  39. bitwiseNot_sq = cv2.bitwise_not(square)
  40. cv2.imshow("NOT - square", bitwiseNot_sq)
  41. cv2.waitKey(0)
  42.  
  43. ### Notice the last operation inverts the image totally
  44.  
  45. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top