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()
Example #3
0
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()
Example #4
0
def _calc_features(img_dirs, feature_calculator, *args):
    #DEBUG
    # counter = 0
    #DEBUG END
    features = []
    for img_dir in img_dirs:
        img = read_img(img_dir)

        if args is None:
            feature = feature_calculator(img)
        else:
            feature = feature_calculator(img, *args)

        feature_files.save_features(img_dir, feature)
        features.append(feature)
        #DEBUG
        # print 'image: ' + str(counter)
        # counter = counter+1
        #DEBUG END
    return np.asarray(features)
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()
Example #7
0
def calc_features(img_dirs, feature_names):
    """
    calculates all features given by feature_names
    :param img_dirs: a list of image paths
    :type img_dirs: list
    :param feature_names: a list containing all feature names
    :type feature_names: list
    :return: dict (img_dir, features) features is also a dict (feature_name, feature vector)
    :rtype: dict
    """

    features = {}

    dir_haarfeatures = os.path.join(os.getcwd(), 'res', 'haarcascades')
    face_frontal_cascade = cv2.CascadeClassifier(os.path.join(dir_haarfeatures, 'haarcascade_frontalface_default.xml'))
    face_profile_cascade = cv2.CascadeClassifier(os.path.join(dir_haarfeatures, 'haarcascade_profileface.xml'))

    model = FasterRCNNVGG16(pretrained_model='voc07')

    for img_dir in img_dirs:
        print 'calc features for: ' + img_dir
        img = read_img(img_dir)
        features[img_dir] = dict() #init features dict for each image

        for name in feature_names:
            if TU_feature_file_exists(img_dir, name):
                continue

            if name == Features.Face_count or name == Features.Rot_distance or name == Features.Face_bb:
                face_count, rot_distance, face_bb = face_detection(img, face_frontal_cascade, face_profile_cascade)

                feature_files.save_features(img_dir, Features.Face_count, face_count)
                feature_files.save_features(img_dir, Features.Rot_distance, rot_distance)
                feature_files.save_features(img_dir, Features.Face_bb, face_bb)

                features[img_dir][Features.Face_count] = face_count
                features[img_dir][Features.Rot_distance] = rot_distance
                features[img_dir][Features.Face_bb] = face_bb

            elif name == Features.Face_bb_full_img:
                feature = _face_bb_full_img_calculator(img, face_frontal_cascade, face_profile_cascade)
                feature_files.save_features(img_dir, Features.Face_bb_full_img, feature)

                features[img_dir][Features.Face_bb_full_img] = feature

            elif name == Features.Face_bb_quarter_imgs:
                feature = _face_bb_quarter_imgs_calculator(img, face_frontal_cascade, face_profile_cascade)
                feature_files.save_features(img_dir, Features.Face_bb_quarter_imgs, feature)

                features[img_dir][Features.Face_bb_quarter_imgs] = feature

            elif name == Features.Face_bb_eighth_imgs:
                feature = _face_bb_eighth_imgs_calculator(img, face_frontal_cascade, face_profile_cascade)
                feature_files.save_features(img_dir, Features.Face_bb_eighth_imgs, feature)

                features[img_dir][Features.Face_bb_eighth_imgs] = feature

            elif name == Features.Tilted_edges:
                feature = _img_tilted_calculator(img)
                feature_files.save_features(img_dir, Features.Tilted_edges, feature)

                features[img_dir][Features.Tilted_edges] = feature

            elif name == Features.Edge_hist_v0:
                feature = _edge_hist_dir_calculator(img, False, False)
                feature_files.save_features(img_dir, Features.Edge_hist_v0, feature)

                features[img_dir][Features.Edge_hist_v0] = feature

            elif name == Features.Edge_hist_v1:
                feature = _edge_hist_dir_calculator(img, True, True)
                feature_files.save_features(img_dir, Features.Edge_hist_v1, feature)

                features[img_dir][Features.Edge_hist_v1] = feature

            elif name == Features.Edge_hist_v2:
                feature = _edge_hist_dir_calculator(img, True, False)
                feature_files.save_features(img_dir, Features.Edge_hist_v2, feature)

                features[img_dir][Features.Edge_hist_v2] = feature

            elif name == Features.Symmetry:
                feature = _symmetry_calculator(img, model)
                feature_files.save_features(img_dir, Features.Symmetry, feature)

                features[img_dir][Features.Symmetry] = feature

            # precalculated features
            elif name == Features.Hsv_hist:
                feature = load_precalc_feature(img_dir, Features.Hsv_hist)
                feature_files.save_features(img_dir, Features.Hsv_hist, feature)

                features[img_dir][Features.Hsv_hist] = feature

            elif name == Features.DenseSIFT_L0:
                feature = load_precalc_feature(img_dir, Features.DenseSIFT_L0)
                feature_files.save_features(img_dir, Features.DenseSIFT_L0, feature)

                features[img_dir][Features.DenseSIFT_L0] = feature

            elif name == Features.DenseSIFT_L1:
                feature = load_precalc_feature(img_dir, Features.DenseSIFT_L1)
                feature_files.save_features(img_dir, Features.DenseSIFT_L1, feature)

                features[img_dir][Features.DenseSIFT_L1] = feature

            elif name == Features.DenseSIFT_L2:
                feature = load_precalc_feature(img_dir, Features.DenseSIFT_L2)
                feature_files.save_features(img_dir, Features.DenseSIFT_L2, feature)

                features[img_dir][Features.DenseSIFT_L2] = feature

            elif name == Features.Hog_L0:
                feature = load_precalc_feature(img_dir, Features.Hog_L0)
                feature_files.save_features(img_dir, Features.Hog_L0, feature)

                features[img_dir][Features.Hog_L0] = feature

            elif name == Features.Hog_L1:
                feature = load_precalc_feature(img_dir, Features.Hog_L1)
                feature_files.save_features(img_dir, Features.Hog_L1, feature)

                features[img_dir][Features.Hog_L1] = feature

            elif name == Features.Hog_L2:
                feature = load_precalc_feature(img_dir, Features.Hog_L2)
                feature_files.save_features(img_dir, Features.Hog_L2, feature)

                features[img_dir][Features.Hog_L2] = feature

            elif name == Features.Lbp_L0:
                feature = load_precalc_feature(img_dir, Features.Lbp_L0)
                feature_files.save_features(img_dir, Features.Lbp_L0, feature)

                features[img_dir][Features.Lbp_L0] = feature

            elif name == Features.Lbp_L1:
                feature = load_precalc_feature(img_dir, Features.Lbp_L1)
                feature_files.save_features(img_dir, Features.Lbp_L1, feature)

                features[img_dir][Features.Lbp_L1] = feature

            elif name == Features.Lbp_L2:
                feature = load_precalc_feature(img_dir, Features.Lbp_L2)
                feature_files.save_features(img_dir, Features.Lbp_L2, feature)

                features[img_dir][Features.Lbp_L2] = feature

            elif name == Features.Gist:
                feature = load_precalc_feature(img_dir, Features.Gist)
                feature_files.save_features(img_dir, Features.Gist, feature)

                features[img_dir][Features.Gist] = feature

            elif name == Features.CNN_fc7:
                feature = load_precalc_feature(img_dir, Features.CNN_fc7)
                feature_files.save_features(img_dir, Features.CNN_fc7, feature)

                features[img_dir][Features.CNN_fc7] = feature

            elif name == Features.CNN_prob:
                feature = load_precalc_feature(img_dir, Features.CNN_prob)
                feature_files.save_features(img_dir, Features.CNN_prob, feature)

                features[img_dir][Features.CNN_prob] = feature

            else:
                raise NotImplementedError

    return features
def crop_black_borders(img_dirs):
    """
    crops black borders around image. replaces image with the modified image.
    :param img_dirs: a list containing all image paths
    :type img_dirs: list
    :return:
    :rtype:
    """
    for img_dir in img_dirs:
        img = read_img(img_dir)

        img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        #sharpen image
        kernel_sharpen_1 = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
        img_gray = cv2.filter2D(img_gray, -1, kernel_sharpen_1)

        edges = cv2.Canny(img_gray, 1, 1)

        #morphological closing
        kernel = np.ones((5, 5), np.uint8)
        closing = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
        for i in range(100):
            closing = cv2.morphologyEx(closing, cv2.MORPH_CLOSE, kernel)


        #check if border contains white pixels (contour)

        contours_pixel_cnt = cv2.countNonZero(closing)
        if contours_pixel_cnt > 0:
            height, width, channels = img.shape

            crop = False #is true if img should be cropped

            row_idx_top, row_idx_bottom = find_horizontal_img_border(closing)
            if row_idx_top != 0 and row_idx_bottom != height:
                crop = True
                #cv2.line(img, (0, row_idx_top), (width, row_idx_top), (0, 0, 255), 3)
                #cv2.line(img, (0, row_idx_bottom), (width, row_idx_bottom), (0, 0, 255), 3)

            col_idx_left, col_idx_right = find_vertical_img_border(closing)
            if col_idx_left != 0 and col_idx_right != width:
                crop = True
                #cv2.line(img, (col_idx_left, 0), (col_idx_left, height), (0, 0, 255), 3)
                #cv2.line(img, (col_idx_right, 0), (col_idx_right, height), (0, 0, 255), 3)

        #crop image if necessary
        if crop:
            img = img[row_idx_top:row_idx_bottom, col_idx_left:col_idx_right]

        # save img
        '''
        if not os.path.exists(directory + '\\cropped\\'):
            os.makedirs(directory + '\\cropped\\')

        imgPath = directory + '\\cropped\\' + imgName
        success = cv2.imwrite(imgPath, img)
        '''
        success = cv2.imwrite(img_dir, img)

    '''