|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
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: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: |
// Motion Tracker with Optical Flow.cpp : Defines the entry point for the console application.
//
#include <stdafx.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <stdio.h>
const int MAX_COUNT = 2000;
int count = 0, startOfTrackPoints = 0;
CvPoint2D32f* points[2] = {0,0};
IplImage *color_img=0;
/* Listener for mouse clicks */
void OnMouseClick(int event, int x, int y, int flags, void* param)
{
if(color_img->origin)
y = color_img->height - y;
if(event == CV_EVENT_LBUTTONDOWN && count < MAX_COUNT)
{
// Add point
points[1][count] = points[0][count] = cvPoint2D32f(x, y);
count++;
}
}
int main(int argc,char *argv[])
{
int c, i, j, step, win_size, flags, color;
char* status = 0;
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if( !capture )
{
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
printf("Debug 1\n");
IplImage *gray_img, *prev_img, *curr_img;
IplImage *pyramid, *prev_pyramid, *optical_flow_field;
CvPoint p1, p2, p3, p4, p5;
printf("Welcome to Ax demo! ^_^\n");
printf("Click on the 'This is my webcam!' window to add trackballs.\n");
printf("The 'Field' window shows the optical flow with larger \n");
printf("arrows corresponding to larger displacements.\n\n");
printf("Keyboard commands:\n");
printf(" ESC to quit\n");
printf(" c to clear all the trackballs\n");
printf(" n to cycle through the different trackball colors\n");
/* Initialization and space allocation */
color_img = cvQueryFrame(capture);
if(!color_img)
return 0;
gray_img = cvCreateImage(cvSize(color_img->width,color_img->height), IPL_DEPTH_8U, 1);
optical_flow_field = cvCreateImage( cvSize(color_img->width,color_img->height), IPL_DEPTH_8U, 1);
prev_img = cvCreateImage(cvSize(color_img->width,color_img->height), IPL_DEPTH_8U, 1);
curr_img = cvCreateImage(cvSize(color_img->width,color_img->height), IPL_DEPTH_8U, 1);
pyramid = cvCreateImage(cvSize(color_img->width,color_img->height), IPL_DEPTH_8U, 1);
prev_pyramid = cvCreateImage(cvSize(color_img->width,color_img->height), IPL_DEPTH_8U, 1);
gray_img->origin = color_img->origin;
optical_flow_field->origin = color_img->origin;
prev_img->origin = color_img->origin;
curr_img->origin = color_img->origin;
pyramid->origin = color_img->origin;
prev_pyramid->origin = color_img->origin;
points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
status = (char*)cvAlloc(MAX_COUNT);
flags = 0;
step = 10;
win_size = 10;
color = 0;
/* Create windows */
cvNamedWindow("This is my webcam!", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Field", CV_WINDOW_AUTOSIZE);
/* Set up mouse event listener */
int mouseParam=5;
cvSetMouseCallback("This is my webcam!", OnMouseClick, &mouseParam);
/* Initialize the points to the points on the image separated
by distances of 10 pixels */
printf("Debug 2\n");
for(i = 0; i < color_img->height; i+=step)
{
for(j = 0; j < color_img->width; j+=step)
{
points[0][count] = cvPoint2D32f(j, i);
points[1][count] = cvPoint2D32f(j, i);
count++; // count the number of points
}
}
/* Where track points begin */
startOfTrackPoints = count;
/* Use the "first" frame */
color_img = cvQueryFrame(capture);
if(!color_img)
return 0;
cvCvtColor( color_img, gray_img, CV_BGR2GRAY );
/* Smooth to improve accuracy */
cvSmooth(gray_img, gray_img, CV_GAUSSIAN, 3, 0, 0);
cvSmooth(gray_img, gray_img, CV_GAUSSIAN, 3, 0, 0);
cvSmooth(gray_img, gray_img, CV_GAUSSIAN, 3, 0, 0);
cvCopy(curr_img, prev_img, 0);
cvCopy(gray_img, curr_img, 0);
/* Begin the main infinite loop until break */
for(;;)
{
color_img = cvQueryFrame(capture);
if(!color_img)
break;
cvCvtColor( color_img, gray_img, CV_BGR2GRAY );
/* Smooth to improve accuracy */
cvSmooth(gray_img, gray_img, CV_GAUSSIAN, 3, 0, 0);
cvSmooth(gray_img, gray_img, CV_GAUSSIAN, 3, 0, 0);
cvSmooth(gray_img, gray_img, CV_GAUSSIAN, 3, 0, 0);
cvCopy(curr_img, prev_img, 0);
cvCopy(gray_img, curr_img, 0);
cvCopy(pyramid, prev_pyramid, 0);
for(i = startOfTrackPoints; i < count; i++)
points[0][i] = points[1][i];
/* Calculate the optical flow using pyramidal iterative LK */
cvCalcOpticalFlowPyrLK(prev_img, curr_img, prev_pyramid, pyramid,
points[0], points[1], count, cvSize(win_size, win_size), 3, status, 0,
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03), flags);
/* Threshold to ignore noise */
for(i = startOfTrackPoints; i < count; i++)
if(abs((int)(points[0][i].x - points[1][i].x)) > 50 ||
abs((int)(points[0][i].y - points[1][i].y)) > 50)
points[1][i] = points[0][i];
/* Set flags */
flags |= CV_LKFLOW_PYR_A_READY;
/* Refresh the optical flow field */
for (i = 0; i < optical_flow_field->height; i++)
for (j = 0; j < optical_flow_field->width; j++)
(optical_flow_field->imageData + optical_flow_field->widthStep*i)[j] = (char)255;
/* Draw the optical flow field; the greater the velocity, the bigger the
respective arrow and arrowhead will be for better visualization */
for (i = 0; i < startOfTrackPoints; i++)
{
/* Threshold velocities: not drawing very small or very large velocities */
if((abs((int)(points[0][i].x - points[1][i].x)) >= 1 ||
abs((int)(points[0][i].y - points[1][i].y)) >= 1) &&
(abs((int)(points[0][i].x - points[1][i].x)) <= 20 &&
abs((int)(points[0][i].y - points[1][i].y)) <= 20))
{
/* Arrow */
cvLine(optical_flow_field, cvPointFrom32f(points[0][i]),
cvPointFrom32f(points[1][i]), cvScalar(0, 0, 0,0), 1, 8, 0);
/* Arrowhead */
p1 = cvPoint((int)(cvPointFrom32f(points[0][i]).x + 0.7*(cvPointFrom32f(points[1][i]).x - cvPointFrom32f(points[0][i]).x)),
(int)(cvPointFrom32f(points[0][i]).y + 0.7*(cvPointFrom32f(points[1][i]).y -
cvPointFrom32f(points[0][i]).y)));
p2 = cvPoint((int)(cvPointFrom32f(points[0][i]).x + 0.7*(cvPointFrom32f(points[1][i]).x -
cvPointFrom32f(points[0][i]).x)),
(int)cvPointFrom32f(points[1][i]).y);
p3 = cvPoint((int)cvPointFrom32f(points[1][i]).x,(int)(cvPointFrom32f(points[0][i]).y +
0.7*(cvPointFrom32f(points[1][i]).y - cvPointFrom32f(points[0][i]).y)));
p4 = cvPoint((int)(p1.x + 0.5*(p2.x-p1.x)),
(int)(p1.y + 0.5*(p2.y-p1.y)));
p5 = cvPoint((int)(p1.x + 0.5*(p3.x-p1.x)),
(int)(p1.y + 0.5*(p3.y-p1.y)));
cvLine(optical_flow_field, cvPointFrom32f(points[1][i]),
p4, cvScalar(0, 0, 0,0), 1, 8, 0);
cvLine(optical_flow_field, cvPointFrom32f(points[1][i]),
p5, cvScalar(0, 0, 0,0), 1, 8, 0);
}
else
{
/* Draws a dot */
cvLine(optical_flow_field, cvPointFrom32f(points[0][i]),
cvPointFrom32f(points[0][i]), cvScalar(0, 0, 0,0), 1, 8, 0);
}
}
for(i = startOfTrackPoints; i < count; i++)
{
/* Draw trackballs */
if(color == 0)
cvCircle(color_img, cvPoint((int)points[1][i].x, (int)points[1][i].y),
5, cvScalar(0,255,0,0), 3, 8, 0);
else if(color == 1)
cvCircle(color_img, cvPoint((int)points[1][i].x, (int)points[1][i].y),
5, cvScalar(255,0,0,0), 3, 8, 0);
else
cvCircle(color_img, cvPoint((int)points[1][i].x, (int)points[1][i].y), 5, cvScalar(0,255,255,0), 3, 8, 0);
}
/* Show images */
cvShowImage("This is my webcam!", color_img);
cvShowImage("Field", optical_flow_field);
/* Wait */
c = cvWaitKey(10);
/* Check for ESC */
if(c == 27)
break;
else if(c == 'c')
startOfTrackPoints = count;
else if(c == 'n')
{
color++;
color = color % 3;
}
}
/* Release windows */
cvReleaseCapture(&capture);
cvDestroyWindow("This is my webcam!");
cvDestroyWindow("Field");
}
|
Advertisement
| Hall of Fame |