Repository navigation
Expand file tree
/
Copy pathimage_to_array.py
More file actions
executable file
·64 lines (50 loc) · 1.59 KB
/
Copy pathimage_to_array.py
File metadata and controls
executable file
·64 lines (50 loc) · 1.59 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
59
60
61
62
63
64
#!/usr/bin/env python3
import sys, os
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Validate inputs
if (len(sys.argv) < 3):
print("Usage: image_to_array.py <image> <process>")
exit(0)
# Load params
path = sys.argv[1]
process = sys.argv[2]
preview = False
if (len(sys.argv) == 4 and sys.argv[3] == "-preview"): preview = True
# Process image
def processImage(img, p):
if (p == "gray"): res = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Gray
elif (p == "laplacian"): res = cv2.Laplacian(img, cv2.CV_64F) # Laplacian
elif (p == "sobelx"): res = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) # Sobel X
elif (p == "sobely"): res = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) # Sobel Y
elif (p == "canny"): res = cv2.Canny(img, 100, 200) # Canny Edges
elif (p == "threshold"): x, res = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
return res
# Grayscale
def toGray(img):
temp = np.uint8(outImage)
img = cv2.cvtColor(temp, cv2.COLOR_HSV2BGR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return img
# Resize Image
def resizeImage(img):
return cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
# View image
def viewImage(img):
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Load image
img = cv2.imread(path)
outImage = processImage(img, process)
# Convert to grayscale if color
if (len(outImage.shape) > 2 and outImage.shape[2] == 3):
outImage = toGray(outImage)
if (preview):
viewImage(outImage)
else:
# Save dataset
dataset = outImage.ravel().astype(int)
datasetStr = ",".join(dataset.astype(str))
print(datasetStr)