-
Notifications
You must be signed in to change notification settings - Fork 20
/
Preprocessing.py
66 lines (56 loc) · 2.02 KB
/
Preprocessing.py
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
59
60
61
62
63
64
65
66
import numpy as np
from tqdm import tqdm
import cv2
import os
import imutils
def crop_img(img):
"""
Finds the extreme points on the image and crops the rectangular out of them
"""
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray = cv2.GaussianBlur(gray, (3, 3), 0)
# threshold the image, then perform a series of erosions +
# dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
# find contours in thresholded image, then grab the largest one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# find the extreme points
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
ADD_PIXELS = 0
new_img = img[extTop[1]-ADD_PIXELS:extBot[1]+ADD_PIXELS, extLeft[0]-ADD_PIXELS:extRight[0]+ADD_PIXELS].copy()
return new_img
if __name__ == "__main__":
training = "dataset/Training"
testing = "dataset/Testing"
training_dir = os.listdir(training)
testing_dir = os.listdir(testing)
IMG_SIZE = 256
for dir in training_dir:
save_path = 'cleaned/Training/'+ dir
path = os.path.join(training,dir)
image_dir = os.listdir(path)
for img in image_dir:
image = cv2.imread(os.path.join(path,img))
new_img = crop_img(image)
new_img = cv2.resize(new_img,(IMG_SIZE,IMG_SIZE))
if not os.path.exists(save_path):
os.makedirs(save_path)
cv2.imwrite(save_path+'/'+img, new_img)
for dir in testing_dir:
save_path = 'cleaned/Testing/'+ dir
path = os.path.join(testing,dir)
image_dir = os.listdir(path)
for img in image_dir:
image = cv2.imread(os.path.join(path,img))
new_img = crop_img(image)
new_img = cv2.resize(new_img,(IMG_SIZE,IMG_SIZE))
if not os.path.exists(save_path):
os.makedirs(save_path)
cv2.imwrite(save_path+'/'+img, new_img)