diff --git a/mariusz/build/Debug/GNU-Linux-x86/Mouse.o.d b/mariusz/build/Debug/GNU-Linux-x86/Mouse.o.d deleted file mode 100644 index 2b0e22d..0000000 --- a/mariusz/build/Debug/GNU-Linux-x86/Mouse.o.d +++ /dev/null @@ -1,3 +0,0 @@ -build/Debug/GNU-Linux-x86/Mouse.o: Mouse.cpp Mouse.hpp - -Mouse.hpp: diff --git a/mariusz/build/Debug/GNU-Linux-x86/main.o.d b/mariusz/build/Debug/GNU-Linux-x86/main.o.d deleted file mode 100644 index fd85132..0000000 --- a/mariusz/build/Debug/GNU-Linux-x86/main.o.d +++ /dev/null @@ -1 +0,0 @@ -build/Debug/GNU-Linux-x86/main.o: main.cpp diff --git a/mariusz/build/Debug/GNU-Linux-x86/src/HandPointer/HandPointer.cpp.o.d b/mariusz/build/Debug/GNU-Linux-x86/src/HandPointer/HandPointer.cpp.o.d deleted file mode 100644 index dbdb541..0000000 --- a/mariusz/build/Debug/GNU-Linux-x86/src/HandPointer/HandPointer.cpp.o.d +++ /dev/null @@ -1,4 +0,0 @@ -build/Debug/GNU-Linux-x86/src/HandPointer/HandPointer.cpp.o: \ - src/HandPointer/HandPointer.cpp.cpp src/HandPointer/Mouse.hpp - -src/HandPointer/Mouse.hpp: diff --git a/mariusz/dist/Debug/GNU-Linux-x86/mariusz b/mariusz/dist/Debug/GNU-Linux-x86/mariusz index 1b2550b..15f6f13 100755 Binary files a/mariusz/dist/Debug/GNU-Linux-x86/mariusz and b/mariusz/dist/Debug/GNU-Linux-x86/mariusz differ diff --git a/mariusz/nbproject/Makefile-Debug.mk b/mariusz/nbproject/Makefile-Debug.mk index 6ad8aa7..76e9aa7 100644 --- a/mariusz/nbproject/Makefile-Debug.mk +++ b/mariusz/nbproject/Makefile-Debug.mk @@ -43,8 +43,8 @@ OBJECTFILES= \ CFLAGS= # CC Compiler Flags -CCFLAGS=`pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -CXXFLAGS=`pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob +CCFLAGS=`pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -lX11 -ldl -lXext +CXXFLAGS=`pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -lX11 -ldl -lXext # Fortran Compiler Flags FFLAGS= @@ -66,12 +66,12 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/mariusz: ${OBJECTFILES} ${OBJECTDIR}/src/HandPointer/HandPointer.o: nbproject/Makefile-${CND_CONF}.mk src/HandPointer/HandPointer.cpp ${MKDIR} -p ${OBJECTDIR}/src/HandPointer ${RM} $@.d - $(COMPILE.cc) -g -Iinclude/cvBlob `pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/HandPointer/HandPointer.o src/HandPointer/HandPointer.cpp + $(COMPILE.cc) -g -Iinclude/cvBlob `pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -lX11 -ldl -lXext -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/HandPointer/HandPointer.o src/HandPointer/HandPointer.cpp ${OBJECTDIR}/src/HandPointer/Media.o: nbproject/Makefile-${CND_CONF}.mk src/HandPointer/Media.cpp ${MKDIR} -p ${OBJECTDIR}/src/HandPointer ${RM} $@.d - $(COMPILE.cc) -g -Iinclude/cvBlob `pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/HandPointer/Media.o src/HandPointer/Media.cpp + $(COMPILE.cc) -g -Iinclude/cvBlob `pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -lX11 -ldl -lXext -MMD -MP -MF $@.d -o ${OBJECTDIR}/src/HandPointer/Media.o src/HandPointer/Media.cpp # Subprojects .build-subprojects: diff --git a/mariusz/nbproject/configurations.xml b/mariusz/nbproject/configurations.xml index 85b204e..042a394 100644 --- a/mariusz/nbproject/configurations.xml +++ b/mariusz/nbproject/configurations.xml @@ -46,7 +46,7 @@ include/cvBlob - `pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob + `pkg-config opencv --cflags --libs` -lX11 -I/usr/local/include -L/usr/local/lib -lcvblob -lX11 -ldl -lXext diff --git a/mariusz/src/HandPointer/HandPointer.cpp b/mariusz/src/HandPointer/HandPointer.cpp index d322556..b12cb44 100644 --- a/mariusz/src/HandPointer/HandPointer.cpp +++ b/mariusz/src/HandPointer/HandPointer.cpp @@ -9,121 +9,185 @@ #include #include #include +#include +#include +#include +#include "opencv2/opencv.hpp" +#include #include #include #include #include #include #include -#include "opencv2/opencv.hpp" -#include + #include "Media.hpp" +#include +#include +#include +#include +#include + + using namespace cv; using namespace std; using namespace cvb; +Mat frame, img1; +Point point; +Point matchLoc; +int drag = 0; +int key = 0; +CvRect rect; +bool done; -int r = 51, g = 99, b = 57; -int rT = 90, gT = 255, bT = 129; -int maxArea=1000, minArea=170; - -void setR(int v) {r = v;} -void setG(int v) {g = v;} -void setB(int v) {b = v;} -void setRT(int v) {rT = v;} -void setGT(int v) {gT = v;} -void setBT(int v) {bT = v;} +void mouseHandler(int event, int x, int y, int flags, void* param) { + if (event == CV_EVENT_LBUTTONUP) { + point = Point(x, y); + done = true; + } +} -void setMax(int v){maxArea = v;} -void setMin(int v){minArea = v;} +void matchImage(Mat &img1, Mat &img2) { + Mat imgOutput; + int row = img1.rows - img2.rows + 1; + int col = img1.cols - img2.cols + 1; + imgOutput.create(col, row, CV_32FC1); + matchTemplate(img1, img2, imgOutput, CV_TM_CCOEFF_NORMED); + normalize(imgOutput, imgOutput, 0, 1, NORM_MINMAX, -1, Mat()); + Point minLoc, maxLoc; + double minVal, maxVal; + minMaxLoc(imgOutput, &minVal, &maxVal, &minLoc, &maxLoc, Mat()); + matchLoc = maxLoc; + +} int main(int, char**) { - VideoCapture cap(0); // open the default camera - if (!cap.isOpened()) // check if we succeeded - return -1; Media mouse; bool mouseon = true; - int x, y; + int x, x1, y, y1; // W H - pair res = mouse.getResolution(); - + pair res = mouse.getResolution(); + + + + VideoCapture cap(0); // open the default camera + if (!cap.isOpened()) // check if we succeeded + return 0; + namedWindow("result", 1); namedWindow("output", 1); namedWindow("bin", 1); - - CvBlobs blobs; - CvTracks tracks; - - IplImage *red = 0, *tmp = 0,* framee=0,*labelImg=0; - - Mat edges, frame; - cap >> frame; - - red = cvCreateImage(cvSize(frame.cols, frame.rows), IPL_DEPTH_8U, 1); - tmp = cvCreateImage(cvSize(frame.cols, frame.rows), IPL_DEPTH_8U, 3); - labelImg = cvCreateImage(cvGetSize(red), IPL_DEPTH_LABEL, 1); - framee= cvCreateImage(cvGetSize(red), 8, 3); - - cvCreateTrackbar("R","bin",&r,255,setR); - cvCreateTrackbar("G","bin",&g,255,setG); - cvCreateTrackbar("B","bin",&b,255,setB); - - cvCreateTrackbar("RT","bin",&rT,255,setRT); - cvCreateTrackbar("BT","bin",&bT,255,setBT); - cvCreateTrackbar("GT","bin",&gT,255,setGT); - - cvCreateTrackbar("min","bin",&minArea,1000,setMin); - cvCreateTrackbar("max","bin",&maxArea,100000,setMax); + flip(frame, frame, 1); - - for (;;) { - cap >> frame; // gt a new frame from camera + Mat rawTemplate, rawSearch; + while (!done) { + + cap >> frame; flip(frame, frame, 1); - cvtColor(frame, edges, CV_BGR2HSV); + imshow("result", frame); + if (rect.width > 0) + frame = frame(rect); + setMouseCallback("result", mouseHandler, NULL); + imshow("result", frame); + cvWaitKey(10); + } - tmp->imageData = (char*) edges.data; - framee->imageData = (char*) frame.data; + Point p1 = Point(point.x - 20, point.y - 20); + Point p2 = Point(point.x + 20, point.y + 20); + + rectangle(frame, p1, p2, CV_RGB(255, 0, 0), 1, 8, 0); + Rect ra = Rect(p1, p2); + Mat imgTemplate = frame(ra); + Point p3 = Point(p1.x - 30, p1.y - 30); + Point p4 = Point(p2.x + 60, p2.y + 60); + + Mat searchRegion = frame(Rect(p3, p4)); + rawTemplate = imgTemplate = imgTemplate.clone(); + rawSearch = searchRegion = searchRegion.clone(); + matchImage(searchRegion, imgTemplate); + + char key = -1; + Rect solution = Rect(Point(matchLoc.x + p3.x, matchLoc.y + p3.y), Point(matchLoc.x + imgTemplate.cols + p3.x, matchLoc.y + + imgTemplate.rows + p3.y)); + + while (true) { + cap >> frame; + flip(frame, frame, 1); + Point p1 = Point(matchLoc.x, matchLoc.y); + Point p2 = Point(matchLoc.x + imgTemplate.cols, matchLoc.y + imgTemplate.rows); + + + imgTemplate = searchRegion.clone(); + imgTemplate = imgTemplate(Rect(p1, p2)); + + + searchRegion = frame.clone(); + searchRegion = searchRegion(Rect(p3, p4)); + + + + matchImage(searchRegion, imgTemplate); + solution = Rect(Point(matchLoc.x + p3.x, matchLoc.y + p3.y), Point(matchLoc.x + imgTemplate.cols + p3.x, matchLoc.y + + imgTemplate.rows + p3.y)); + + + if ((x = solution.x - 50) <= 0) + x = 0; + else + if ((x = solution.x - 50) >= 640) + x = 640; + if ((y = solution.y - 50) <= 0) + y = 0; + else + if ((y = solution.y - 50) >= 480) + y = 480; + if ((x1 = solution.x + solution.width + 50) <= 0) + x1 = 0; + else + if ((x1 = solution.x + solution.width + 50) >= 640) + x1 = 640; + if ((y1 = solution.y + solution.height + 50) <= 0) + y1 = 0; + else + if ((y1 = solution.y + solution.height + 50) >= 480) + y1 = 480; + + + p3 = Point(x, y); + p4 = Point(x1, y1); + rectangle(frame, p3, p4, Scalar(0, 0, 255), 2, 8, 0); + rectangle(frame, solution, Scalar(255, 0, 0), 2, 8, 0); + + + + x = (int) (solution.x + solution.x + solution.width) / 2; + y = (int) (solution.y + solution.y + solution.height) / 2; - cvInRangeS(tmp, Scalar(r, g, b), Scalar(rT, gT, bT), red); - - cvDilate(red, red, NULL, 3); - cvErode(red, red, NULL, 4); - cvDilate(red, red, NULL, 2); - - cvLabel(red, labelImg, blobs); - cvFilterByArea(blobs, minArea, maxArea); - cvRenderBlobs(labelImg, blobs, framee, framee, CV_BLOB_RENDER_BOUNDING_BOX); - cvUpdateTracks(blobs, tracks, 200, 5); - cvRenderTracks(tracks, framee, framee, CV_TRACK_RENDER_ID | CV_TRACK_RENDER_BOUNDING_BOX); - - for (CvBlobs::const_iterator it = blobs.begin(); it != blobs.end(); ++it) { - x = (int) it->second->centroid.x; - y = (int) it->second->centroid.y; - if (mouseon) { - mouse.setMouse(x/(double)frame.cols*res.second,y/(double)frame.rows*res.first); - } + mouse.setMouse(x / (double) frame.cols * res.second, y / (double) frame.rows * res.first,3); + mouse.drawArc(x / (double) frame.cols * res.second, y / (double) frame.rows * res.first, 3); + + + imshow("result3", searchRegion); + imshow("result", frame); + imshow("output", imgTemplate); + + key = waitKey(1); + + if (key == 'q') break; - } - - - imshow("output", Mat(framee)); - imshow("bin", Mat(red)); - - char key = (char) waitKey(1); switch (key) { - - case 'a': - mouseon = mouseon ? false : true; + case 'r': + imgTemplate = rawTemplate.clone(); + searchRegion = frame.clone(); break; } - if (key == 'b') break; //exit program } - destroyAllWindows(); - // the camera will be deinitialized automatically in VideoCapture destructor return 0; } + diff --git a/mariusz/src/HandPointer/Media.cpp b/mariusz/src/HandPointer/Media.cpp index b832568..cbeada5 100644 --- a/mariusz/src/HandPointer/Media.cpp +++ b/mariusz/src/HandPointer/Media.cpp @@ -9,27 +9,107 @@ Media::Media() { dpy = XOpenDisplay(0); - root_window = XRootWindow(dpy, 0); - XSelectInput(dpy, root_window, KeyReleaseMask); - + if (dpy == NULL) { + std::cout << "Err:"; + return; + } + XColor colorval; + Colormap colormap; + XGCValues gc_val; + XWindowAttributes wa; + + char color[] = "#FF0000"; + + screen = DefaultScreen(dpy); + root_window = DefaultRootWindow(dpy); + XGetWindowAttributes(dpy, root_window, &wa); + gc_val.function = GXxor; + gc_val.plane_mask = AllPlanes; + gc_val.foreground = WhitePixel(dpy, screen); + gc_val.background = BlackPixel(dpy, screen); + gc_val.line_width = 4; + gc_val.line_style = LineSolid; + gc_val.cap_style = CapButt; + gc_val.join_style = JoinMiter; + gc_val.fill_style = FillOpaqueStippled; + gc_val.fill_rule = WindingRule; + gc_val.graphics_exposures = False; + gc_val.clip_x_origin = 0; + gc_val.clip_y_origin = 0; + gc_val.clip_mask = None; + gc_val.subwindow_mode = IncludeInferiors; + + colormap = DefaultColormap(dpy, 0); + XMapWindow(dpy, root_window); + + gc = XCreateGC(dpy, root_window, GCFunction | GCPlaneMask | GCForeground | + GCBackground | GCLineWidth | GCLineStyle | + GCCapStyle | GCJoinStyle | GCFillStyle | + GCFillRule | GCGraphicsExposures | + GCClipXOrigin | GCClipYOrigin | GCClipMask | + GCSubwindowMode, &gc_val); + double_buffer = XCreatePixmap(dpy, root_window, + wa.width, wa.height, wa.depth); + XParseColor(dpy, colormap, color, &colorval); + XAllocColor(dpy, colormap, &colorval); + XSetForeground(dpy, gc, colorval.pixel); + XFillRectangle(dpy, double_buffer, gc, 0, 0, wa.width, wa.height); + currXM = 1600 / 2; + currYM = 900 / 2; + currXR = 1600 / 2; + currYR = 900 / 2; } -void Media::setMouse(const int x,const int y){ - XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, x, y); + +void Media::setMouse(const int _x, const int _y, double speed) { + + + currXM -= speed * (xM - _x); + currYM -= speed * (yM - _y); + if (currXM > 1600) currXM = 1600; + if (currXM < 0) currXM = 0; + if (currYM > 900) currYM = 900; + if (currYM < 0) currYM = 0; + xM = _x; + yM = _y; + XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, currXM, currYM); + XFlush(dpy); } -std::pair Media::getResolution(){ - - std::pair resoluton; - - resoluton.first = XDisplayHeight(dpy,0); - resoluton.second= XDisplayWidth(dpy,0); - return resoluton; - + +std::pair Media::getResolution() { + std::pair resoluton; + resoluton.first = XDisplayHeight(dpy, 0); + resoluton.second = XDisplayWidth(dpy, 0); + return resoluton; + } -Media::Media(const Media& orig) { + +void Media::drawArc(int _x, int _y, double speed) { + + + + + draw(); + currXR -= speed * (xR - _x); + currYR -= speed * (yR - _y); + if (currXR > 1600) currXR = 1600; + if (currXR < 0) currXR = 0; + if (currYR > 900) currYR = 900; + if (currYR < 0) currYR = 0; + xR = _x; + yR = _y; + draw(); + XFlush(dpy); +} + +void Media::draw() { + + XFillRectangle(dpy, root_window, gc, currXR, currYR, 50, 50); +} + +Media::Media(const Media & orig) { } Media::~Media() { //free(&dpy); } - diff --git a/mariusz/src/HandPointer/Media.hpp b/mariusz/src/HandPointer/Media.hpp index b97ef84..fdb27d5 100644 --- a/mariusz/src/HandPointer/Media.hpp +++ b/mariusz/src/HandPointer/Media.hpp @@ -9,18 +9,34 @@ #define MOUSE_HPP #include #include +#include +#include class Media { public: Media(); Media(const Media& orig); - void setMouse(const int,const int); + void setMouse(const int,const int,double); + void setPointer(const int,const int); std::pair getResolution(); + void drawArc(int,int,double); + void draw(); virtual ~Media(); private: Display *dpy; Window root_window; - + GC gc; + unsigned long valuemask; + int screen; + int xM; + int yM; + int xR; + int yR; + Pixmap double_buffer; + int currXR; + int currYR; + int currXM; + int currYM; }; #endif /* MOUSE_HPP */