File size: 1,007 Bytes
2b32b36
 
775d79d
f490375
775d79d
f490375
3e8f8f6
 
 
 
 
f490375
 
3e8f8f6
775d79d
 
 
 
 
 
 
 
 
 
2969573
 
 
2b32b36
 
 
 
54cb6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os

import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np

def show_image(image, cmap = None):
    if cmap:
        plt.imshow(image, cmap = cmap)
    else:
        plt.imshow(image)
    plt.axis('off')
    plt.show()

def draw_contours(contour, image_width, image_height):
    contours_image = np.ones((image_width, image_height), dtype = np.uint8)
    if type(contour) == tuple:
        # If the contour is a tuple, it is a tuple of multiple contours
        cv.drawContours(contours_image, contour, -1, (0,255,0), 4)
    else:
        # Else, it is a single contour
        cv.drawContours(contours_image, [contour], 0, (0,255,0), 4)
    show_image(contours_image, cmap = 'gray')

def save_image(image, filename, convert_to_bgr = True):
    if convert_to_bgr:
        image = cv.cvtColor(image.astype(np.uint8), cv.COLOR_RGB2BGR)
    
    directory = os.path.dirname(filename)
    if not os.path.exists(directory):
        os.makedirs(directory)
    cv.imwrite(filename, image)