Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- Name = "test1.png" #Image Name
- Img = cv2.imread(Name, cv2.IMREAD_GRAYSCALE) #Reading the Image in gray scale
- Clear_Image = cv2.imread(Name, 1)
- _, threshold = cv2.threshold (Img, 200, 255, cv2.THRESH_BINARY) # making threshold for the contours
- contours, hierarcy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- font = cv2.FONT_HERSHEY_TRIPLEX # Font used in typing
- #Blue Color
- Low = np.array([100, 50, 50])
- High = np.array([140, 255, 255])
- #Green Color
- Low = np.array([40, 50, 50])
- High = np.array([80, 255, 255])
- #Red Color
- Low = np.array([140, 150, 0])
- High = np.array([180, 255, 255])
- #Yellow Color
- Low = np.array([20, 190, 20])
- High = np.array([30, 255, 255])
- for cnt in contours :
- Area = cv2.contourArea(cnt) # Contour's Area
- approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True) # approximation for the contours in the Image
- cv2.drawContours(Img, [approx], 0, (0), 2) # Drawing a line at the Circumfrence of the Contour
- x = approx.ravel()[0]
- y = approx.ravel()[1]
- if len(approx) == 3:
- cv2.putText(Img, "Triangle", (x, y), font, 1, (0)) #putting this text on the pic if we find the object
- print ("Triangle")
- elif len(approx) == 4:
- cv2.putText(Img, "Square", (x, y), font, 1, (0))
- print("Square")
- elif Area < 100 :
- cv2.putText(Img, "Line", (x, y), font, 1, (0))
- print("Line")
- else:
- cv2.putText(Img, "Circle", (x, y), font, 1, (0))
- print("Circle")
- print(Img) # Printing the Matrix of pixels
- cv2.imshow(Name, Img) #Sowing the Image to the user
- cv2.waitKey(0) # Delaying for 5 seconds
- cv2.destroyAllWindows() # Destroying the Image after the Given delay
Advertisement
Add Comment
Please, Sign In to add comment