def countcotours(img): nc=0 dir="img/"+img+".png" image = cv2.imread(dir) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Image", gray) cv2.waitKey(0) blurred = cv2.GaussianBlur(gray, (5, 5), 0) cv2.imshow("Image", blurred) cv2.waitKey(0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1] cv2.imshow("Image", thresh) cv2.waitKey(0) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() for c in cnts: # compute the center of the contour if cv2.contourArea(c)<800: continue else: M = cv2.moments(c) cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) shape = sd.detect(c) #if(shape=="square" or shape=="rectangle"): if 1==1: cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.circle(image, (cX, cY), 7, (255, 255, 255), -1) cv2.putText(image, "center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) nc=nc+1 cv2.imshow("Image", image) cv2.waitKey(0) return nc
def findcolors(nc,im): cont=0 image = cv2.imread(im) blurred = cv2.GaussianBlur(image, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] cl = ColorLabeler() sd = ShapeDetector() for c in cnts: if cv2.contourArea(c)< 800: continue else: M = cv2.moments(c) cX = int((M["m10"] / M["m00"])) cY = int((M["m01"] / M["m00"])) shape = sd.detect(c) color = cl.label(lab, c) print(shape) print(color) if (shape=="square" or shape=="rectangle"): if (color==nc): text = "{}".format(color) cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cont=cont+1 cv2.imshow("Image", image) cv2.waitKey(0) return cont
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY_INV)[1] # find contours in thresholded image and initialize shape detector cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() # loop over contours for c in cnts: # compute center, detect name of shape M = cv2.moments(c) cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) shape = sd.detect(c) # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and nape of the shapes c = c.astype("float") c *= ratio c = c.astype("int") cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) # show image cv2.imshow("Image", image) cv2.waitKey(0)
original=image.copy() gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) blurred=cv2.GaussianBlur(gray,(5,5),0) thresh=cv2.threshold(blurred,60,255,cv2.THRESH_BINARY)[1] cnts=cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts=cnts[1] sd=ShapeDetector() for c in cnts: M=cv2.moments(c) cX=0 cY=0 if(M["m00"])!=0: cX=int(M["m10"]/M["m00"]) cY=int(M["m01"]/M["m00"]) shape=sd.detect(c) cv2.drawContours(image,[c],-1,(0,255,0),2) cv2.circle(image,(cX,cY),7,(255,255,255),2) cv2.putText(image, shape, (cX - 20, cY - 20),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) plt.subplot(2,2,1),plt.imshow(original,cmap = 'gray'),plt.title('Original') plt.axis("off") plt.subplot(2,2,2),plt.imshow(thresh,cmap="gray"),plt.title('Threshold') plt.axis("off") plt.subplot(2,2,3),plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)),plt.title('Result') plt.axis("off") plt.show()
class PartDetector: def __init__(self): self.sd = ShapeDetector() def detect(self, frame, markInFrame=False): resized = imutils.resize(frame, width=300) ratio = frame.shape[0] / float(resized.shape[0]) contours = self._find_contour(resized) if len(contours) == 0: return None, None contour, center = self._Filter_n_find_center(contours, resized.shape, ratio) if contour is None: return None, None shape = self.sd.detect(contour) if shape is None: return None, None if markInFrame: c = contour.astype("float") c *= ratio c = c.astype("int") self._mark_object(frame, c, center, shape) return shape, center def _find_contour(self, frame): #cv2.imshow('frame',frame) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #cv2.imshow('gray',gray) blurred = cv2.GaussianBlur(gray, (5, 5), 0) #blurred = cv.medianBlur(gray, 5) #cv2.imshow('blurred',blurred) #clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) #hist_equal = clahe.apply(blurred) hist_equal = cv2.equalizeHist(blurred) #cv2.imshow('hist_equal',hist_equal) ret, thresh = cv2.threshold( hist_equal, 50, 255, cv2.THRESH_BINARY_INV) # + cv2.THRESH_OTSU) thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C,\ cv2.THRESH_BINARY_INV,11,2) thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY_INV,11,2) """ thresh[:,:60] = 0 thresh[:,-60:] = 0 thresh[:60,:] = 0 thresh[-60:,:] = 0 """ #cv2.imshow('thresh',thresh) # Taking a matrix of size 5 as the kernel #kernel = np.ones((3,3), np.uint8) # The first parameter is the original image, # kernel is the matrix with which image is # convolved and third parameter is the number # of iterations, which will determine how much # you want to erode/dilate a given image. #img_erosion = cv2.erode(thresh, kernel, iterations=1) #thresh = cv2.dilate(img_erosion, kernel, iterations=1) #cv2.imshow('erode_dilate',thresh) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) return cnts def _Filter_n_find_center(self, cnts, shape, ratio=1): for c in cnts: M = cv2.moments(c) if M["m00"] == 0: continue cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) area = M['m00'] frameArea = shape[0] * shape[1] areaRatio = area / frameArea if areaRatio < 0.01 or areaRatio > 0.1: continue return c, [cX, cY] return None, None def _mark_object(self, frame, contour, center, shape, ratio=1): cv2.circle(frame, tuple(center), 5, (0, 255, 0)) cv2.drawContours(frame, [contour], -1, (0, 255, 0), 2) cv2.putText(frame, shape, tuple(center), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)