def cumulative_hist(dir, n_bins=[255, 255, 255], type='lab', mask=None): files = Utils.get_files_from_dir_and_subdirs(dir) if mask is None or cv2.countNonZero(mask) == 0: mask = np.ones((img.shape[0], img.shape[1]), dtype=np.uint8) if mask.shape[0] != img.shape[0] or mask.shape[1] != img.shape[1]: print "mask shape: " + str(mask.shape) print "image shape: " + str(img.shape) print str(mask.shape[0] / float(mask.shape[1])) + ',' + str( img.shape[0] / float(img.shape[1])) raise ValueError('trouble with mask size, resetting to image size') n_pixels = cv2.countNonZero(mask) for file in files: img = cv2.imread(file) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # OpenCV uses H: 0 - 180, S: 0 - 255, V: 0 - 255 # histograms hist_hue = cv2.calcHist([hsv], [0], mask, [bins[0]], [0, 180]) hist_hue = [item for sublist in hist_hue for item in sublist] # flatten nested hist_hue = np.divide(hist_hue, n_pixels) hist_sat = cv2.calcHist([hsv], [1], mask, [bins[1]], [0, 255]) hist_sat = [item for sublist in hist_sat for item in sublist] hist_sat = np.divide(hist_sat, n_pixels) hist_int = cv2.calcHist([hsv], [2], mask, [bins[2]], [0, 255]) hist_int = [item for sublist in hist_int for item in sublist] # flatten nested list hist_int = np.divide(hist_int, n_pixels)
def caffe_filelist(path): types = ['bubba_kush', 'hawg', 'hindu_kush', 'platinum_og2'] file_list = Utils.get_files_from_dir_and_subdirs(path) print('n_files:'+str(len(file_list))) for file_n in file_list: print('file:'+str(file_n)) if 'cropped' in file_n: print('got :'+str(file_n)) mpath=os.path.split(file_n)[0] containing_folder=os.path.split(mpath)[0] print('mpath {0} containing {1}'.format(mpath,containing_folder)) # tail=os.path.split[1] file_class = None for i in range(0,len(types)): if types[i] in file_n: file_class = i if file_class is not None: print('{0} is class {1} :'.format(file_n,file_class))
def batch_crop(path): file_list = Utils.get_files_from_dir_and_subdirs(path) print('n_files:'+str(len(file_list))) #print('filelist:'+str(file_list)) bb = [350,150,450,550] for file_n in file_list: # if not '_cropped' in file_n: if not 0 : #skip the skipping print('file:'+str(file_n)) img_arr = cv2.imread(file_n) cropped = crop(img_arr,bb) name=file_n.split('.')[0] suffix=file_n.split('.')[1] name = name+'_cropped.'+suffix print('newname:'+str(name)) cv2.imwrite(name,cropped) else: print('skipping '+str(file_n)+', already done')