Advertisement
Guest User

Code

a guest
Dec 3rd, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. def getContours(resized):
  5.     contours, hierarchy = cv2.findContours(resized, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  6.     for cnt in contours:
  7.         area = cv2.contourArea(cnt)
  8.         print(area)
  9.         if area > 5:
  10.             cv2.drawContours(imgContour, cnt, -1,(255,0,0), 3)
  11.             peri = cv2.arcLength(cnt,True)
  12.             print(peri)
  13.             approx = cv2.approxPolyDP(cnt, 0.02*peri, True)
  14.             objCor = len(approx)
  15.             x, y, w, h = cv2.boundingRect(approx)
  16.             cv2.rectangle(imgContour, (x,y), (x+w, y+h), (0, 255, 0), 2)
  17.            
  18.             if objCor == 4: ObjectType = "Pan"
  19.             else: ObjectType="None"
  20.             cv2.putText(imgContour, ObjectType,(x+(w//2)-10, y+(h//2)-10), cv2.FONT_HERSHEY_COMPLEX, 0.25,(0,0,0),1)
  21.  
  22.  
  23. path = r'C:\Users\Polo\Desktop\Python\Dakpannen\dakpannen4.jpg'
  24. img = cv2.imread(path)
  25.  
  26. scale_percent = 300 # percent of original size
  27. width = int(img.shape[1] * scale_percent / 100)
  28. height = int(img.shape[0] * scale_percent / 100)
  29. dim = (width, height)
  30.  
  31. resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
  32.  
  33. imgGray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
  34. imgBlur = cv2.GaussianBlur(imgGray, (7,7), 1)
  35. imgCanny = cv2.Canny(imgBlur, 25,25)
  36. imgContour = imgGray.copy()
  37. getContours(imgCanny)
  38.  
  39. cv2.imshow('', imgContour)
  40. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement