def display_contours(self, img, amount=0, color=None, save_path=None): """ Action: Displays the image with the current list of contours :param img: image from which the contours were taken from, numpy array or image path :param color: the color of the of the contours outline :param amount: amount of contours to display :param save_path: if the image should be saved, pass the wanted result path :return: the image with the drawn contours. """ if type(img) is str: img = cv2.imread(img) image_for_display = copy.copy(img) if self.contour_amount > 0: if amount > 0: output_contours = self.contours[0:amount] else: output_contours = self.contours if type(output_contours) is not list: output_contours = [output_contours] if color is None: color = (0, 0, 0) cv2.drawContours(image_for_display, output_contours, -1, color, 2) if type(save_path) is str: cv2.imwrite(save_path, image_for_display) display_image(image_for_display) return image_for_display
def translate_parameters(self): """ Translates parameters to a json save-able format, capable of translating Color and MultiColor objects too. :return: the dictionary with the translated parameters for filter functions """ param_copy = copy.copy(self.__params) for idx1, param_list in enumerate(param_copy): for idx2, val in enumerate(param_list): if type(val) is Color: param_copy[idx1][idx2] = Color.json_serialize(val) if type(val) is MultiColor: param_copy[idx1][idx2] = MultiColor.json_serialize(val) return param_copy
def minimize_temperature(self): """ """ import random, numpy, math, copy tour = list(self.sequence) cities = self.data N = len(self.sequence) for temperature in numpy.logspace(0, 5, num=100000)[::-1]: [i, j] = sorted(random.sample(list(range(N)), 2)) newTour = tour[:i] + tour[j:j + 1] + tour[i + 1:j] + tour[i:i + 1] + tour[j + 1:] if math.exp((sum([ math.sqrt( sum([(cities[tour[ (k + 1) % N]][d] - cities[tour[k % N]][d])**2 for d in [0, 1]])) for k in [j, j - 1, i, i - 1] ]) - sum([ math.sqrt( sum([(cities[newTour[ (k + 1) % N]][d] - cities[newTour[k % N]][d])**2 for d in [0, 1]])) for k in [j, j - 1, i, i - 1] ])) / temperature) > random.random(): self.sequence = copy.copy(newTour)
def apply_sample(self, **kwargs): """ Action: Finds contours and applies filters on a single image given by a image path, image object (numpy array) or camera port and takes an image, a custom color can be passed through the kw color. the kw display/show determines how the result is displayed, None will not display, False with imshow, True with matlplotlib. :param kwargs: :return: the image (numpy array) and the contours found (list of numpy arrays) """ def cam_port_activate(port_val, color, wid, hgt): if self.camera is None: self.camera_setup(port_val, img_width=wid, img_height=hgt) return_val, image = self.camera.read() if return_val: self.contours = self.get_contours(image, color) self.apply_all_filters() else: print_pipe( self.log_path, "Something went wrong with getting the sample image!") return self.contours, image def image_activate(image_loc, color): if type(image_loc) is str: image = cv2.imread(image_loc, cv2.IMREAD_COLOR) else: image = image_loc self.contours = self.get_contours(image, color) self.apply_all_filters() return self.contours, image multi_mode = False if "color" in kwargs: color_object = kwargs["color"] if type(color_object) is MultiColor: multi_mode = True elif "hsv_low_limit" in kwargs and "hsv_high_limit" in kwargs: color_object = Color(kwargs["hsv_low_limit"], kwargs["hsv_high_limit"]) else: if type(self.color) is MultiColor: multi_mode = True color_object = self.color if "display" in kwargs: display = kwargs["display"] elif "show" in kwargs: display = kwargs["show"] else: display = None if "camera_port" in kwargs: port = kwargs["camera_port"] contours, img = cam_port_activate(port, color_object, self.width, self.height) elif "port" in kwargs: port = kwargs["port"] contours, img = cam_port_activate(port, color_object, self.width, self.height) elif "image" in kwargs: contours, img = image_activate(kwargs["image"], color_object) elif "img" in kwargs: contours, img = image_activate(kwargs["img"], color_object) elif "pic" in kwargs: contours, img = image_activate(kwargs["pic"], color_object) elif "picture" in kwargs: contours, img = image_activate(kwargs["picture"], color_object) else: raise ValueError("No image or camera port given!") if display is not None: image_for_display = copy.copy(img) drawn = cv2.drawContours(image_for_display, contours, -1, BuiltInColors.red_high_hsv.high_bound) if display: figure(str(time.time())) image_for_display = cv2.cvtColor(drawn, cv2.COLOR_HSV2RGB) imshow(image_for_display) show() else: cv2.imshow(str(time.time()), image_for_display) cv2.waitKey() return contours, img