forked from henrikmidtiby/MarkerLocator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageAnalyzer.py
68 lines (56 loc) · 2.59 KB
/
ImageAnalyzer.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
# -*- coding: utf-8 -*-
"""
Image analyzer class for talking with the MarkerTracker.
@author: Henrik Skov Midtiby
"""
import cv2
import numpy as np
from MarkerTracker import MarkerTracker
class ImageAnalyzer:
'''
Purpose: Locate markers in the presented images.
'''
def __init__(self, downscaleFactor = 2):
self.downscaleFactor = downscaleFactor
self.markerTrackers = []
self.tempImage = None
self.greyScaleImage = None
self.subClassesInitialized = False
self.markerLocationsX = []
self.markerLocationsY = []
pass
def addMarkerToTrack(self, order, kernelSize, scaleFactor):
self.markerTrackers.append(MarkerTracker(order, kernelSize, scaleFactor))
self.markerLocationsX.append(0)
self.markerLocationsY.append(0)
self.subClassesInitialized = False
# Is called with a colour image.
def initializeSubClasses(self, frame):
self.subClassesInitialized = True
reducedWidth = frame.shape[1] / self.downscaleFactor
reducedHeight = frame.shape[0] / self.downscaleFactor
reducedDimensions = (reducedHeight, reducedWidth)
self.frameGray = np.zeros (reducedDimensions+(1,), dtype=np.float32)
self.frameGray = np.zeros (reducedDimensions+(1,), dtype=np.float32)
self.originalImage = np.zeros (reducedDimensions+(3,), dtype=np.float32)
self.reducedImage = np.zeros (reducedDimensions+(frame.shape[2],), dtype=np.float32)
for k in range(len(self.markerTrackers)):
self.markerTrackers[k].allocateSpaceGivenFirstFrame(self.reducedImage)
# Is called with a colour image.
def analyzeImage(self, frame):
assert(frame.shape[2] == 3)
if(self.subClassesInitialized is False):
self.initializeSubClasses(frame)
self.reducedImage = cv2.resize(frame,(0,0), fx=1.0/self.downscaleFactor, fy=1.0/self.downscaleFactor)
self.originalImage=self.reducedImage
#cv.ConvertScale(self.reducedImage, self.originalImage)
self.frameGray=cv2.cvtColor(self.originalImage,cv2.cv.CV_RGB2GRAY)
for k in range(len(self.markerTrackers)):
markerLocation = self.markerTrackers[k].locateMarker(self.frameGray)
(xm, ym) = markerLocation
(xm, ym) = (self.downscaleFactor * xm, self.downscaleFactor * ym)
self.markerLocationsX[k] = xm
self.markerLocationsY[k] = ym
#cv.Line(frame, (0, ym), (frame.width, ym), (0, 0, 255)) # B, G, R
#cv.Line(frame, (xm, 0), (xm, frame.height), (0, 0, 255))
return frame