def go(img): def preprocess(img): U = {} V = {} s = {} for i in (0, 1, 2): res = svd(img[..., i], full_matrices=False) U[i] = np.mat(res[0]) V[i] = np.mat(res[2]) s[i] = res[1] return U, V, s U, V, s = preprocess(img) def display(_, _from, _to): res = np.zeros_like(img) for i in (0, 1, 2): _s = s[i].copy() _s[:_from] = 0 _s[_to:] = 0 S = np.mat(np.diag(_s)) res[..., i] = U[i] * S * V[i] return res viewer = ImageViewer(img) plugin = Plugin(image_filter=display) plugin.name = "" plugin += Slider('_from', 0, len(s[0]), 0, 'int', update_on='move') plugin += Slider('_to', 0, len(s[0]), len(s[0]), 'int', update_on='move') viewer += plugin viewer.show()
def go(img): def preprocess(img): U = {} V = {} s = {} for i in (0, 1, 2): res = svd(img[...,i], full_matrices=False) U[i] = np.mat(res[0]) V[i] = np.mat(res[2]) s[i] = res[1] return U, V, s def display(_, R, G, B): rgb = (R, G, B) res = np.zeros_like(img) for i in (0, 1, 2): _s = s[i].copy() _s[:rgb[i]] = 0 S = np.mat(np.diag(_s)) res[...,i] = U[i]*S*V[i] return res def _display_all(RGB): if RGB in cache: return cache[RGB] cache[RGB] = display(None, RGB, RGB, RGB) return cache[RGB] if os.path.isfile(cache_fname): cache = pickle.load(open(cache_fname, "rb")) n = len(cache) - 1 img = _display_all(n) else: U, V, s = preprocess(img) n = len(s[0]) cache = {} for i in range(n + 1): print("caching", i) _display_all(i) with open(cache_fname, "wb") as fh: pickle.dump(cache, fh) print("wrote to:", cache_fname) def display_all(_, RGB): return _display_all(RGB) viewer = ImageViewer(img) plugin = Plugin(image_filter = display_all) plugin.name = "" plugin += Slider('RGB', 0, n, 0, 'int', update_on='move') viewer += plugin viewer.show()
def __init__(self, search_pattern): bottom_px = 60 self.bottom_px = bottom_px plugin = Plugin(image_filter=self.image_filter, dock="right") self.setup_names = ['Bottom'] self.show_orig = CheckBox('show_orig', value=False, alignment='left') plugin += self.show_orig plugin += ComboBox('setup', self.setup_names) plugin += Slider('bottom_px', 0, 500, value=self.bottom_px, value_type='int') plugin += Button("Print", callback=self.on_print_click) fnames = [path for path in glob.iglob(search_pattern, recursive=True)] images, gray_images = read_images(fnames) self.viewer = CollectionViewer(images) self.viewer += plugin
def __init__(self, search_pattern): blur_kernel_size = 1 cal_fnames = [ path for path in glob.iglob('camera_cal/*.jpg', recursive=True) ] cal_images, cal_gray_images = read_images(cal_fnames) self.objpoints, self.imgpoints, _ = get_chessboard_corners( cal_images, cal_gray_images) self.blur_kernel_size = blur_kernel_size self.top_left_x = top_left_x self.top_left_y = top_left_y self.top_right_x = top_right_x self.top_right_y = top_right_y self.bottom_right_x = bottom_right_x self.bottom_right_y = bottom_right_y self.bottom_left_x = bottom_left_x self.bottom_left_y = bottom_left_y plugin = Plugin(image_filter=self.image_filter, dock="right") self.setup_names = [ 'ROI poly', "ROI Transformation", 'ROI Transformed', "Final Transformation poly", 'Final Transformation', 'Final Transformed' ] self.show_orig = CheckBox('show_orig', value=False, alignment='left') plugin += self.show_orig plugin += ComboBox('setup', self.setup_names) plugin += Slider('blur_kernel_size', 0, 31, value=self.blur_kernel_size, value_type='int') plugin += Slider('top_left_x', 0, 1, value=self.top_left_x) plugin += Slider('top_left_y', 0, 1, value=self.top_left_y) plugin += Slider('top_right_x', 0, 1, value=self.top_right_x) plugin += Slider('top_right_y', 0, 1, value=self.top_right_y) plugin += Slider('bottom_right_x', 0, 1, value=self.bottom_right_x) plugin += Slider('bottom_right_y', 0, 1, value=self.bottom_right_y) plugin += Slider('bottom_left_x', 0, 1, value=self.bottom_left_x) plugin += Slider('bottom_left_y', 0, 1, value=self.bottom_left_y) plugin += Button("Print", callback=self.on_print_click) fnames = [path for path in glob.iglob(search_pattern, recursive=True)] images, gray_images = read_images(fnames) self.viewer = CollectionViewer(images) self.viewer += plugin
def go(img): def preprocess(img): U = {} V = {} s = {} for i in (0, 1, 2): res = svd(img[..., i], full_matrices=False) U[i] = np.mat(res[0]) V[i] = np.mat(res[2]) s[i] = res[1] return U, V, s U, V, s = preprocess(img) def display(_, R, G, B): rgb = (R, G, B) res = np.zeros_like(img) for i in (0, 1, 2): _s = s[i].copy() _s[rgb[i]:] = 0 S = np.mat(np.diag(_s)) res[..., i] = U[i] * S * V[i] return res def display_all(_, RGB): return display(_, RGB, RGB, RGB) viewer = ImageViewer(img) plugin = Plugin(image_filter=display) plugin.name = "" plugin += Slider('R', 0, len(s[0]), len(s[0]), 'int') plugin += Slider('G', 0, len(s[0]), len(s[1]), 'int') plugin += Slider('B', 0, len(s[0]), len(s[2]), 'int') viewer += plugin plugin = Plugin(image_filter=display_all) plugin.name = "" plugin += Slider('RGB', 0, len(s[0]), len(s[0]), 'int') viewer += plugin viewer.show()
def __init__(self, search_pattern): self.plugin = Plugin(image_filter=self.image_filter, dock="right") self.setup_names = ['Yellow', 'White', 'Yellow / White'] self.color_spaces = ['LAB', 'HSV', 'HLS', 'LUV'] self.show_orig = CheckBox('show_orig', value=False, alignment='left') self.plugin += self.show_orig self.setup = ComboBox('setup', self.setup_names) self.color_space = ComboBox('color_space', self.color_spaces) self.sensity_range = Slider('sensity_range', 10, 100, value=sensity_range, value_type='int') self.lower_yellow_1 = Slider('lower_yellow_1', 0, 255, value=lower_yellow_1, value_type='int') self.lower_yellow_2 = Slider('lower_yellow_2', 0, 255, value=lower_yellow_2, value_type='int') self.lower_yellow_3 = Slider('lower_yellow_3', 0, 255, value=lower_yellow_3, value_type='int') self.upper_yellow_1 = Slider('upper_yellow_1', 0, 255, value=upper_yellow_1, value_type='int') self.upper_yellow_2 = Slider('upper_yellow_2', 0, 255, value=upper_yellow_2, value_type='int') self.upper_yellow_3 = Slider('upper_yellow_3', 0, 255, value=upper_yellow_3, value_type='int') self.lower_white_1 = Slider('lower_white_1', 0, 255, value=lower_white_1, value_type='int') self.lower_white_2 = Slider('lower_white_2', 0, 255, value=lower_white_2, value_type='int') self.lower_white_3 = Slider('lower_white_3', 0, 255, value=lower_white_3, value_type='int') self.upper_white_1 = Slider('upper_white_1', 0, 255, value=upper_white_1, value_type='int') self.upper_white_2 = Slider('upper_white_2', 0, 255, value=upper_white_2, value_type='int') self.upper_white_3 = Slider('upper_white_3', 0, 255, value=upper_white_3, value_type='int') self.plugin += self.setup self.plugin += self.color_space # self.plugin += self.sensity_range self.plugin += self.lower_yellow_1 self.plugin += self.lower_yellow_2 self.plugin += self.lower_yellow_3 self.plugin += self.upper_yellow_1 self.plugin += self.upper_yellow_2 self.plugin += self.upper_yellow_3 self.plugin += self.lower_white_1 self.plugin += self.lower_white_2 self.plugin += self.lower_white_3 self.plugin += self.upper_white_1 self.plugin += self.upper_white_2 self.plugin += self.upper_white_3 fnames = [path for path in glob.iglob(search_pattern, recursive=True)] images, gray_images = read_images(fnames) self.viewer = CollectionViewer(images) self.viewer.connect_event('button_press_event', self.on_filter_color) self.viewer.connect_event('key_press_event', self.on_press) self.viewer += self.plugin
class ColorViewer: def __init__(self, search_pattern): self.plugin = Plugin(image_filter=self.image_filter, dock="right") self.setup_names = ['Yellow', 'White', 'Yellow / White'] self.color_spaces = ['LAB', 'HSV', 'HLS', 'LUV'] self.show_orig = CheckBox('show_orig', value=False, alignment='left') self.plugin += self.show_orig self.setup = ComboBox('setup', self.setup_names) self.color_space = ComboBox('color_space', self.color_spaces) self.sensity_range = Slider('sensity_range', 10, 100, value=sensity_range, value_type='int') self.lower_yellow_1 = Slider('lower_yellow_1', 0, 255, value=lower_yellow_1, value_type='int') self.lower_yellow_2 = Slider('lower_yellow_2', 0, 255, value=lower_yellow_2, value_type='int') self.lower_yellow_3 = Slider('lower_yellow_3', 0, 255, value=lower_yellow_3, value_type='int') self.upper_yellow_1 = Slider('upper_yellow_1', 0, 255, value=upper_yellow_1, value_type='int') self.upper_yellow_2 = Slider('upper_yellow_2', 0, 255, value=upper_yellow_2, value_type='int') self.upper_yellow_3 = Slider('upper_yellow_3', 0, 255, value=upper_yellow_3, value_type='int') self.lower_white_1 = Slider('lower_white_1', 0, 255, value=lower_white_1, value_type='int') self.lower_white_2 = Slider('lower_white_2', 0, 255, value=lower_white_2, value_type='int') self.lower_white_3 = Slider('lower_white_3', 0, 255, value=lower_white_3, value_type='int') self.upper_white_1 = Slider('upper_white_1', 0, 255, value=upper_white_1, value_type='int') self.upper_white_2 = Slider('upper_white_2', 0, 255, value=upper_white_2, value_type='int') self.upper_white_3 = Slider('upper_white_3', 0, 255, value=upper_white_3, value_type='int') self.plugin += self.setup self.plugin += self.color_space # self.plugin += self.sensity_range self.plugin += self.lower_yellow_1 self.plugin += self.lower_yellow_2 self.plugin += self.lower_yellow_3 self.plugin += self.upper_yellow_1 self.plugin += self.upper_yellow_2 self.plugin += self.upper_yellow_3 self.plugin += self.lower_white_1 self.plugin += self.lower_white_2 self.plugin += self.lower_white_3 self.plugin += self.upper_white_1 self.plugin += self.upper_white_2 self.plugin += self.upper_white_3 fnames = [path for path in glob.iglob(search_pattern, recursive=True)] images, gray_images = read_images(fnames) self.viewer = CollectionViewer(images) self.viewer.connect_event('button_press_event', self.on_filter_color) self.viewer.connect_event('key_press_event', self.on_press) self.viewer += self.plugin def on_filter_color(self, event): if event.inaxes and event.inaxes.get_navigate(): self.viewer.status_message( self.format_coord(event.xdata, event.ydata)) else: self.viewer.status_message('') def format_coord(self, x, y): x = int(x + 0.5) y = int(y + 0.5) pixel = self.viewer.image[y, x] lower_lst = [ pixel[0] - self.sensity_range.val, pixel[1] - self.sensity_range.val, pixel[2] - self.sensity_range.val ] upper_lst = [ pixel[0] + self.sensity_range.val, pixel[1] + self.sensity_range.val, pixel[2] + self.sensity_range.val ] setup = self.setup.val if setup == "Yellow": self.update_yellow_params(*lower_lst, *upper_lst) elif setup == "White": self.update_white_params(*lower_lst, *upper_lst) else: print("Select only one color!") def update_yellow_params(self, lower1, lower2, lower3, upper1, upper2, upper3): lower1 = self.update_val(self.lower_yellow_1, lower1) lower2 = self.update_val(self.lower_yellow_2, lower2) lower3 = self.update_val(self.lower_yellow_3, lower3) upper1 = self.update_val(self.upper_yellow_1, upper1) upper2 = self.update_val(self.upper_yellow_2, upper2) upper3 = self.update_val(self.upper_yellow_3, upper3) self.plugin.filter_image() def update_white_params(self, lower1, lower2, lower3, upper1, upper2, upper3): lower1 = self.update_val(self.lower_white_1, lower1) lower2 = self.update_val(self.lower_white_2, lower2) lower3 = self.update_val(self.lower_white_3, lower3) upper1 = self.update_val(self.upper_white_1, upper1) upper2 = self.update_val(self.upper_white_2, upper2) upper3 = self.update_val(self.upper_white_3, upper3) self.plugin.filter_image() def image_filter(self, image, *args, **kwargs): print("image: ", image.shape) image = apply_crop_bottom(image) # print("cropped image: ", image.shape) image, M, Minv = apply_warp(image) # print("warped image: ", image.shape) show_orig = kwargs["show_orig"] setup = kwargs["setup"] color_space = kwargs["color_space"] lower_yellow_1 = kwargs["lower_yellow_1"] lower_yellow_2 = kwargs["lower_yellow_2"] lower_yellow_3 = kwargs["lower_yellow_3"] upper_yellow_1 = kwargs["upper_yellow_1"] upper_yellow_2 = kwargs["upper_yellow_2"] upper_yellow_3 = kwargs["upper_yellow_3"] lower_white_1 = kwargs["lower_white_1"] lower_white_2 = kwargs["lower_white_2"] lower_white_3 = kwargs["lower_white_3"] upper_white_1 = kwargs["upper_white_1"] upper_white_2 = kwargs["upper_white_2"] upper_white_3 = kwargs["upper_white_3"] if show_orig: return image lower_yellow = np.array( [lower_yellow_1, lower_yellow_2, lower_yellow_3]) upper_yellow = np.array( [upper_yellow_1, upper_yellow_2, upper_yellow_3]) lower_white = np.array([lower_white_1, lower_white_2, lower_white_3]) upper_white = np.array([upper_white_1, upper_white_2, upper_white_3]) target_color_space = cv2.COLOR_RGB2HSV if color_space == "LAB": target_color_space = cv2.COLOR_RGB2Lab elif color_space == "HLS": target_color_space = cv2.COLOR_RGB2HLS elif color_space == "LUV": target_color_space = cv2.COLOR_RGB2LUV result_image = None if setup == "Yellow": result_image = filter_color(image, lower_yellow, upper_yellow, trg_color_space=target_color_space) elif setup == "White": result_image = filter_color(image, lower_white, upper_white, trg_color_space=target_color_space) elif setup == "Yellow / White": result_yellow = filter_color(image, lower_yellow, upper_yellow, trg_color_space=target_color_space) result_white = filter_color(image, lower_white, upper_white, trg_color_space=target_color_space) result_image = cv2.bitwise_or(result_yellow, result_white) return result_image def on_press(self, event): if event.key == 'ctrl+r': self.on_reset() elif event.key == 'ctrl+p': self.on_print() def on_print(self, args=None): print(""" lower_yellow_1 = {} lower_yellow_2 = {} lower_yellow_3 = {} upper_yellow_1 = {} upper_yellow_2 = {} upper_yellow_3 = {} lower_white_1 = {} lower_white_2 = {} lower_white_3 = {} upper_white_1 = {} upper_white_2 = {} upper_white_3 = {} """.format( self.lower_yellow_1.val, self.lower_yellow_2.val, self.lower_yellow_3.val, self.upper_yellow_1.val, self.upper_yellow_2.val, self.upper_yellow_3.val, self.lower_white_1.val, self.lower_white_2.val, self.lower_white_3.val, self.upper_white_1.val, self.upper_white_2.val, self.upper_white_3.val)) def on_reset(self, args=None): self.update_val(self.lower_yellow_1, lower_yellow_1) self.update_val(self.lower_yellow_2, lower_yellow_2) self.update_val(self.lower_yellow_3, lower_yellow_3) self.update_val(self.upper_yellow_1, upper_yellow_1) self.update_val(self.upper_yellow_2, upper_yellow_2) self.update_val(self.upper_yellow_3, upper_yellow_3) self.update_val(self.lower_white_1, lower_white_1) self.update_val(self.lower_white_2, lower_white_2) self.update_val(self.lower_white_3, lower_white_3) self.update_val(self.upper_white_1, upper_white_1) self.update_val(self.upper_white_2, upper_white_2) self.update_val(self.upper_white_3, upper_white_3) self.plugin.filter_image() def update_val(self, comp, newval, min_val=0, max_val=255): newval = max(0, newval) newval = min(255, newval) comp.val = newval comp.editbox.setText("%s" % newval) return newval def show(self): self.viewer.show()
def __init__(self, search_pattern): plugin = Plugin(image_filter=self.image_filter, dock="right") self.setup_names = [ 'Sobel Thresh X', 'Sobel Thresh Y', 'Sobel Thresh X / Y', 'Magnitude Thresh', 'Gradient Direction', 'Magnitude / Gradient Direction Thresh', "2 & 2", "2 | 2" ] self.show_orig = CheckBox('show_orig', value=False, alignment='left') plugin += self.show_orig plugin += ComboBox('setup', self.setup_names) plugin += Slider('grad_ksize', 0, 31, value=grad_ksize, value_type='int') plugin += Slider('grad_thresh_low', 0, 255, value=grad_thresh_low, value_type='int') plugin += Slider('grad_thresh_high', 0, 255, value=grad_thresh_high, value_type='int') plugin += Slider('mag_binary_ksize', 0, 31, value=mag_binary_ksize, value_type='int') plugin += Slider('mag_binary_thresh_low', 0, 255, value=mag_binary_thresh_low, value_type='int') plugin += Slider('mag_binary_thresh_high', 0, 255, value=mag_binary_thresh_high, value_type='int') plugin += Slider('dir_binary_ksize', 0, 31, value=dir_binary_ksize, value_type='int') plugin += Slider('dir_binary_thresh_low', 0, np.pi, value=dir_binary_thresh_low) plugin += Slider('dir_binary_thresh_high', 0, np.pi, value=dir_binary_thresh_high) fnames = [path for path in glob.iglob(search_pattern, recursive=True)] images, gray_images = read_images(fnames) self.viewer = CollectionViewer(images) self.viewer += plugin