def get_contour_from_image(img): line_yellows = get_range( img, np.array([10, 100, 180], np.uint8), np.array([60, 255, 255], np.uint8), cv2.COLOR_BGR2HSV ) # u: 10/110/200 ; 40/255/255 f: 20/110/200 ; 40/255/255 line_whites = get_range( img, np.array([65, 150, 30], np.uint8), np.array([255, 255, 180], np.uint8), cv2.COLOR_BGR2HLS ) # u: 80/150/30 ; 255/255/130 f: 80/150/30 ; 255/255/150 line_reds = get_range(img, np.array([150, 0, 0], np.uint8), np.array([255, 255, 255], np.uint8), cv2.COLOR_BGR2HLS) img_gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY) # convert the color image to gray image contours = get_gray_countour(img_gray) contour_dump("img3/contours_full" + str(int(time.time() * 10)) + ".jpg", contours, img) _, contours = remove_contour(line_yellows, contours, 1) if len(contours) == 0: return None # _, contours = remove_contour( line_whites, contours, -1 ) # u: ## f: ~~ if len(contours) == 0: return None contour_left = contour_largest(contours) # u: largest f: leftest contour_dump( "img/contours_extracted" + str(int(time.time() * 10)) + ".jpg", contour_left, img) return contour_left
def contour_largest(contours, dump=False, filename=""): contour_largest = contours[0] x_largest = 0 for contour in contours: area = cv2.contourArea(contour) # print("area = ", area) if area > x_largest: contour_largest = contour x_largest = area if dump: contour_dump(filename, contour_largest, img) return contour_largest
def contour_leftest(contours, dump=False, filename=""): contour_leftest = contours[0] x_leftest = sys.maxsize for contour in contours: moment_contour = cv2.moments(contour) if moment_contour['m00']: moment_contour_x = int(moment_contour['m10'] / moment_contour['m00']) if moment_contour_x < x_leftest: contour_leftest = contour x_leftest = moment_contour_x if dump: contour_dump(filename, contour_leftest, img) return contour_leftest
def get_contour_from_image( img ): line_yellows = get_yellow( img ) line_whites = get_range( img, np.array([0,160,0], np.uint8), np.array([255,255,255], np.uint8), cv2.COLOR_BGR2HLS) line_reds = get_range( img, np.array([150,0,0], np.uint8), np.array([255,255,255], np.uint8), cv2.COLOR_BGR2HLS) img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # convert the color image to gray image contours = get_gray_countour(img_gray) contour_dump( "img/contours_full" + str(int(time.time()*10)) + ".jpg", contours, img) _, contours = remove_contour( line_yellows, contours, 1 ) if len(contours) == 0: return None _, contours = remove_contour( line_whites, contours, -1 ) if len(contours) == 0: return None contour_left = contour_leftest(contours) contour_dump( "img/contours_extracted" + str(int(time.time()*10)) + ".jpg", contour_left, img) return contour_left
def remove_contour(lines, contours, left_direction, dump=False, filename=None, img=None): # left is 1, other -1 if len(lines) == 0: print("No target lines") return 0, contours to_be_removed = [] ret_contour = [] print("type(contours) = ", type(contours)) print("lines = ", lines) for contour in contours: print("type(contour) = ", type(contour)) moment_contour = cv2.moments(contour) # print("moment_contour['m00'] = ", moment_contour['m00']) # print("moment_contour['m10'] = ", moment_contour['m10']) # print("moment_contour['m01'] = ", moment_contour['m01']) if moment_contour['m00']: moment_contour_x = int(moment_contour['m10'] / moment_contour['m00']) moment_contour_y = int(moment_contour['m01'] / moment_contour['m00']) # print("Center at (", moment_contour_x, ", ", moment_contour_y, ")") fit_value = moment_contour_x * lines[0][0] + lines[0][ 1] - moment_contour_y print("(", "{0:.2f}".format(lines[0][0]), "x+", "{0:.2f}".format(lines[0][1]), "), at (", moment_contour_x, ", ", moment_contour_y, "), Value = ", fit_value) if left_direction == 1: if fit_value * lines[0][0] > 0: ret_contour.append(contour) else: if fit_value * lines[0][0] < 0: ret_contour.append(contour) else: ret_contour.append(contour) if dump: if img is None: raise ValueError('You should input img when you want to dump') contour_dump(filename, ret_contour, img) return len(contours) - len(ret_contour), np.asarray(ret_contour)
def get_contour_from_image(img): line_yellows = get_yellow(img) line_whites = get_white(img) line_reds = get_red(img) img_gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY) # convert the color image to gray image contours = get_gray_countour(img_gray) contour_dump("img/contours_full" + str(int(time.time() * 10)) + ".jpg", contours, img) _, contours = remove_contour(line_yellows, contours, 1) if len(contours) == 0: return None # _, contours = remove_contour( line_whites, contours, -1 ) # if len(contours) == 0: return None contour_left = contour_largest(contours) contour_dump( "img/contours_extracted" + str(int(time.time() * 10)) + ".jpg", contour_left, img) return contour_left, len(line_reds)
def get_contour_from_image( img ): line_yellows = get_yellow( img ) # line_whites = get_white( img ) line_reds = get_red( img ) global redline_cnt if not line_reds: redline_cnt = 0 print("no red line") else: redline_cnt += 1 # cmd = "redline \n" # s.write(cmd.encode()) print("red lines:{0}".format(redline_cnt)) if redline_cnt >= 20: cmd = "/ServoStop/run \n" s.write(cmd.encode()) return None img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # convert the color image to gray image contours = get_gray_countour(img_gray) # contour_dump( "img/contours_full" + str(int(time.time()*10)) + ".jpg", contours, img) _, contours = remove_contour( line_yellows, contours, 1 ) if len(contours) == 0: return None # _, contours = remove_contour( line_whites, contours, -1 ) # if len(contours) == 0: return None contour_large = contour_largest(contours) # contour_dump( "img/contours_extracted" + str(int(time.time()*10)) + ".jpg", contour_large, img) contour_dump( "img/contours_extracted.jpg", contour_large, img) return contour_large