def calc_histograms(directory): ''' calc histograms of images in directory. saves them into ./histograms/imgname_histogram.jpg :param directory: path to images (e.x.: 'D:\\PR aus Visual Computing\\Interestingness16data\\allvideos\\images\\interesting\\cropped') :return: ''' print('calc_histograms') imgNames = read_img_names(directory) for imgName in imgNames: img = read_img(os.path.join(directory, imgName)) color = ('b', 'g', 'r') for i, col in enumerate(color): histr = cv2.calcHist([img], [i], None, [256], [0, 256]) plt.plot(histr, color=col) plt.xlim([0, 256]) #plt.show() # save img if not os.path.exists(directory + '\\histograms\\'): os.makedirs(directory + '\\histograms\\') imgPath = directory + '\\histograms\\' + imgName.split( '.jpg')[0] + '_histogram.png' plt.savefig(imgPath)
def calc_histograms_bw(directory): ''' calc grayscale histograms of images in directory. saves them into ./histograms/imgname_histogram.jpg :param directory: path to images (e.x.: 'D:\\PR aus Visual Computing\\Interestingness16data\\allvideos\\images\\interesting\\cropped') :return: ''' print('calc_histograms_bw') imgNames = read_img_names(directory) for imgName in imgNames: img = read_img(os.path.join(directory, imgName)) gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) plt.hist(gray_image.ravel(), 256, [0, 256]) #plt.show() # save img if not os.path.exists(directory + '\\histograms\\grayscale\\'): os.makedirs(directory + '\\histograms\\grayscale\\') imgPath = directory + '\\histograms\\grayscale\\' + imgName.split( '.jpg')[0] + '_histogram.png' plt.savefig(imgPath) plt.clf()
def face_to_img_ratios_to_csv(directory, face_frontal_cascade, face_profile_cascade): ''' calculates the ratio between image height and face heights and writes them in a csv file. saves csv file at './face_img_ratios.csv' :param directory: path to images (e.x.: 'D:\\PR aus Visual Computing\\Interestingness16data\\allvideos\\images\\interesting\\cropped\\faces') :return: ''' imgNames = read_img_names(directory) csvfile = open(directory + '\\face_to_img_ratios.csv', 'wb') csvwriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_NONNUMERIC) csvwriter.writerow( ['shotname', 'faces_detected', 'heights (relative to img height)']) for imgName in imgNames: img = read_img(os.path.join(directory, imgName)) faces_height = calc_face_to_img_height_ratio(img, face_frontal_cascade, face_profile_cascade) csvwriter.writerow([ imgName, len(faces_height), ",".join(map(str, faces_height.values())) ]) csvfile.close()
def draw_phi_grid(directory): ''' draws phi grid into image. saves images in subdirectory ./phigrid :param directory: path to images (e.x.: 'D:\\PR aus Visual Computing\\Interestingness16data\\allvideos\\images\\interesting\\cropped') :return: ''' print("draw_phi_grid") imgNames = read_img_names(directory) for imgName in imgNames: img = read_img(os.path.join(directory, imgName)) img = draw_grid(img) #save img if not os.path.exists(directory + '\\phigrid\\'): os.makedirs(directory + '\\phigrid\\') imgPath = directory + '\\phigrid\\' + imgName success = cv2.imwrite(imgPath, img)
def calc_histograms_plus_img(directory): ''' calc histograms of images in directory. saves them beside image into ./histograms/combined/imgname_histogram.jpg :param directory: path to images (e.x.: 'D:\\PR aus Visual Computing\\Interestingness16data\\allvideos\\images\\interesting\\cropped') :return: ''' print('calc_histograms_plus_img') imgNames = read_img_names(directory) fig = plt.figure() for imgName in imgNames: img = read_img(os.path.join(directory, imgName)) a = fig.add_subplot(2, 1, 1) plt.imshow(cv2.cvtColor(img, cv2.COLOR_RGB2BGR)) a.set_title('Image') a = fig.add_subplot(2, 1, 2) color = ('b', 'g', 'r') for i, col in enumerate(color): histr = cv2.calcHist([img], [i], None, [256], [0, 256]) plt.plot(histr, color=col) plt.xlim([0, 256]) a.set_title('Histogram') #plt.show() # save img if not os.path.exists(directory + '\\histograms\\combined\\'): os.makedirs(directory + '\\histograms\\combined\\') imgPath = directory + '\\histograms\\combined\\' + imgName.split( '.jpg')[0] + '_histogram.png' plt.savefig(imgPath) plt.clf()
def selectTrainingAndTestData(dir_interesting_imgs, dir_uninteresting_imgs, output_dir_training, output_dir_test, interesting_training_samples=50, uninteresting_training_samples=500, interesting_test_samples=50, uninteresting_test_samples=500): """ selects randomly interesting and uninteresting imgs and stores them at output_directory :param dir_interesting_imgs: directory of all interesting images :param dir_uninteresting_imgs: directory of all uninteresting images :param output_dir_training: output directory for training data :param output_dir_test: output directory for test data :param interesting_training_samples: number of how much interesting imgs should be selected for training data set :param uninteresting_training_samples: number of how much uninteresting imgs should be selected for training data set :param interesting_test_samples: number of how much interesting imgs should be selected for test data set :param uninteresting_test_samples: number of how much uninteresting imgs should be selected for test data set :return: """ #select random img_names #interesting img_names_interesting = read_img_names(dir_interesting_imgs) total_sample_number_interesting = interesting_training_samples + interesting_test_samples rnd_interesting_img_names = random.sample(img_names_interesting, total_sample_number_interesting) rnd_interesting_img_names_training = rnd_interesting_img_names[: interesting_training_samples] rnd_interesting_img_names_test = rnd_interesting_img_names[ interesting_training_samples:total_sample_number_interesting] if not os.path.exists(os.path.join(output_dir_training, 'interesting')): os.makedirs(os.path.join(output_dir_training, 'interesting')) if not os.path.exists(os.path.join(output_dir_test, 'interesting')): os.makedirs(os.path.join(output_dir_test, 'interesting')) for img_name in rnd_interesting_img_names_training: copyfile(os.path.join(dir_interesting_imgs, img_name), os.path.join(output_dir_training, 'interesting', img_name)) for img_name in rnd_interesting_img_names_test: copyfile(os.path.join(dir_interesting_imgs, img_name), os.path.join(output_dir_test, 'interesting', img_name)) #uninteresting img_names_uninteresting = read_img_names(dir_uninteresting_imgs) total_sample_number_uninteresting = uninteresting_training_samples + uninteresting_test_samples rnd_uninteresting_img_names = random.sample( img_names_uninteresting, total_sample_number_uninteresting) rnd_uninteresting_img_names_training = rnd_uninteresting_img_names[: uninteresting_training_samples] rnd_uninteresting_img_names_test = rnd_uninteresting_img_names[ uninteresting_training_samples:total_sample_number_uninteresting] if not os.path.exists(os.path.join(output_dir_training, 'uninteresting')): os.makedirs(os.path.join(output_dir_training, 'uninteresting')) if not os.path.exists(os.path.join(output_dir_test, 'uninteresting')): os.makedirs(os.path.join(output_dir_test, 'uninteresting')) for img_name in rnd_uninteresting_img_names_training: copyfile(os.path.join(dir_uninteresting_imgs, img_name), os.path.join(output_dir_training, 'uninteresting', img_name)) for img_name in rnd_uninteresting_img_names_test: copyfile(os.path.join(dir_uninteresting_imgs, img_name), os.path.join(output_dir_test, 'uninteresting', img_name))