def extract_hsv_features(ifile, pfile): """ Given img file, and patched img file, return hue-sat 2Dhist and val hist for each patch (10x10 + 10 = 110 dim feats) """ img = get_RGB(ifile) p_img = get_RGB(pfile) hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) pi = Image.open(pfile) n_patches = defaultdict(int) for pixel in pi.getdata(): n_patches[pixel] += 1 if n_patches is None: print("\nNo patches:", pfile) return None hsv_feats = [] patch_colors = [] for p in n_patches: patch_pixels = np.where((p_img == p).all(axis=2)) hsv_values = hsv_img[patch_pixels[0], patch_pixels[1], :] hs, _, _ = np.histogram2d(hsv_values[:, 0], hsv_values[:, 1], [10, 10]) v, _ = np.histogram(hsv_values[:, 2], bins=10) hist_fea = np.concatenate((hs.reshape(100, ), v)) hsv_feats.append(hist_fea) patch_colors.append(p) return np.asarray(hsv_feats), patch_colors
def get_gt_label_info(fid_box, fid_obj): """ Ground truth patch color (RGB) to label mapping """ gt_files = os.listdir(SEG_DIR) gp_label = defaultdict(int) for gt_file in gt_files: if gt_file[-4:] != EXT: continue fid = gt_file.split(".")[0] gt_file = SEG_DIR + gt_file # 1. read the ground truth segmented file gt_patches = get_patch_info(gt_file) g_img = get_RGB(gt_file) for gp in gt_patches: if gp == CREAM: continue elif gp == BGR: label = BGR_LABEL else: gt_pixels = np.where((g_img == gp).all(axis=2)) box = fid_box[fid] labs = fid_obj[fid] label = get_me_the_label(gt_pixels, box, labs) if label is not None: gp_label[gp] = label return gp_label
def parallel_data_prep_svm(lst_fids): """ Save label information for each patch at each scale level. Parallel execution """ info_dir = FEAT_DIR + "patch_label_info/" patch_subd = os.listdir(PATCH_DIR) for fid in lst_fids: patch_label_lst = [] for pd in patch_subd: pfile = PATCH_DIR + pd + "/" + fid + EXT p_part = "/".join(pfile.replace(EXT, "").split("/")[-2:]) p_img = get_RGB(pfile) n_patches = get_patch_info(pfile) for p in n_patches: patch_pixels = np.where((p_img == p).all(axis=2)) # find out the label of this patch_pixels # using manual segmentation info patch_label = find_overlap_with_gt(patch_pixels, GP_LABEL, fid) patch_label_lst.append([patch_label, p, p_part]) # print(patch_label, p, p_part) pickle.dump(patch_label_lst, open(info_dir + fid + ".pkl", "wb"))
def find_overlap(p1, p2_info, s1_ifile, s2_ifile): s1_img = get_RGB(s1_ifile) s2_img = get_RGB(s2_ifile) p1_pixels = np.where((s1_img == p1).all(axis=2)) p1_xy = [xy for xy in zip(p1_pixels[0], p1_pixels[1])] max_cnt = 0 max_overlap_patch = None for p2 in p2_info: p2_pixels = np.where((s2_img == p2).all(axis=2)) p2_xy = [xy for xy in zip(p2_pixels[0], p2_pixels[1])] cnt = len(set(p1_xy) & set(p2_xy)) if cnt > max_cnt: max_overlap_patch = p2 max_cnt = cnt return max_cnt, max_overlap_patch
def compute_patch_sift_hist(ifile, pfile, f_c_ixs): """ Given img file, patched img file and corresponding VQ indices of the keypoints, return histogram features """ def is_kp_in_patch(pp, kp_pt): """ Check if key point is in patch """ flag = False x_min = pp[0].min() x_max = pp[0].max() y_min = pp[1].min() y_max = pp[1].max() if kp_pt[0] >= x_min and kp_pt[0] <= x_max: if kp_pt[1] >= y_min and kp_pt[1] <= y_max: flag = True return flag fid = os.path.splitext(os.path.basename(ifile))[0] kpts = pickle.load(open(FEAT_DIR + "kp/kp_" + fid + ".pkl", "rb")) p_img = get_RGB(pfile) pi = PIL.Image.open(pfile) n_patches = defaultdict(int) for pixel in pi.getdata(): n_patches[pixel] += 1 if n_patches is None: print("\nNo patches:", pfile) return None hist_feats = [] patch_colors = [] for p in n_patches: patch_pixels = np.where((p_img == p).all(axis=2)) patch_cixs = [] for kp_ix, kp in enumerate(kpts): if is_kp_in_patch(patch_pixels, kp): patch_cixs.append(f_c_ixs[kp_ix]) # print(patch_cixs) if len(patch_cixs) > 0: # print(patch_cixs) h, _ = np.histogram(patch_cixs, bins=np.arange(0, 1001)) # print(h.shape) # print(h) # break hist_feats.append(h) patch_colors.append(p) # break hist_feats = np.asarray(hist_feats) # print(hist_feats.shape, np.count_nonzero(hist_feats)) return hist_feats, patch_colors
def find_overlap_with_gt(pt_pixels, gp_label, fid): """ Find overlap of unsupervised patch with the ground truth patch """ patch_label = BGR_LABEL pt_xy = [(x, y) for x, y in zip(pt_pixels[0], pt_pixels[1])] gt_file = SEG_DIR + fid + EXT # 1. read the ground truth segmented file gt_patches = get_patch_info(gt_file) g_img = get_RGB(gt_file) for i, gp in enumerate(gt_patches): gt_pixels = np.where((g_img == gp).all(axis=2)) gt_xy = [xy for xy in zip(gt_pixels[0], gt_pixels[1])] cnt = len(set(pt_xy) & set(gt_xy)) if cnt >= (len(pt_xy) * 0.75): if gp == CREAM: patch_label = None elif gp == BGR: patch_label = BGR_LABEL else: patch_label = gp_label[gp] break # if > 75 overlap is found, return the label else: if gp == CREAM: patch_label = BGR_LABEL else: patch_label = int(-1 * gp_label[gp]) return patch_label