This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfunctions.py
130 lines (98 loc) · 3.9 KB
/
functions.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import cv2
import numpy as np
IMAGE_SIZE = 32
def list_folders(root_folder):
"""Function to get subdir list"""
folder_list = []
for folder in sorted(os.listdir(root_folder)):
if os.path.isdir(os.path.join(root_folder, folder)):
folder_list.append(folder)
return folder_list
def create_folders(root_folder, folder_list):
"""Function to create folders in new dataset"""
for folder in folder_list:
os.makedirs(os.path.join(root_folder, folder), exist_ok=True)
def read_transparent_png(filename):
"""
Change transparent bg to white
"""
image_4channel = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
alpha_channel = image_4channel[:, :, 3]
rgb_channels = image_4channel[:, :, :3]
# White Background Image
white_background_image = np.ones_like(rgb_channels, dtype=np.uint8) * 255
# Alpha factor
alpha_factor = alpha_channel[:, :, np.newaxis].astype(np.float32) / 255.0
alpha_factor = np.concatenate((alpha_factor, alpha_factor, alpha_factor), axis=2)
# Transparent Image Rendered on White Background
base = rgb_channels.astype(np.float32) * alpha_factor
white = white_background_image.astype(np.float32) * (1 - alpha_factor)
final_image = base + white
return final_image.astype(np.uint8)
def clean(img):
"""Process an image"""
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(__, img_bw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
ctrs, __ = cv2.findContours(img_bw.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# take largest contour
ctr = sorted(ctrs, key=lambda ctr: (cv2.boundingRect(ctr)[2] * cv2.boundingRect(ctr)[3]),
reverse=True)[0]
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)
# Getting ROI
roi = img_bw[y:y + h, x:x + w]
return skeletize(crop(roi, IMAGE_SIZE))
def crop(image, desired_size):
"""Crop and pad to req size"""
old_size = image.shape[:2] # old_size is in (height, width) format
ratio = float(desired_size) / max(old_size)
new_size = tuple([int(x * ratio) for x in old_size])
# new_size should be in (width, height) format
im = cv2.resize(image, (new_size[1], new_size[0]))
delta_w = desired_size - new_size[1]
delta_h = desired_size - new_size[0]
top, bottom = delta_h // 2, delta_h - (delta_h // 2)
left, right = delta_w // 2, delta_w - (delta_w // 2)
color = [0, 0, 0]
new_im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT,
value=color)
return new_im
def process_folder(folder):
"""Process all images in a folder"""
extension = '.png'
new_list = []
for img in sorted(os.listdir(folder)):
if img.endswith(extension):
try:
image = read_transparent_png(os.path.join(folder, img))
new_img = clean(image)
new_list.append([img, new_img])
except:
print("\t" + img)
return new_list
def save_new(folder, imglist):
"""Save newly created images"""
for img in imglist:
cv2.imwrite(os.path.join(folder, img[0]), img[1])
def process_images(raw_folder, clean_folder, folder_list):
"""Process the images"""
for folder in folder_list:
print(folder)
imglist = process_folder(os.path.join(raw_folder, folder, 'output'))
save_new(os.path.join(clean_folder, folder), imglist)
def skeletize(img):
size = np.size(img)
skel = np.zeros(img.shape, np.uint8)
element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
done = False
while not done:
eroded = cv2.erode(img, element)
temp = cv2.dilate(eroded, element)
temp = cv2.subtract(img, temp)
skel = cv2.bitwise_or(skel, temp)
img = eroded.copy()
zeroes = size - cv2.countNonZero(img)
if zeroes == size:
done = True
return skel