-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblob.cpp
73 lines (55 loc) · 2.04 KB
/
blob.cpp
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
// Test code for color blob tracking
// Adapted from http://www.aishack.in/
#include "cv.h"
#include "highgui.h"
#include <stdlib.h>
#include <stdio.h>
// Declarations
IplImage* ColorThresholdImage(IplImage* img);
// Code Body
int main(void) {
// Attempt to initialize capture device
CvCapture* capture = NULL;
capture = cvCaptureFromCAM(0);
if(capture == NULL) {
printf("Could not initialize capture device\n");
return -1;
}
// Set up display windows
cvNamedWindow("video");
cvNamedWindow("thresh");
IplImage* capturedFrame = NULL; // BGR 8-bit?
IplImage* thresholded = NULL; // Binary
//CvMoments *moments = (CvMoments*) malloc(sizeof(CvMoments)); // Try static alloc first
CvMoments moments;
double moment10, moment01, area;
int posX, posY;
while(true) {
cvGrabFrame(capture); // Store internally
capturedFrame = cvRetrieveFrame(capture); // Decode - Don't modify returned data!
if(capturedFrame == NULL) {
printf("Frame capture failed.\n");
break;
}
thresholded = ColorThresholdImage(capturedFrame);
cvMomentsS(imgYellowThresh, &moments, true); // true for binary image
moment10 = cvGetSpatialMoment(&moments, 1, 0);
moment01 = cvGetSpatialMoment(&moments, 0, 1);
area = cvGetCentralMoment(&moments, 0, 0);
posX = (int) (moment10/area);
posY = (int) (moment01/area);
}
}
IplImage* ColorThresholdImage(IplImage* img) {
// Create temporary and return image
IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3); // Size, depth, channels
IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);
// Threshold operation
cvCvtColor(img, imgHSV, CV_BGR2HSR);
CvScalar lowBound = cvScalar(20, 100, 100);
CvScalar highBound = cvScalar(30, 255, 255);
cvInRangeS(imgHSV, lowBound, highBound, imgThreshed);
// Cleanup
cvReleaseImage(&imgHSV);
return imgThreshed;
}