-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptive_thresholding.py
37 lines (27 loc) · 1.01 KB
/
adaptive_thresholding.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
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
import imutils
path='data/collected_image'
output='data/'
for count,img_path in enumerate(os.listdir(path)):
img=cv2.imread(os.path.join(path,img_path), cv2.IMREAD_GRAYSCALE)
img=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,25,6)
# _,img=cv2.threshold(img,160,250,cv2.THRESH_BINARY)
# # resize without losing aspect ratio
if img.shape[0]>img.shape[1]:
img=imutils.resize(img, height=320)
matrix=np.ones((320,320-img.shape[1]))*255
img=np.hstack((img,matrix))
else:
img=imutils.resize(img, width=320)
matrix=np.ones((320-img.shape[0],320))*255
img=np.vstack((img,matrix))
# img=cv2.resize(img,size,interpolation = cv2.INTER_AREA)
# # fill missing values by white pixels to make sure image size is 320x320
# img=img-255
# img.resize((320,320))
# img=img+255
#save img
cv2.imwrite(os.path.join(output,img_path),img)