forked from vlachoudis/bCNC
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCamera.py
197 lines (174 loc) · 6.19 KB
/
Camera.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: ascii -*-
# $Id$
#
# Author: vvlachoudis@gmail.com
# Date: 24-Aug-2014
try:
import cv2 as cv
except ImportError:
cv = None
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
from PIL import Image, ImageTk
except ImportError:
cv = None
import Utils
#-------------------------------------------------------------------------------
def hasOpenCV(): return cv is not None
#===============================================================================
# Camera processing class
# A wrapper to opencv needed functions
#===============================================================================
class Camera:
#-----------------------------------------------------------------------
# prefix is the prefix to get configuration parameters from ini
#-----------------------------------------------------------------------
def __init__(self, prefix=""):
if cv is None: return
self.prefix = prefix
self.idx = Utils.getInt("Camera", prefix)
self.camera = None
self.image = None
self.frozen = None
self.imagetk = None
#-----------------------------------------------------------------------
def isOn(self):
if cv is None: return False
return self.camera is not None and self.camera.isOpened()
#-----------------------------------------------------------------------
def start(self):
if cv is None: return
self.camera = cv.VideoCapture(self.idx)
if self.camera is None: return
s, self.image = self.camera.read()
if not self.camera.isOpened():
self.stop()
return False
self.set()
return True
#-----------------------------------------------------------------------
def stop(self):
if cv is None or self.camera is None: return
self.camera.release()
# del self.camera
self.camera = None
#-----------------------------------------------------------------------
def set(self):
width = Utils.getInt("Camera", self.prefix+"_width", 0)
if width: self.camera.set(3, width)
height = Utils.getInt("Camera", self.prefix+"_height", 0)
if height: self.camera.set(4, height)
self.angle = Utils.getInt("Camera", self.prefix+"_angle")//90 % 4
# self.camera.set(38, 3) # CV_CAP_PROP_BUFFERSIZE
#-----------------------------------------------------------------------
# Read one image and rotated if needed
#-----------------------------------------------------------------------
def read(self):
s,self.image = self.camera.read()
if s:
self.image = self.rotate90(self.image)
else:
self.stop()
self.original = self.image
if self.frozen is not None:
self.image = cv.addWeighted(self.image, 0.7, self.frozen, 0.3, 0.0)
return s
#-----------------------------------------------------------------------
# Save image to file
#-----------------------------------------------------------------------
def save(self, filename):
cv.imwrite(filename, self.original)
#-----------------------------------------------------------------------
# Rotate image in steps of 90deg
#-----------------------------------------------------------------------
def rotate90(self, image):
if self.angle == 1: # 90 deg
return cv.transpose(cv.flip(image,1))
elif self.angle == 2: # 180 deg
return cv.flip(image,-1)
elif self.angle == 3: # 270 deg
return cv.flip(cv.transpose(image),1)
else:
return image
#-----------------------------------------------------------------------
# Resize image up to a maximum width,height
#-----------------------------------------------------------------------
def resize(self, factor, maxwidth, maxheight):
if factor==1.0: return
h,w = self.image.shape[:2]
wn = int(w*factor)
hn = int(h*factor)
if wn>maxwidth or hn>maxheight:
# crop the image to match max
wn = int(maxwidth/factor)//2
hn = int(maxheight/factor)//2
w2 = w//2
h2 = h//2
left = max(w2-wn,0)
right = min(w2+wn,w-1)
top = max(h2-hn,0)
bottom = min(h2+hn,h-1)
self.image = self.image[top:bottom,left:right]
try:
self.image = cv.resize(self.image, (0,0), fx=factor, fy=factor)
except:
# FIXME Too much zoom out, results in void image!
#self.image = None
pass
#-----------------------------------------------------------------------
# Canny edge detection
#-----------------------------------------------------------------------
def canny(self, threshold1, threshold2):
edge = cv.cvtColor(cv.Canny(self.image, threshold1, threshold2), cv.COLOR_GRAY2BGR)
self.image = cv.addWeighted(self.image, 0.9, edge, 0.5, 0.0)
#-----------------------------------------------------------------------
# Freeze and overlay image
#-----------------------------------------------------------------------
def freeze(self, f):
if f:
self.frozen = self.image.copy()
else:
self.frozen = None
#-----------------------------------------------------------------------
# Return center portion of the image to be used as a template
#-----------------------------------------------------------------------
def getCenterTemplate(self, r):
h,w = self.original.shape[:-1]
w2 = w//2
h2 = h//2
return self.original[h2-r:h2+r, w2-r:w2+r]
#-----------------------------------------------------------------------
# return location of matching template
#-----------------------------------------------------------------------
def matchTemplate(self, template):
method = cv.TM_CCOEFF_NORMED
res = cv.matchTemplate(self.original, template, method)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
h,w = self.original.shape[:-1]
w2 = w//2
h2 = h//2
h,w = template.shape[:-1]
r = w//2
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
if method in (cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED):
top_left = min_loc
else:
top_left = max_loc
#bottom_right = (top_left[0]+2*r, top_left[1]+2*r)
dx= w2-r - top_left[0]
dy= h2-r - top_left[1]
#cv.rectangle(img, top_left, bottom_right, 255, 2)
#print "Match=",dx,dy
return dx,dy
#-----------------------------------------------------------------------
# Convert to Tk image
#-----------------------------------------------------------------------
def toTk(self):
if self.image is None: return None
self.imagetk = ImageTk.PhotoImage(
image=Image.fromarray(
cv.cvtColor(self.image, cv.COLOR_BGR2RGB), "RGB"))
return self.imagetk