Hi all
Here is a code running on Ubuntu Linux which move the mouse cursor using laser point .The main library used for this code is XLib and Open CV.
Code
#define SPACE XK_A
#include<stdio.h>
#include<highgui.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include<string.h>
#include<math.h>
XKeyEvent createKeyEvent(Display *display, Window &win, Window &winRoot, bool press, int keycode, int modifiers)
{
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = True;
event.keycode = XKeysymToKeycode(display, keycode);
event.state = modifiers;
return event;
}
main()
{
CvCapture* VideoBuffer = cvCaptureFromCAM(CV_CAP_ANY);
uchar *Data;
int revert,Step,noPointerInterval = 0,i,j,k,XCordSum = 0,YCordSum = 0;
int RedPixelCount = 0,hold=0,noPointerFlag=0,blinkInterval=0,Width,Height;
Display *display = XOpenDisplay(0);
XKeyEvent event;
Window winRoot;
winRoot = XDefaultRootWindow(display);
Window winFocus;
char num;
if(!VideoBuffer)
{
printf("NO CAM FOUND!!!\n");
exit(0);
}
while(1)
{
RedPixelCount=0;
XCordSum=0;
YCordSum=0;
IplImage* Frame=cvQueryFrame(VideoBuffer);
Height=Frame->height;
Width=Frame->width;
Step=Frame->widthStep;
Data=(uchar *)Frame->imageData;
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
{
if((2*Data[i*Step+j*3+2]-Data[i*Step+j*3+1]-Data[i*Step+j*3]>130))
{
XCordSum+=j;
YCordSum+=i;
RedPixelCount++;
}
}
if(RedPixelCount)
{
XWarpPointer(display, None, winRoot, 0, 0, 0, 0,(XCordSum/RedPixelCount)*2,(YCordSum/RedPixelCount)*2);
XGetInputFocus(display, &winFocus, &revert);
if(noPointerInterval<=10 && noPointerFlag!=0)
{
if(blinkInterval<5)
{
blinkInterval=5;
}
else
{
XGetInputFocus(display, &winFocus, &revert);
event = createKeyEvent(display, winFocus, winRoot, true, SPACE, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
event = createKeyEvent(display, winFocus, winRoot, false, SPACE, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
blinkInterval=0;
}
noPointerFlag=0;
fflush(stdout);
}
blinkInterval++;
hold=0;
noPointerInterval=0;
}
if(noPointerInterval>10 && hold==0)
{
printf("\nNO pointer.");
fflush(stdout);
hold=1;
}
noPointerFlag=1;
}
cvReleaseCapture(&VideoBuffer);
return(0);
}
Compiling Command
g++ `pkg-config opencv --cflags --libs` filename.cpp -o outputname
Execution
$./outputname
0 comments:
Post a Comment