#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "Image.h"
#include "WatchPoint.h"
#include "Thread.h"
#include "Timer.h"
#define WHITESPACE " \t\n\r"
using namespace std;
static const char *window1_id = "Camera input";
bool running = true;
bool draw_box = false;
CvPoint pt1,pt2;
IplImage *image = NULL;
IplImage *motion_image = NULL;
RgbImage input_image;
std::vector<WatchPoint> watch_point;
int tolerance = 60;
void mouse_handler1(int event,int x,int y,int flags,void *param);
void keyboard_handler();
DWORD update_watch_point(LPVOID data);
Thread<void> thread(NULL);
Thread<void> update_thread(NULL);
DWORD thread_func(LPVOID data);
Timer *timer;
int main(int argc,char *argv[])
{
thread.ThreadFunction = thread_func;
thread.Create();
CvCapture *capture = NULL;
cvNamedWindow(window1_id,CV_WINDOW_NORMAL);
cvMoveWindow(window1_id,100,100);
cvResizeWindow(window1_id,800,600);
cvSetMouseCallback(window1_id,mouse_handler1);
motion_image = cvLoadImage("OnMotionDetected.png");
capture = cvCaptureFromCAM(0);
image = cvQueryFrame(capture);
//processed_image = image;
update_thread.ThreadFunction = update_watch_point;
update_thread.Create();
while(running)
{
image = cvQueryFrame(capture);
input_image = image;
keyboard_handler();
if(draw_box)
cvRectangle(input_image,pt1,pt2,cvScalar(255.0,255.0,255.0),5,10);
for(int i = 0;i < watch_point.size();i++)
{
cvRectangle(input_image,watch_point[i]._p1,watch_point[i]._p2,cvScalar(255.0,155.0,155.0),5,1);
}
cvShowImage(window1_id,input_image);
}
update_thread.Close();
thread.Close();
Sleep(200);
cvReleaseImage(&motion_image);
cvReleaseCapture(&capture);
cvDestroyWindow(window1_id);
exit(0);
return 0;
}
void mouse_handler1(int event,int x,int y,int flags,void *param)
{
switch(event)
{
case CV_EVENT_RBUTTONDOWN:
{
//(flags & CV_EVENT_FLAG_CTRLKEY)
{
draw_box = true;
pt1.x = x;
pt1.y = y;
cout<<"Left button pressed at "<<x<<" "<<y<<endl;
}
}
break;
case CV_EVENT_RBUTTONUP:
{
//if(flags & CV_EVENT_FLAG_CTRLKEY)
{
draw_box = false;
watch_point.push_back(WatchPoint(input_image,pt1,pt2));
}
}
break;
case CV_EVENT_MOUSEMOVE:
{
//if(flags & CV_EVENT_FLAG_CTRLKEY)
{
if(draw_box)
{
pt2.x = x;
pt2.y = y;
}
}
}
break;
default: break;
}
}
void keyboard_handler()
{
char key = cvWaitKey(1);
if(key == 27)
{
running = false;
}
if(key == '-')
{
tolerance--;
if(tolerance < 0)tolerance = 0;
cout<<"tolerance: "<<tolerance<<endl;
}
if(key == '+')
{
tolerance++;
cout<<"tolerance: "<<tolerance<<endl;
}
if(key == 'i')
{
watch_point[0].improve_quality();
}
}
DWORD update_watch_point(LPVOID data)
{
while(update_thread.is_running())
{
Sleep(10);
static int c = 0;
for(int i = 0;i < watch_point.size();i++)
{
watch_point[i].flip_image(input_image);
watch_point[i].set_tolerance(tolerance);
watch_point[i].set_motion_image(motion_image);
watch_point[i].movement();
}
}
return 0;
}
DWORD thread_func(LPVOID data){
while(thread.is_running())
{
char char_text[100];
char *token = NULL;
cin.getline(char_text,100,'\n');
token = strtok(char_text,WHITESPACE);
if(strcmp(token,"exit") == 0)
{
running = false;
}
else if(strcmp(token,"watchpoint") == 0)
{
int sensor = atoi(strtok(NULL,WHITESPACE));
token = strtok(NULL,WHITESPACE);
if(sensor > watch_point.size())
{
cout<<"Watch point "<<sensor<<" doesn't exis"<<endl;
continue;
}
if(strcmp(token,"tolerance") == 0)
{
watch_point[sensor].set_tolerance(atoi(strtok(NULL,WHITESPACE)));
cout<<"Watchpoint "<<sensor<<" tolerance set to "<<watch_point[sensor].get_tolerance()<<endl;
}
else if(strcmp(token,"fliptime") == 0)
{
watch_point[sensor].set_fliptime(atoi(strtok(NULL,WHITESPACE)));
cout<<"Watchpoint "<<sensor<<" fliptime set to "<<watch_point[sensor].get_fliptime()<<endl;
}
else if(strcmp(token,"improve") == 0)
{
cout<<"Improving watchpoint "<<sensor<<endl;
watch_point[sensor].improve_quality();
}
else
{
cout<<"Unknown command"<<endl;
}
}
else
{
cout<<"Unkown command"<<endl;
}
}
cout<<"Quitting thread"<<endl;
return 0;
}