-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenCVExtended.py
152 lines (130 loc) · 5.9 KB
/
OpenCVExtended.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import cv2 as cv # OpenCV
import numpy as np # NumPy
def ImagePhalanx (images,linewidthbetween=0,bgcol=(0,0,0)):
'''
Creates a composite image using a list of image objects, with an optional line space in between and background colour. Horizontal arrangement.
'''
if len (images) <= 0:
print "ImagePhalanx was given an empty list!"
return np.zeros((1,1,3),np.uint8)
else:
maxHeight = max([i.shape[0] for i in images])
totalWidth = sum([i.shape[1] for i in images]) + linewidthbetween * (len(images) - 1)
returnImage = np.zeros((maxHeight,totalWidth,3),np.uint8)
returnImage[:] = bgcol
runningWidth = 0
for i in range(len(images)):
img = images[i] if images[i].ndim == 3 else cv.cvtColor(images[i],cv.COLOR_GRAY2BGR)
currH, currW = img.shape[:2]
returnImage[0:currH,runningWidth:runningWidth+currW] = img
runningWidth += currW + linewidthbetween
return returnImage
def ImageVertigo (images,linewidthbetween=0,bgcol=(0,0,0)):
'''
Creates a composite image using a list of image objects, with an optional line space in between and background colour. Vertical arrangement.
'''
if len (images) <= 0:
print "ImageVertigo was given an empty list!"
return np.zeros((1,1,3),np.uint8)
else:
maxWidth = max([i.shape[1] for i in images])
totalHeight = sum([i.shape[0] for i in images]) + linewidthbetween * (len(images) - 1)
returnImage = np.zeros((totalHeight,maxWidth,3),np.uint8)
returnImage[:] = bgcol
runningHeight = 0
for i in range(len(images)):
img = images[i] if images[i].ndim == 3 else cv.cvtColor(images[i],cv.COLOR_GRAY2BGR)
currH, currW = img.shape[:2]
returnImage[runningHeight:runningHeight+currH,0:currW] = img
runningHeight += currH + linewidthbetween
return returnImage
def ImageCatalog (images,imagesPerRow= 0,linewidthbetween=0,bgcol=(0,0,0)):
'''
Creates a composite image using a list of image objects, with an optional line space in between and background colour. Grid arrangement.
'''
if len (images) <= 0:
print "ImageCatalog was given an empty list!"
return np.zeros((1,1,3),np.uint8)
else:
imagesPerRow = int( len (images) / 2 ) if (imagesPerRow < 1) else imagesPerRow
finalToBeVertigo = []
runningPool = []
for idx,img in enumerate(images):
if ( idx == (len(images)-1) ) or ( (idx+1) % imagesPerRow == 0 ):
runningPool.append(img)
finalToBeVertigo.append(ImagePhalanx(runningPool, linewidthbetween, bgcol))
del runningPool [:]
else:
runningPool.append(img)
return ImageVertigo(finalToBeVertigo, linewidthbetween, bgcol)
def ImageNaming (image, text, text_scale= 1.0,color = (255,255,255)):
'''
Prints the given text on top of the an image.
'''
boxpadding = int(np.ceil((20.0) * (text_scale / 1.0)))
h, w = image.shape[:2]
h2 = h+boxpadding
returnImage = np.zeros((h2,w,3),np.uint8)
img = image if image.ndim == 3 else cv.cvtColor(image,cv.COLOR_GRAY2BGR)
returnImage[boxpadding:h2,0:w] = img
cv.putText(returnImage,text,(3,int(boxpadding-5)),cv.FONT_HERSHEY_PLAIN,text_scale,color)
return returnImage
def NamedImageCatalog (imagesWithNames,imagesPerRow= -1,linewidthbetween=0,bgcol=(0,0,0),text_scale=1.0,textcol = (255,255,255)):
'''
Creates a composite image using a list of image objects, with an optional line space in between and background colour. Grid arrangement withe very frame named.
'''
if len(imagesWithNames) == 0 or len(imagesWithNames[0]) != 2:
print "NamedImageCatalog was either an empty imagesWithNames list or invalid pairings!"
return np.zeros((1,1,3),np.uint8)
else:
toBeCataloged = []
for image,name in imagesWithNames:
toBeCataloged.append(ImageNaming(image,name,text_scale,textcol))
return ImageCatalog(toBeCataloged, imagesPerRow, linewidthbetween, bgcol)
def RescaleImage (image,factor,method = cv.INTER_CUBIC ):
'''
Resizes an image by a factor
'''
return cv.resize(image,(int(image.shape[1]*factor*1.0),int(image.shape[0]*factor*1.0) ),0,0,interpolation=method)
def RescaleImageToHeight (image,height, method = cv.INTER_CUBIC):
'''
Resizes an image to a given height, keeping its aspect ratio locked
'''
heightRatio = 1.0 * height / image.shape[0]
return cv.resize(image,(int(image.shape[1]*heightRatio*1.0),int(image.shape[0]*heightRatio*1.0) ),0,0,interpolation=method)
def RescaleImageToHeightWidth (image,height,width,method = cv.INTER_CUBIC):
'''
Resized an image to a given height and width, regardless of its aspect ratio
'''
return cv.resize(image,(width,height),0,0,interpolation=method)
def CenterPointOfContour(theContour):
'''
Attempts to find the center point of a given contour, if it fails then it returns (-1,-1)
'''
try:
M = cv.moments(theContour)
return int(M['m10']/M['m00']), int(M['m01']/M['m00'])
except:
return -1,-1
def DebugPointer (image):
'''
Displays the image in a window and then ends the program afterwards
'''
cv.imshow("Debug out",ImagePhalanx([image]))
cv.waitKey(0)
cv.destroyAllWindows()
quit()
def RescaleAllImagesToHeight(images,height,method = cv.INTER_CUBIC):
return [RescaleImageToHeight(img, height,method) for img in images]
def SimpleWebcamFeed(src=0,debugText=False):
cam = cv.VideoCapture(src)
ret, frame = cam.read()
while ret:
ret, frame = cam.read()
if debugText:
frame = ImageNaming(frame, "Press Q to exit", 0.8)
cv.imshow("Simple Window",frame)
k = cv.waitKey(1)
if k == ord('q'):
break
cv.destroyWindow("Simple Window")