-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageOperations.py
More file actions
58 lines (45 loc) · 1.52 KB
/
ImageOperations.py
File metadata and controls
58 lines (45 loc) · 1.52 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import cv2
import numpy as np
class ImageOperations:
@staticmethod
def convert_to_gray(image):
image = image.copy()
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@staticmethod
def get_shape(image):
if len(image.shape) > 2:
height, width, _ = image.shape
return height, width
else:
height, width = image.shape
return height, width
@staticmethod
def morph_open_erode_close_image(image):
image = image.copy()
kernel = np.ones((5, 5), np.uint8)
_, mask = cv2.threshold(image, 107, 255, cv2.THRESH_BINARY_INV)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.erode(mask, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
return mask
@staticmethod
def read_image(filename):
return cv2.imread(filename)
@staticmethod
def color_threshold(image, threshold = 95):
image_copied = image.copy()
rows, cols = image_copied.shape
for i in range(rows):
for j in range(cols):
pixel_value = image_copied[i, j]
if pixel_value <= threshold:
image_copied[i, j] = 0
return image_copied
@staticmethod
def resize_image(image):
width, height, _ = image.shape
return image[20:width - 50, 0:height - 50]
@staticmethod
def convert_to_gray(image):
image = image.copy()
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)