-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worm.h
205 lines (180 loc) · 6.71 KB
/
Worm.h
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
198
199
200
201
202
203
204
205
/* Worm.h
* ------
* Header file for the Worm class, WormOutputData structure, and
* imageWithOutputOverlay function. The Worm object corresponds to
* a frame of tracked data.
*
* Created by John Whitworth on 8/26/14.
* Copyright 2015 Eileen Mazzochette, et al <[email protected]>
*/
#pragma once
#pragma managed(push,off)
#include <cv.h>
#pragma managed(pop)
#include "Zaber.h"
using namespace std;
/* Structure: WormOutputData
* -------------------------
* Made up of all the data that will be recorded from each from of
* tracking data.
*/
struct WormOutputData {
cv::Mat image;
int frameNumber;
SYSTEMTIME frameTime;
bool stimulusActive;
int stimulusNumber;
cv::Point2d stagePosition;
Movement stageMovement;
cv::Point cantilever;
cv::Point target;
cv::Point head;
cv::Point tail;
cv::Point targetSegment1;
cv::Point targetSegment2;
bool headTailToggled;
vector<vector<cv::Point> > contours;
int wormContourIndex;
vector<cv::Point> wormContour;
vector<pair<int, int> > segments;
vector<cv::Point> skeleton;
};
/* Class: Worm
* -----------
* Object corresponding to a frame of tracking data. All the info on the worm
* in the image captured.
*/
class Worm {
//properties
public:
//location point in image space of target
cv::Point target;
//location point in image space of tail
cv::Point tail;
//location point in image space of head
cv::Point head;
//Location of contour points for along the skeleton closest to the target, used for measuring width
cv::Point targetSegment1;
cv::Point targetSegment2;
private:
//processed frame number
int frameNumber;
//time of frame capture
SYSTEMTIME frameTime;
//boolean flags whether or not to use the search radius to find tail
bool resetTailFinding;
//all the contours in the image
vector<vector<cv::Point> > contours;
//the index corresponding to the worm's contour in the contours vector
int wormContourIndex;
//the worm's contour
vector<cv::Point> wormContour;
//the index corresponding to the head point in the wormContour vector
int headIndex;
//the index corresponding to the tail point in the wormContour vector
int tailIndex;
//all the pairs of indexes which indicate the indexes of segment points in wormContour
vector<pair<int, int> > segments;
//all the points along the worm's skeleton, starting at the head
vector<cv::Point> skeleton;
//all images along the processing steps
struct WormImages {
//original image
cv::Mat original;
//resized image
cv::Mat resized;
//image with gaussian blur
cv::Mat smooth;
//image with OTSU's method thresholding
cv::Mat threshold;
} images;
//prototypes
public:
/* Function: Worm
* --------------
* Constructor for a Worm object. Resizes the image, finds the worm, and updates the target
* property. 1st parameter is the original image, 2nd parameter is the percentage of total
* length along the worm to target, 3rd parameter is the tail location from the previously
* processed worm (used for tracking), 4th and 5th parameters are frame info.
*/
Worm(cv::Mat image, double targetLengthPercentage, cv::Point prevTail, int frame, SYSTEMTIME time, bool reset);
cv::Point translateTail(cv::Point oldTail, double stageMovement_x, double stageMovement_y);
/* Function: extractWormOutputData
* -------------------------------
* Extracts all the data that needs to be output from the worm and returns it in the form of
* a WormOutputData structure. Parameters are extra info pieces to be stored and output.
*/
WormOutputData extractWormOutputData(Movement stageMovement, cv::Point2d stagePosition, cv::Point cantilever, bool toggled, bool stimulusActive, int stimulusNumber);
private:
/* Function: findWorm
* ------------------
* Smooths the image, thresholds the image, finds all the contous in the image,
* finds the worm contour, finds the tail, then the head, segments the worm, then
* finds the skeleton and target.
*/
void findWorm(cv::Point prevTail);
/* Function: findTarget
* --------------------
* Caclulates the target length from the head, moves along the skeleton until the
* length is greater than the target length and then interpolates back along the
* previous skeleton part to find the exact target point.
*/
cv::Point findTarget(double percentLength);
/* Function: findWormContour
* -------------------------
* Finds the worm contour by looking for the largest (most points in the vector) contour
* in the list of all contours.
*/
void findWormContour(void);
/* Function: findWormTail
* ----------------------
* Finds the sharpest point along the worm contour. If this isn't the first frame
* only points within a certain range of the previous tail are checked.
*/
void findWormTail(cv::Point prevTail);
/* Function: findWormHead
* ----------------------
* Finds the sharpest point on the other side of the worm from the tail.
*/
void findWormHead(void);
/* Function: segmentWorm
* ---------------------
* Segments the worm by moving along one side of the worm by a set increment and
* then find the closest point on the other side of the worm.
*/
void segmentWorm(void);
/* Function: findSkeleton
* ----------------------
* Connects the head, all the midpoints of the segments, and the tail to be the skelton.
* Updates the target property.
*/
void findSkeleton(void);
/* Function: addMidpointToSkeleton
* -------------------------------
* Takes a pair of indexes corresponding to segment endpoints along the wormContour, finds
* the midpoint of the segment and adds it to the skeleton.
*/
void addMidpointToSkeleton(pair<int, int> segmentIndexes);
/* Function: pointDistance
* -----------------------
* Returns the absolute distance between two points. Uses the distance formula.
*/
double pointDistance(cv::Point a, cv::Point b);
/* Function: boundCheck
* --------------------
* Ensures that the currentIndex is within the range of 0 and the maxIndex.
* If it is not it returns the correct value of the index, assuming the indexes
* loop around (the index after maxIndex is 0).
*/
int boundCheck(int currentIndex, int maxIndex);
};
/* Function: imageWithOutputOverlay
* --------------------------------
* Returns an the image from data paramter with overlays for all the important worm parts.
*/
cv::Mat imageWithOutputOverlay(WormOutputData* data, bool allOverLays);
/* Function: drawCross
* -------------------
* Draws a cross overlay on the image at the specified point in the specified color.
*/
void drawCross(cv::Mat *image, cv::Point point, cv::Scalar color);