def process(x): # load the image and resize it to a smaller factor so that # the shapes can be approximated better image = cv2.imread(x) resized = imutils.resize(image, width=300) ratio = image.shape[0] / float(resized.shape[0]) adjusted = adjust_gamma(resized, gamma=1.0) # blur the resized image slightly, then convert it to both # grayscale and the L*a*b* color spaces blurred = cv2.GaussianBlur(adjusted, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)[1] # find contours in the thresholded image cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # initialize the shape detector and color labeler # sd = ShapeDetector() cl = ColorLabeler() # loop over the contours for c in cnts: # compute the center of the contour M = cv2.moments(c) if M["m00"] == 0: # M["m00"] = 1 continue cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) # detect the shape of the contour and label the color # shape = sd.detect(c) color = cl.label(lab, c) # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape and labeled # color on the image c = c.astype("float") c *= ratio c = c.astype("int") text = "{}".format(color) if text != "NaN": cv2.drawContours(image, [c], -1, (255, 255, 255), 2) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) else: cv2.drawContours(image, [c], -1, (255, 255, 255), 2) # show the output image cv2.imwrite(x, image)
cnts = cnts[0] if imutils.is_cv2() else cnts[1] # initialize the shape detector and color labeler sd = ShapeDetector() cl = ColorLabeler() # loop over the contours for c in cnts: # compute the center of the contour M = cv2.moments(c) cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) # detect the shape of the contour and label the color shape = sd.detect(c) color = cl.label(lab, c) # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape and labeled # color on the image c = c.astype("float") c *= ratio c = c.astype("int") text = "{} {}".format(color, shape) 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) # show the output image cv2.imshow("Image", image) cv2.waitKey(0)
def getGPSPos(image, ROBOT='blue'): blurred = cv2.GaussianBlur(image, (5, 5), 0) ## convert to hsv hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) if ROBOT == 'blue': #Blue mask = cv2.inRange(hsv, (177 / 2, 100, 40), (197 / 2, 255, 255)) if ROBOT == 'red': #Red mask = cv2.inRange(hsv, (340 / 2, 100, 40), (360 / 2, 255, 255)) blurred = cv2.bitwise_and(image, image, mask=mask) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] # find contours in the thresholded image cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if cnts is None: return 0, 0, 0 cnts = cnts[1] # initialize the shape detector and color labeler sd = ShapeDetector() cl = ColorLabeler() _, ySize = mask.shape[:2] if isinstance(cnts, Iterable): # loop over the contours for c in cnts: # compute the center of the contour M = cv2.moments(c) if M["m00"] != 0: cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) else: cX = 0 cY = 0 # detect the shape of the contour and label the color shape = sd.detect(c) color = cl.label(lab, c) if shape == "triangle" and color == ROBOT: # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape and labeled # color on the image c = c.astype("float") c = c.astype("int") # Find sticky point maxDistance = 0 for point in c: distance = euclideanDistance(point[0], [cX, cY]) if distance > maxDistance: maxDistance = distance stickyPoint = point pixelAngle = math.atan2( (ySize - cY) - (ySize - stickyPoint[0][1]), cX - stickyPoint[0][0]) if pixelAngle < 0: pixelAngle = 2 * math.pi + pixelAngle return cX, cY, pixelAngle else: return 0, 0, 0 else: return 0, 0, 0
cv2.CHAIN_APPROX_SIMPLE) contours_b = cv2.findContours(thresh_b.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours_r = imutils.grab_contours(contours_r) contours_g = imutils.grab_contours(contours_g) contours_b = imutils.grab_contours(contours_b) # initialize the sahpe detector and color labeler cl = ColorLabeler() for c in contours_r: if cv2.contourArea(c) > 0: M = cv2.moments(c) red_label = cl.label(lab_r, c) # label color of object c = c.astype("int") # change to type int text = "{}".format(red_label) cv2.drawContours(median_red, [c], -1, (0, 255, 0), 2) cv2.drawContours(median_red, [c], -1, (0, 255, 0), 3) if red_label == "red": red_detector = 1 robot_move = 1 robot_text = "red" if blue_detector == 1 or green_detector == 1: defect_counter += 1 blue_detector = 0 green_detector = 0 for d in contours_b:
# shape detector cnts = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() cl = ColorLabeler() # loop over the contours for c in cnts: # compute the center of the contour, then detect the name of the # shape using only the contour M = cv2.moments(c) cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) shape = sd.detect(c) color = cl.label(image, c) # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape on the image c = c.astype("float") c *= ratio c = c.astype("int") text = "{} {}".format(color, shape) cv2.drawContours(image, [c], -1, (255, 255, 255), 3) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) # show the output image cv2.imshow("Image", frame) cv2.waitKey(0)
cnts = cnts[0] if imutils.is_cv2() else cnts[1] sd = ShapeDetector() cl = ColorLabeler() for c in cnts: peri = cv2.arcLength(c, True) if(peri < 50): continue M = cv2.moments(c) if(M["m00"] != 0): shape = sd.detect(c) color = cl.label(blurred, c) c = c.astype("float") c *= ratio c = c.astype("int") print(shape, color) if(color == "vermelho"): sendSerial("r") if(color == "verde"): sendSerial("g") text = "{} {}".format(shape, color) cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
def ProcessImage(self, image, colour): # View the original image seen by the camera. if debug: cv2.imshow('original', image) time.sleep(0.3) #cv2.waitKey(0) # convert the resized image to grayscale, blur it slightly, # and threshold it blurred_img = cv2.GaussianBlur(image, (7, 7), 0) #if debug: # cv2.imshow('GaussianBlur', blurred_img) # cv2.waitKey(0) grey_img = cv2.cvtColor(blurred_img, cv2.COLOR_BGR2GRAY) #if debug: # cv2.imshow('cvtColor GRAY', grey_img) # cv2.waitKey(0) lab_img = cv2.cvtColor(blurred_img, cv2.COLOR_BGR2LAB) #if debug: # cv2.imshow('cvtColor LAB', lab_img) # cv2.waitKey(0) #thresh_img = cv2.threshold(grey_img, 60, 255, cv2.THRESH_BINARY)[1] thresh_img = cv2.adaptiveThreshold(grey_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) if debug: cv2.imshow('threshold', thresh_img) time.sleep(0.3) #cv2.waitKey(0) ## Blur the image #image = cv2.medianBlur(image, 5) #if debug: # cv2.imshow('blur', image) # cv2.waitKey(0) ## Convert the image from 'BGR' to HSV colour space #image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) #if debug: # cv2.imshow('cvtColour', image) # cv2.waitKey(0) ## We want to extract the 'Hue', or colour, from the image. The 'inRange' ## method will extract the colour we are interested in (between 0 and 180) ## In testing, the Hue value for red is between 95 and 125 ## Green is between 50 and 75 ## Blue is between 20 and 35 ## Yellow is... to be found! #if colour == "red": # imrange = cv2.inRange(image, numpy.array((95, 127, 64)), numpy.array((125, 255, 255))) #elif colour == "green": # imrange = cv2.inRange(image, numpy.array((40, 127, 64)), numpy.array((75, 255, 255))) #elif colour == 'blue': # imrange = cv2.inRange(image, numpy.array((20, 64, 64)), numpy.array((35, 255, 255))) # View the filtered image found by 'imrange' #if debug: # cv2.imshow('imrange', imrange) # cv2.waitKey(0) # I used the following code to find the approximate 'hue' of the ball in # front of the camera # for crange in range(0,170,10): # imrange = cv2.inRange(image, numpy.array((crange, 64, 64)), numpy.array((crange+10, 255, 255))) # print(crange) # cv2.imshow('range',imrange) # cv2.waitKey(0) # Find the contours #contour_img, contours, hierarchy = cv2.findContours(thresh_img.copy(), cv2.RETR_LIST, # cv2.CHAIN_APPROX_SIMPLE) #if debug: # cv2.imshow('contour', contour_img) # cv2.waitKey(0) # cv2.RETR_EXTERNAL - just outermost contour # cv2.RETR_LIST - all contours, no heirarchy contours = cv2.findContours(thresh_img.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours = contours[0] if imutils.is_cv2() else contours[1] print('count of contours={}'.format(len(contours))) sd = ShapeDetector() cl = ColorLabeler() # Go through each contour #foundArea = -1 #foundX = -1 #foundY = -1 squares = [] sc = [] for idx, contour in enumerate(contours): #print('contour={}'.f5ormat(contour)) x, y, w, h = cv2.boundingRect(contour) if w <= 15 or h <= 15 or w > 50 or h > 50: continue sc.append(contour) print('x={} y={} w={} h={}'.format(x, y, h, w)) cx = x + (w / 2) cy = y + (h / 2) area = w * h print(' cx={} cy={} area={}'.format(cx, cy, area)) shape = sd.detect(contour) colour = cl.label(lab_img, contour) print(' shape={} color={}'.format(shape, colour)) squares.append([idx, x, y, h, w, cx, cy, area, colour]) #if foundArea < area: # foundArea = area # foundX = cx # foundY = cy #if foundArea > 0: # ball = [foundX, foundY, foundArea] #else: # ball = None ## Set drives or report ball status #self.SetSpeedFromBall(ball) print('Found {} shapes.'.format(len(squares))) print('Shapes={}'.format(squares)) #print([i[0] for i in squares]) for sq in squares: cv2.rectangle(image, (sq[1], sq[2]), (sq[1] + sq[3], sq[2] + sq[4]), (0, 0, 255) if sq[8] == 'red' else (255, 0, 0), cv2.FILLED) cv2.drawContours(image, sc, -1, (255, 0, 0), cv2.FILLED) cv2.imshow('filled', image) cv2.waitKey(0) cv2.destroyAllWindows() #sort by cy ysorted_sqs = OrderedDict() min_x = squares[0][0] min_y = squares[0][1] for sq in squares: # add cx, cy, h, v, colour ysorted_sqs[sq[6]] = (sq[5], sq[6], sq[3], sq[4], sq[8]) if sq[0] < min_x: min_x = sq[0] if sq[1] < min_y: min_y = sq[1] #group by rows, centres differ by less than a 3rd. rows = [] for idx, ssq in enumerate(ysorted_sqs): if idx != 0: # 0=cx, 1=cy, 2=h, 3=v, 4=colour if abs(ssq[1] - ysorted_sqs[idx - 1][1]) > ysorted_sqs[idx - 1][3] / 3: # not same row, add to new row rows.append([ssq]) # add square to current row rows.extend([ssq]) #sort by cx for row in rows: xsorted_sqs = OrderedDict() for ssq in row: xsorted_sqs[ssq[0]] = ssq
def main(argv): # Read the image data from argument image = cv2.imread(argv[1]) # Resize if it is necessary(faster but not accurate) resized = imutils.resize(image, width=800) ratio = image.shape[0] / float(resized.shape[0]) blurred = cv2.GaussianBlur(resized, (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] # Get the edge by canny effect canny = cv2.Canny(gray.copy(), 80, 120) # Set the kernel for dilation(make the white line thicker) kernel = np.ones((3, 3), np.uint8) dilation = cv2.dilate(canny.copy(), kernel, iterations=7) cv2.imwrite('dilate.jpg', dilation) # Get the contour from dilated picture, EXTERNAL contour used cnts = cv2.findContours(dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cnts = imutils.grab_contours(cnts) # ColorLabel # initialize the ShapeDetector class sd = ShapeDetector() cl = ColorLabeler() oracle = [] for c in cnts: try: # Do if the contourarea is larger than specific area if cv2.contourArea( c) > np.shape(image)[0] / 40 * np.shape(image)[1] / 40: # Get the moments of the shapes M = cv2.moments(c) cX = int(M['m10'] / M['m00']) cY = int(M['m01'] / M['m00']) # Detect the shape shape = sd.detect(c) color = cl.label(lab, c) # Put additional information in the image 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, (0, 0, 255), 2, ) # Store the shape information to oracle oracle.append([shape, np.array([cX, cY]), color]) except: continue # Save the image file cv2.imwrite('Shape_recognized.jpg', image) alsoOracle = oracle oracle = np.array(oracle) #print(oracle) # Create the json file to return the following value to PHP # ["Type of shape", "RowID", "number of shape in the same row"] jsonfile = get_block(image, oracle, (6, 6), alsoOracle) print(jsonfile)