Eng-Mohamed-Essam

Untitled

Sep 15th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. Name = "test1.png" #Image Name
  4. Img = cv2.imread(Name, cv2.IMREAD_GRAYSCALE) #Reading the Image in gray scale
  5. Clear_Image = cv2.imread(Name, 1)
  6. _, threshold = cv2.threshold (Img, 200, 255, cv2.THRESH_BINARY) # making threshold for the contours
  7. contours, hierarcy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  8. font = cv2.FONT_HERSHEY_TRIPLEX # Font used in typing
  9. #Blue Color
  10. Low = np.array([100, 50, 50])
  11. High = np.array([140, 255, 255])
  12. #Green Color
  13. Low = np.array([40, 50, 50])
  14. High = np.array([80, 255, 255])
  15. #Red Color
  16. Low = np.array([140, 150, 0])
  17. High = np.array([180, 255, 255])
  18. #Yellow Color
  19. Low = np.array([20, 190, 20])
  20. High = np.array([30, 255, 255])
  21. for cnt in contours :
  22.     Area = cv2.contourArea(cnt) # Contour's Area
  23.     approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True) # approximation for the contours in the Image
  24.     cv2.drawContours(Img, [approx], 0, (0), 2) # Drawing a line at the Circumfrence of the Contour
  25.     x = approx.ravel()[0]
  26.     y = approx.ravel()[1]
  27.     if len(approx) == 3:
  28.         cv2.putText(Img, "Triangle", (x, y), font, 1, (0)) #putting this text on the pic if we find the object
  29.         print ("Triangle")
  30.     elif len(approx) == 4:
  31.         cv2.putText(Img, "Square", (x, y), font, 1, (0))
  32.         print("Square")
  33.     elif Area < 100 :
  34.         cv2.putText(Img, "Line", (x, y), font, 1, (0))
  35.         print("Line")
  36.     else:
  37.         cv2.putText(Img, "Circle", (x, y), font, 1, (0))
  38.         print("Circle")
  39.  
  40. print(Img) # Printing the Matrix of pixels
  41. cv2.imshow(Name, Img) #Sowing the Image to the user
  42. cv2.waitKey(0) # Delaying for 5 seconds
  43. cv2.destroyAllWindows() # Destroying the Image after the Given delay
Advertisement
Add Comment
Please, Sign In to add comment