Ejemplo n.º 1
0
        print(f'Area: {area}, Perimeter:{perimeter}')

    return objects


if __name__ == '__main__':
    try:
        edge_filter_dict = {
            'sobel': apply_sobel,
            'prewitt': apply_prewitt,
            'canny': apply_canny,
        }
        filter_func = edge_filter_dict.get(sys.argv[1])

        if len(sys.argv) < 3 and filter_func:
            start_cv_video(img_filter=filter_func)

        elif len(sys.argv) < 4 and filter_func:
            img_path = sys.argv[2]
            img = cv2.imread(img_path, cv2.IMREAD_COLOR)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            new_img = filter_func(img)
            plt_show_img(new_img)

        else:
            raise Exception('Method not implemented')

    except Exception as error:
        print(error)
        print(
            'Usage: python3 edge_detection[canny | sobel | prewitt] [file_path]'
        for (x, y, r) in circles:
            # draw the circle in the output image, then draw a rectangle
            # corresponding to the center of the circle
            cv2.circle(output, (x, y), r, RGB_GREEN, 4)
            cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), RGB_GREEN, -1)
    
    return output

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Emoji segmentation')
    parser.add_argument('-i', '--image', type=str, action='store', dest='src_img', help='The image to apply the filter')
    parser.add_argument('-ks', '--kernel_size', type=int, default=5, action='store', dest='kernel_size', help='The size of the kernel when applying dilatation or erotion')
    parser.add_argument('-d', '--dilate', type=int, default=0, action='store', dest='dilate_iter', help='Number of times to apply the Dilate operation')
    parser.add_argument('-e', '--erode', type=int, default=0, action='store', dest='erode_iter', help='Number of times to apply the Erode operation')
    args = parser.parse_args()

    if args.src_img:
        try:
            img = cv2.imread(args.src_img, cv2.IMREAD_COLOR)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            rgb_img = filter(img)
            plt_show_img(rgb_img)

        except Exception as error:
            print(error)

    else:
        start_cv_video(0, filter)

def increase_sat(img):
    img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV).astype('float32')
    h, s, v = cv2.split(img)
    s = s + 200
    s = np.clip(s, 0, 255)
    imghsv = cv2.merge((h, s, v))
    imghsv = cv2.cvtColor(imghsv.astype('uint8'), cv2.COLOR_HSV2RGB)

    return imghsv


if __name__ == '__main__':
    try:
        alpha = float(input('* Enter the contrast value [1.0 to 3.0]: '))
        beta = int(input('* Enter the brightness value [-255 to 255]: '))

        if len(sys.argv) < 2:
            start_cv_video(0, apply_contrast_brightness, alpha, beta)

        else:
            img_path = sys.argv[1]
            img = cv2.imread(img_path, cv2.IMREAD_COLOR)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            new_img = apply_contrast_brightness(img, alpha, beta)
            plt_show_img(new_img)

    except Exception as error:
        print(error)
Ejemplo n.º 4
0
        erode_iter = int(params[2]) if len(params) > 2 else 0

        kernel = np.ones((kernel_size,kernel_size), np.uint8)
        mask = cv2.dilate(mask, kernel, iterations=dilate_iter)
        mask = cv2.erode(mask, kernel, iterations=erode_iter) 
   
    return img * mask

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='RGB filter')
    parser.add_argument('-i', '--image', type=str, action='store', dest='src_img', help='The image to apply the filter')
    parser.add_argument('-ks', '--kernel_size', type=int, default=5, action='store', dest='kernel_size', help='The size of the kernel when applying dilatation or erotion')
    parser.add_argument('-d', '--dilate', type=int, default=0, action='store', dest='dilate_iter', help='Number of times to apply the Dilate operation')
    parser.add_argument('-e', '--erode', type=int, default=0, action='store', dest='erode_iter', help='Number of times to apply the Erode operation')
    args = parser.parse_args()

    if args.src_img:
        try:
            img = cv2.imread(args.src_img, cv2.IMREAD_COLOR)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            rgb_img = rgb_filter(img, args.kernel_size, args.dilate_iter, args.erode_iter)
            plt_show_img(rgb_img)

        except Exception as error:
            print(error)

    else:
        start_cv_video(0, rgb_filter, args.kernel_size, args.dilate_iter, args.erode_iter)
def slow_binarization(img, threshold=127):
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    height, width = img.shape
    binary = np.zeros([height, width, 1], 'uint8')

    for row in range(0, height):
        for col in range(0, width):
            if img[row][col] > threshold:
                binary[row][col] = 255

    return binary


if __name__ == '__main__':
    if len(sys.argv) < 2:
        start_cv_video(img_filter=otsu_binarization)

    else:
        try:
            img_path = sys.argv[1]
            img = cv2.imread(img_path, cv2.IMREAD_COLOR)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            binarized_img = otsu_binarization(img)
            plt_show_img(binarized_img)

            if (sys.argv[2] == '-h'):
                plt_hist(img.ravel(), 'Color Histogram')

        except Exception as error:
            print(error)
Ejemplo n.º 6
0
                        type=str,
                        action='store',
                        dest='train_img',
                        required=True,
                        help='The image to apply the filter')
    parser.add_argument('-test',
                        type=str,
                        action='store',
                        dest='test_img',
                        help='The image to test')
    args = parser.parse_args()

    if args.test_img:
        try:
            img1 = cv2.imread(args.train_img, cv2.IMREAD_COLOR)
            img2 = cv2.imread(args.test_img, cv2.IMREAD_COLOR)

            new_img = feature_match(img1, img2)
            new_img = cv2.cvtColor(new_img, cv2.COLOR_RGB2BGR)
            plt_show_img(new_img)

        except Exception as error:
            print(error)

    else:
        img = cv2.imread(args.train_img, cv2.IMREAD_GRAYSCALE)
        img = process_train_image(img)
        points, desc = ORB_descriptor(img)

        start_cv_video(0, feature_track, points, desc, img)