def Otsu_Detection(mypath): #read image for fn in glob(mypath + "\\*.jpg"): image = cv2.imread(fn) #grayscale image gray = pp.grayImage(image) #filter image Filter = pp.Gfilter(gray) #segment image segment = seg.otsu(Filter) #detect edges edged = seg.CannySeg(segment) #dilate segmented image gradient = cv2.morphologyEx(edged, cv2.MORPH_GRADIENT, det.getKennel()) #get contours of the image contours = det.getContours(gradient, image) #calculate contours area getArea = det.get_contour_areas(contours) # Labeling Contours left to right for (i, c) in enumerate(contours): hull = cv2.convexHull(c) M = cv2.moments(hull) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) #if shape looks like circle/pentagon, treat as pothole shape_detector_start = ShapeDetector.detect(shape_detector, hull) if (shape_detector_start == "circle" or shape_detector_start == "pentagon" or shape_detector_start == "rectangle") and getArea[i] > 3250: draw = cv2.drawContours(image, [hull], 0, (0, 0, 255), 2) cv2.putText(image, "Pothole", (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) else: continue cv2.imshow("result", image) cv2.waitKey(0) cv2.destroyAllWindows()
import cv2 #OpenCv import numpy as np #numpy import pre_processing as pp # preprocessing module import segmentation as seg # segmentation module import detection as det #detection module import ROI # get Region Of Interest from glob import glob #kennel size getkennel = det.getKennel() #Shape Detector class ShapeDetector: def __init__(self): pass def detect(self, c): # initialize the shape name and approximate the contour shape = "unidentified" peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.05 * peri, False) # if the shape is a triangle, it will have 3 vertices if len(approx) == 3: shape = "triangle" # if the shape has 4 vertices, it is either a square or # a rectangle elif len(approx) == 4: # compute the bounding box of the contour and use the # bounding box to compute the aspect ratio (x, y, w, h) = cv2.boundingRect(approx)