def find_sections(image, border, sections, board_settings): thresholded = utils.threshold(image, board_settings["grid_color"]) dilated = utils.dilate(thresholded, iter_num=int(board_settings["dilation"])) eroded = utils.erode(dilated, iter_num=int(board_settings["erosion"])) black_sections = cv2.bitwise_or(eroded, border) white_sections = cv2.bitwise_not(black_sections) im, contours, hierarchy = cv2.findContours(white_sections.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) contours = sorted(contours, key=cv2.contourArea, reverse=True) for i in range(len(contours)): if cv2.contourArea(contours[i]) * int(board_settings[ "min_section_scale"]) > image.shape[0] * image.shape[1]: section_num = i + 1 cimg = np.zeros_like(white_sections) cv2.drawContours(cimg, contours, i, color=255, thickness=-1) rect = cv2.boundingRect(contours[i]) sections[str(section_num)] = Section(cimg, str(section_num), rect) else: break return sections
def find_and_transform_board(image, sections, board_settings): if (board_settings["border_color"] == "RED"): mask1 = utils.threshold(image, "RED1") mask2 = utils.threshold(image, "RED2") contours = cv2.bitwise_or(mask1, mask2) else: contours = utils.threshold(image, board_settings["border_color"]) dilated = utils.dilate(contours) eroded = utils.erode(dilated) points = find_points(eroded) flood_mask = np.zeros((eroded.shape[0] + 2, eroded.shape[1] + 2), np.uint8) cv2.floodFill(eroded, flood_mask, (0, 0), 255) warped_image = transform_corners(image, points) warped_border = transform_corners(eroded, points) flood_mask = np.zeros( (warped_border.shape[0] + 2, warped_border.shape[1] + 2), np.uint8) cv2.floodFill(warped_border, flood_mask, (0, 0), 255) sections = find_sections(warped_image, warped_border, sections, board_settings) return warped_image, sections, points
def create_mask(self, frame, hsv): mask = utils.hsv_mask(frame, hsv) mask = utils.binary_thresh(mask, 127) edge = self.edge_detection(frame, mask) mask = utils.bitwise_not(mask, edge) mask = utils.erode(mask, self.kernel_big) mask = utils.closing_morphology(mask, kernel_d=self.kernel_small, kernel_e=self.kernel_small, itr=3) return mask
def find_board(image, board_settings): if (board_settings["border_color"] == "RED"): mask1 = utils.threshold(image, "RED1") mask2 = utils.threshold(image, "RED2") contours = cv2.bitwise_or(mask1, mask2) else: contours = utils.threshold(image, board_settings["border_color"]) dilated = utils.dilate(contours) eroded = utils.erode(dilated) points = find_points(eroded) return points is not None
def find_outer_radius(img_arr, center_point): cont = np.copy(img_arr) cont = contrast(cont, 2) grayscale, _ = conv_gray(cont) grayscale, palette = quantize(grayscale, 4) # threshold all values smaller than maximal grayscale[grayscale < palette[-2] + 2] = 0 grayscale[grayscale > 0] = 255 grayscale = erode(grayscale, 7) grayscale = dilate(grayscale, 7) R = estimate_radius(grayscale, center_point) return R
def edge_detection(self, frame, mask): edge = utils.bitwise_and(frame, mask) edge = utils.canny_edge_detection(edge, min_val=100, max_val=125) edge = utils.binary_thresh(edge, 127) edge = utils.array8(edge) edge = utils.dilate(edge, self.kernel_small, itr=3) edge = utils.opening_morphology(edge, kernel_e=self.kernel_small, kernel_d=self.kernel_small, itr=3) mask = utils.bitwise_not(mask, edge) mask = utils.erode(mask, self.kernel_medium, itr=3) mask = utils.closing_morphology(mask, kernel_d=self.kernel_medium, kernel_e=self.kernel_medium, itr=3) return mask
def find_inner_circle(img_arr): img = np.copy(img_arr) img = contrast(img, 1.1) img = threshold(img, 10) dilated = dilate(img, 9) eroded = erode(dilated, 9) regions = find_regions(eroded) outlined_regions, filled = outline_regions(regions) regions_areas = np.unique(regions, return_counts=True) guess_for_pupil = find_pupil(outlined_regions, regions_areas, img) center = guess_for_pupil['center'] radius = (guess_for_pupil['x_max'] - guess_for_pupil['x_min'] + guess_for_pupil['y_max'] - guess_for_pupil['y_min']) / 4 return radius, center
def ajouterIndicationCouleursZoneErosionSuccessives(imgContour, imgNettoyee, rayonDisqueCouleur): taille = imgNettoyee.shape gamma8 = strel.build('carre', 1, None) imgFinale = np.zeros(taille, np.float) imgFinale = np.uint8(imgFinale) imgFinale = imgFinale + imgContour imgFinale += imgContour structElementCercle = strel.build_as_list('disque', rayonDisqueCouleur, None) m = np.copy(imgContour) imgContour[0, :] = 0 imgContour[m.shape[0] - 1, :] = 0 imgContour[:, 0] = 0 imgContour[:, m.shape[1] - 1] = 0 # Iteration sur les pixels de imgContour for x in range(0, taille[0]): for y in range(0, taille[1]): if np.any(imgContour[x, y]) != 0: # On cherche a isoler la zone m[:] = 0 m[x, y] = 255 imgReconInf = utils.reconstructionInferieure(imgContour, m, gamma8) # Erosion successive jusqu'a faire disparaitre la zone continuer = True i = 1 while continuer: structElement = strel.build('disque', i, None) imgErodee = utils.erode(imgReconInf, structElement) # Quand la zone a disparu, alors on prend l'erosion successive precedente afin de placer notre cercle de couleur if np.amax(imgErodee) == 0: structElement = strel.build('disque', i - 1, None) imgErodee = utils.erode(imgReconInf, structElement) # On reboucle afin de chercher un pixel de la zone isolee restant apres les erosions successives for x in range(0, taille[0]): for y in range(0, taille[1]): if np.any(imgErodee[x, y]) != 0: pixel = (x, y) break # Boucle permettant de creer notre cercle de couleur a partir de l'element structurant for (i, j) in structElementCercle: if pixel[0] < (taille[1] - rayonDisqueCouleur) and pixel[1] < (taille[0] - rayonDisqueCouleur): imgFinale[pixel[0] + i, j + pixel[1]] = imgNettoyee[pixel[0], pixel[1]] continuer = False i = i + 1 # on enleve la zone isolee de image contour afin de ne pas traiter plusieurs fois les zones imgContour = imgContour - imgReconInf return imgFinale