def HSV(img_in, x, y): if type(img_in) == type(''): frame = read_image(fname) else: frame = img_in frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) return {'BGR': frame[x, y], 'HSV': frameHSV[x, y]}
def get_cmass(img_in): img = read_image(img_in) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) mom = cv2.moments(img_gray) mx = mom['m10'] / mom['m00'] my = mom['m01'] / mom['m00'] return (int(mx), int(my))
def MinMaxAvgHSV(img_in, x1, y1, x2, y2, ignore_mask = None): if type(img_in) == type(''): frame = read_image(fname) else: frame = img_in frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) MinHSV = np.array([255, 255, 255], dtype='int32') MaxHSV = np.array([0, 0, 0], dtype='int32') ignored = 0 not_ignored = 0 for j in range(x1, x2): for i in range(y1, y2): try: if (frame[j, i] == ignore_mask).all(): ignored += 1 continue except: print j, i print img_in.shape not_ignored += 1 for k in [0, 1, 2]: if frameHSV[j, i][k] < MinHSV[k]: MinHSV[k] = frameHSV[j, i][k] if frameHSV[j, i][k] > MaxHSV[k]: MaxHSV[k] = frameHSV[j, i][k] print 'ignored px.', ignored, 'not_ignored px.', not_ignored AvgHSV = (MinHSV + MaxHSV) / 2 return (MinHSV.astype('uint8'), MaxHSV.astype('uint8'), AvgHSV.astype('uint8'))
def test_color(): color_ranges = [] for color in ['red', 'blue', 'yellow']: img = read_image('../pure_markers/hand_3fingers_%s_frame300.png' % color) color_rng = MinMaxHSV(img, 0, 0, img.shape[1], img.shape[0], ignore_mask = np.array([255, 255, 255])) print color, color_rng color_ranges.append(color_rng) inters = ColorRangeIntersection(color_ranges[0], color_ranges[2]) volume = ColorRangeIntersectionVolume(inters) print 'Intersection:', inters, 'Intersection volume:', volume
def test_color(): color_ranges = [] for color in ['red', 'blue', 'yellow']: img = read_image('../pure_markers/hand_3fingers_%s_frame300.png' % color) color_rng = MinMaxAvgHSV(img, 0, 0, img.shape[1], img.shape[0], ignore_mask = np.array([255, 255, 255])) print color, color_rng color_ranges.append(color_rng) inters = ColorRangeIntersection(color_ranges[0], color_ranges[2]) volume = ColorRangeIntersectionVolume(inters) print 'Intersection:', inters, 'Intersection volume:', volume
def MinMaxHSV(img_in, x1, y1, x2, y2, ignore_mask = None): if type(img_in) == type(''): frame = read_image(fname) else: frame = img_in frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) MinHSV = [255, 255, 255] MaxHSV = [0, 0, 0] for i in range(x1, x2): for j in range(y1, y2): for k in [0, 1, 2]: if ignore_mask != None and (frame[j, i] == ignore_mask).all(): continue if frameHSV[j, i][k] < MinHSV[k]: MinHSV[k] = frameHSV[j, i][k] if frameHSV[j, i][k] > MaxHSV[k]: MaxHSV[k] = frameHSV[j, i][k] return (MinHSV, MaxHSV)
def MinMaxHSV(img_in, x1, y1, x2, y2, ignore_mask=None): if type(img_in) == type(''): frame = read_image(fname) else: frame = img_in frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) MinHSV = [255, 255, 255] MaxHSV = [0, 0, 0] for i in range(x1, x2): for j in range(y1, y2): for k in [0, 1, 2]: if ignore_mask != None and (frame[j, i] == ignore_mask).all(): continue if frameHSV[j, i][k] < MinHSV[k]: MinHSV[k] = frameHSV[j, i][k] if frameHSV[j, i][k] > MaxHSV[k]: MaxHSV[k] = frameHSV[j, i][k] return (MinHSV, MaxHSV)
def GUIMinMaxHSV(img_in): if type(img_in) == type(''): frame = read_image(fname) else: frame = img_in cv2.namedWindow('wnd', 0) cv2.setMouseCallback('wnd', on_mouse_event, 0) cv2.imshow('wnd', frame) points_selected = 0 points = [] while cv2.waitKey(100) != 32: if len(points) == 2: print MinMaxHSV(frame, points[0][0], points[0][1], points[1][0], points[1][1]) points = [] if mouse_events[cv2.EVENT_LBUTTONDOWN]: points.append((mouse_x, mouse_y)) clear_events() cv2.destroyWindow('wnd') cv2.waitKey(1000)
def GUIMinMaxHSV(img_in): if type(img_in) == type(''): frame = read_image(fname) else: frame = img_in cv2.namedWindow('wnd', 0) cv2.setMouseCallback('wnd', on_mouse_event, 0) cv2.imshow('wnd', frame) points_selected = 0 points = [] while cv2.waitKey(100) != 32: if len(points) == 2: print MinMaxAvgHSV(frame, points[0][0], points[0][1], points[1][0], points[1][1]) points = [] if mouse_events[cv2.EVENT_LBUTTONDOWN]: points.append(np.array([mouse_x, mouse_y], dtype='uint16')) clear_events_mouse() cv2.destroyWindow('wnd') cv2.waitKey(1000)
def generate_hist_files(fnames): for fn in fnames: pic_fn = fn[0:fn.rfind('.')] h = calc_hue_hist(read_image(pic_fn)) cv2.normalize(h['data'], h['data'], 0, 255, cv2.NORM_MINMAX) save_hist(h, fn)
def test_hue_hist(): img = read_image(FRAME_FILE) h = calc_hue_hist(img) loaded_h = load_hist(HIST_FILE) return all(map(lambda x: x[0], loaded_h['data'] == h['data']))
def GUICalibrateHSV(img_in, fname): if type(img_in) == type(''): frame = read_image(fname) else: frame = img_in main_labels = [ '1) Draw a bounding box around next cube to zoom it in;',\ '2) In the zoomed area draw a contour around the cube, press \'Enter\';',\ '3) Select the color in the terminal:',\ ' Type one character designating the color (r, g, b, c, m or y)',\ ' and press \'Enter\';',\ '4) Then proceed with the next cube in the reopened image window;',\ '5) When all the cube types were selected, press \'Esc\' to exit.'\ ] labels = {\ 'zoom1': main_labels,\ 'zoom2': main_labels,\ 'hsv_select_roi': [],\ 'hsv': main_labels,\ } disp_frame = frame.copy() zoomed_frame = None wnd_name = 'Calibrate Color for Cube detection' cv2.namedWindow(wnd_name, 0) cv2.setMouseCallback(wnd_name, on_mouse_event, 0) points_selected = 0 points = [] state = 'zoom1' draw_debug_messages(disp_frame, labels[state], font_size = 1.0) cv2.imshow(wnd_name, disp_frame) hsv_min = 255 * np.ones([3], dtype='uint8') hsv_max = np.zeros([3], dtype='uint8') color_strs = {\ 'r': 'red',\ 'g': 'green',\ 'b': 'blue',\ 'c': 'cyan',\ 'm': 'magenta',\ 'y': 'yellow',\ } contour_selected = False sub_state = None def debug_state(state, points, events): print 'State: %s, points: %s, mouse_events: %s' % (state, str(points), str(events)) while True: #debug_state(state, points, mouse_events) key = cv2.waitKey(5) if key == 27:#Esc break key_events[key] = True if key == ord('c') and state == 'hsv': hsv_min = 255 * np.ones([3], dtype='uint8') hsv_max = np.zeros([3], dtype='uint8') points = [] elif state == 'hsv_select_roi': if sub_state == 'cont_not_selected': if mouse_events[cv2.EVENT_LBUTTONDOWN]: sub_state = 'cont_select' elif sub_state == 'cont_select': zoomed_frame_contoured = zoomed_frame.copy() draw_contour(zoomed_frame_contoured, points) draw_debug_messages(zoomed_frame_contoured, labels[state], font_size = 1.0) cv2.imshow(wnd_name, zoomed_frame_contoured) if mouse_events[cv2.EVENT_LBUTTONUP]: sub_state = 'cont_selected' clear_events_mouse([cv2.EVENT_LBUTTONDOWN, cv2.EVENT_LBUTTONUP]) elif sub_state == 'cont_selected': if key_events[10]:#Enter clear_events_key([10]) #TODO: assert len(points) >= 2, otherwise switch to sub_state = 'cont_select', #but also make new clicks be processed (points.append at the end of cycle) state = 'hsv' elif mouse_events[cv2.EVENT_LBUTTONDOWN]: sub_state = 'cont_select' elif mouse_events[cv2.EVENT_RBUTTONUP]: points = [] elif state == 'hsv': cont = np.array([points], dtype='int32') #cont = np.array([[[1, 30]], [[30,1]], [[30, 30]], [[1, 1]]], dtype='int32') mask = np.zeros_like(zoomed_frame) cv2.drawContours(mask, cont, -1, SCALAR_WHITE, -1)#Draw contour with filled internals selected_frame = np.bitwise_and(zoomed_frame, mask) ignore_mask = np.array(SCALAR_BLACK[0:3], dtype=selected_frame.dtype) (mi, ma, _) = MinMaxAvgHSV(selected_frame, 0, 0, selected_frame.shape[0], selected_frame.shape[1], ignore_mask) for i in range(len(mi)): if mi[i] < hsv_min[i]: hsv_min[i] = mi[i] if ma[i] > hsv_max[i]: hsv_max[i] = ma[i] cv2.destroyWindow(wnd_name) while True: try: color = raw_input('colour [r, g, b, c, m, y]> ') color = color_strs[color] except KeyError, e: print 'Invalid color: %s. Avaliable colors are: r, g, b, c, m, y. Retry.' % color continue break f = file(fname, 'a+') f.write('%s_hsv_min:\n' % color) f.write(' h: %d\n' % hsv_min[0]) f.write(' s: %d\n' % hsv_min[1]) f.write(' v: %d\n' % hsv_min[2]) f.write('%s_hsv_max:\n' % color) f.write(' h: %d\n' % hsv_max[0]) f.write(' s: %d\n' % hsv_max[1]) f.write(' v: %d\n' % hsv_max[2]) f.close() print '%s written' % fname cv2.imwrite('params/%s.png' % color, selected_frame) print 'params/%s.png written' % color cv2.namedWindow(wnd_name, 0) cv2.setMouseCallback(wnd_name, on_mouse_event, 0) disp_frame = frame.copy() draw_debug_messages(disp_frame, labels[state], font_size = 1.0) cv2.imshow(wnd_name, disp_frame) state = 'zoom1' clear_events_mouse([cv2.EVENT_LBUTTONDOWN, cv2.EVENT_LBUTTONUP]) hsv_min = 255 * np.ones([3], dtype='uint8') hsv_max = np.zeros([3], dtype='uint8') points = [] elif state == 'zoom1': if len(points) == 1: state = 'zoom2'