Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- def getContours(resized):
- contours, hierarchy = cv2.findContours(resized, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- for cnt in contours:
- area = cv2.contourArea(cnt)
- print(area)
- if area > 5:
- cv2.drawContours(imgContour, cnt, -1,(255,0,0), 3)
- peri = cv2.arcLength(cnt,True)
- print(peri)
- approx = cv2.approxPolyDP(cnt, 0.02*peri, True)
- objCor = len(approx)
- x, y, w, h = cv2.boundingRect(approx)
- cv2.rectangle(imgContour, (x,y), (x+w, y+h), (0, 255, 0), 2)
- if objCor == 4: ObjectType = "Pan"
- else: ObjectType="None"
- cv2.putText(imgContour, ObjectType,(x+(w//2)-10, y+(h//2)-10), cv2.FONT_HERSHEY_COMPLEX, 0.25,(0,0,0),1)
- path = r'C:\Users\Polo\Desktop\Python\Dakpannen\dakpannen4.jpg'
- img = cv2.imread(path)
- scale_percent = 300 # percent of original size
- width = int(img.shape[1] * scale_percent / 100)
- height = int(img.shape[0] * scale_percent / 100)
- dim = (width, height)
- resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
- imgGray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
- imgBlur = cv2.GaussianBlur(imgGray, (7,7), 1)
- imgCanny = cv2.Canny(imgBlur, 25,25)
- imgContour = imgGray.copy()
- getContours(imgCanny)
- cv2.imshow('', imgContour)
- cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement