Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
42507eb91a |
No files matched your search
BIN
Binary file not shown.
@@ -27,18 +27,62 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include<stdio.h>
|
#include<stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <cvblob.h>
|
||||||
|
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cvb;
|
using namespace cvb;
|
||||||
Mat frame, img1;
|
Mat frame, img1;
|
||||||
Point point; //punkt klikniecia mysza
|
Point point;
|
||||||
Point matchLoc; //znaleźiony obszar przez matchtemplate
|
Point matchLoc;
|
||||||
int drag = 0;
|
int drag = 0;
|
||||||
int key = 0;
|
int key = 0;
|
||||||
CvRect rect; //prostokat po nacisnieciu myszka ?
|
CvRect rect;
|
||||||
bool done; //pierwszy etap skonczyony uzytkownik klikna myszka na zadany obszar na kltce
|
bool done;
|
||||||
|
int searchSize = 20;
|
||||||
|
Point p1, p2, p3, p4;
|
||||||
|
Mat imgTemplate, searchRegion, rawTemplate, rawSearch, tran;
|
||||||
|
Rect solution;
|
||||||
|
bool noHand = false;
|
||||||
|
int r = 51, g = 99, b = 57;
|
||||||
|
int rT = 90, gT = 255, bT = 129;
|
||||||
|
int maxArea = 10000, minArea = 500;
|
||||||
|
bool hideWindow = false;
|
||||||
|
IplImage *red = 0, *tmp = 0, * framee = 0, *labelImg = 0;
|
||||||
|
CvBlobs blobs;
|
||||||
|
CvTracks tracks;
|
||||||
|
|
||||||
|
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 setMax(int v) {
|
||||||
|
maxArea = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setMin(int v) {
|
||||||
|
minArea = v;
|
||||||
|
}
|
||||||
|
|
||||||
void mouseHandler(int event, int x, int y, int flags, void* param) {
|
void mouseHandler(int event, int x, int y, int flags, void* param) {
|
||||||
if (event == CV_EVENT_LBUTTONUP) {
|
if (event == CV_EVENT_LBUTTONUP) {
|
||||||
@@ -49,26 +93,83 @@ void mouseHandler(int event, int x, int y, int flags, void* param) {
|
|||||||
|
|
||||||
void matchImage(Mat &img1, Mat &img2) {
|
void matchImage(Mat &img1, Mat &img2) {
|
||||||
Mat imgOutput;
|
Mat imgOutput;
|
||||||
int row = img1.rows - img2.rows + 1; //wyliczenie ilosci pikseli w poziomie i w pionie
|
int row = img1.rows - img2.rows + 1;
|
||||||
int col = img1.cols - img2.cols + 1;
|
int col = img1.cols - img2.cols + 1;
|
||||||
imgOutput.create(col, row, CV_32FC1); // stworzenie nowej kopii danego wycinka na ktorym bedzie szykany wzorzec
|
imgOutput.create(col, row, CV_32FC1);
|
||||||
matchTemplate(img1, img2, imgOutput, CV_TM_CCOEFF_NORMED);
|
matchTemplate(img1, img2, imgOutput, CV_TM_CCOEFF_NORMED);
|
||||||
normalize(imgOutput, imgOutput, 0, 1, NORM_MINMAX, -1, Mat()); // to nie wiem po co jest
|
normalize(imgOutput, imgOutput, 0, 1, NORM_MINMAX, -1, Mat());
|
||||||
Point minLoc, maxLoc; // w zaleznosci od uzytej funkcji w matchTemplate zostanie uzyta pierwsz albo druga zmienna patrz dokumentacja w opencv
|
Point minLoc, maxLoc;
|
||||||
double minVal, maxVal;
|
double minVal, maxVal;
|
||||||
minMaxLoc(imgOutput, &minVal, &maxVal, &minLoc, &maxLoc, Mat()); //wyznaczenie wspolrzednych na podstawie obrazu przychodzacego oraz wyliczonych danych
|
minMaxLoc(imgOutput, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
|
||||||
matchLoc = maxLoc;
|
matchLoc = maxLoc;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setRegion(Point point,int frameX,int frameY) {
|
||||||
|
int setRegionSize = 20;
|
||||||
|
if ((p1 = point.x - setRegionSize) > frameX ) p1 = frameX;
|
||||||
|
else
|
||||||
|
if ((p1 = point.x - setRegionSize) < 0) p1 = 0;
|
||||||
|
|
||||||
|
if ((p2 = point.y - setRegionSize) > frameY) p2 = frameY;
|
||||||
|
else
|
||||||
|
if ((p2 = point.y - setRegionSize) < 0) p2 = 0;
|
||||||
|
|
||||||
|
|
||||||
|
p1 = Point(point.x - 20, point.y - 20);
|
||||||
|
p2 = Point(point.x + 20, point.y + 20);
|
||||||
|
|
||||||
|
|
||||||
|
Rect ra = Rect(p1, p2);
|
||||||
|
imgTemplate = frame(ra);
|
||||||
|
p3 = Point(p1.x - 30, p1.y - 30);
|
||||||
|
p4 = Point(p2.x + 60, p2.y + 60);
|
||||||
|
|
||||||
|
searchRegion = frame(Rect(p3, p4));
|
||||||
|
rawTemplate = imgTemplate = imgTemplate.clone();
|
||||||
|
rawSearch = searchRegion = searchRegion.clone();
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
Point searchHand(Mat &img) {
|
||||||
|
|
||||||
|
cvtColor(frame, tran, CV_BGR2HSV);
|
||||||
|
|
||||||
|
tmp->imageData = (char*) tran.data;
|
||||||
|
|
||||||
|
cvInRangeS(tmp, Scalar(r, g, b), Scalar(rT, gT, bT), red);
|
||||||
|
cvSmooth(red, red, CV_BLUR, 3);
|
||||||
|
cvSmooth(red, red, CV_MEDIAN, 3);
|
||||||
|
cvDilate(red, red, NULL, 3);
|
||||||
|
cvErode(red, red, NULL, 8);
|
||||||
|
cvDilate(red, red, NULL, 2);
|
||||||
|
imshow("cos", Mat(red));
|
||||||
|
////cvtColor(frame, frame, CV_RGB2GRAY);
|
||||||
|
framee->imageData = (char*) img.data;
|
||||||
|
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);
|
||||||
|
frame = Mat(framee);
|
||||||
|
cout << "jestem" << endl;
|
||||||
|
for (CvBlobs::const_iterator it = blobs.begin(); it != blobs.end(); ++it) {
|
||||||
|
int x = (int) it->second->centroid.x;
|
||||||
|
int y = (int) it->second->centroid.y;
|
||||||
|
return Point(x, y);
|
||||||
|
}
|
||||||
|
return Point(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int, char**) {
|
int main(int, char**) {
|
||||||
|
|
||||||
Media mouse;
|
Media mouse;
|
||||||
|
bool mouseon = true;
|
||||||
int x, x1, y, y1; //punkty kotre beada tworzyc obwódkę ktora wskazuje na obszar prztwarzania
|
int x, x1, y, y1;
|
||||||
int region = 20; // to chyba niepotrzebne
|
|
||||||
int pointerSpeed = 3; //prszyspieszenie myszki x3
|
|
||||||
bool pointerOn = true,mouseOn = true; //sterowanie myszka, pokazywanie pointera on/off
|
|
||||||
// W H
|
// W H
|
||||||
pair<int, int> res = mouse.getResolution();
|
pair<int, int> res = mouse.getResolution();
|
||||||
|
|
||||||
@@ -80,134 +181,107 @@ int main(int, char**) {
|
|||||||
namedWindow("result", 1);
|
namedWindow("result", 1);
|
||||||
namedWindow("output", 1);
|
namedWindow("output", 1);
|
||||||
namedWindow("bin", 1);
|
namedWindow("bin", 1);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
cap >> frame;
|
cap >> frame;
|
||||||
int frameX = frame.cols, frameY = frame.rows; //rozdzielczosc kamerki
|
int frameX = frame.cols, frameY = frame.rows;
|
||||||
flip(frame, frame, 1);
|
flip(frame, frame, 1);
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
||||||
Mat rawTemplate, rawSearch;
|
|
||||||
while (!done) {
|
while (!done) {
|
||||||
|
|
||||||
cap >> frame;
|
cap >> frame;
|
||||||
flip(frame, frame, 1); //odbicie lustrzane klatki (patrzac na monitor naturalnie sie porusza reka)
|
flip(frame, frame, 1);
|
||||||
imshow("result", frame);
|
imshow("result", frame);
|
||||||
if (rect.width > 0)
|
if (rect.width > 0)
|
||||||
frame = frame(rect); // tego nie wiem musisz sobie sprawdzic
|
frame = frame(rect);
|
||||||
setMouseCallback("result", mouseHandler, NULL); //przchwycenie eventu z myszki
|
setMouseCallback("result", mouseHandler, NULL);
|
||||||
imshow("result", frame);
|
imshow("result", frame);
|
||||||
cvWaitKey(10);
|
cvWaitKey(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
Point p1 = Point(point.x - 10, point.y - 10); //wyznaczenie wzorca o boku 20px
|
setRegion(point);
|
||||||
Point p2 = Point(point.x + 10, point.y + 10);
|
|
||||||
|
|
||||||
Rect ra = Rect(p1, p2);
|
|
||||||
Mat imgTemplate = frame(ra);
|
|
||||||
|
|
||||||
Point p3 = Point(p1.x - 30, p1.y - 30);//wyznaczenie obszaru do obrobki czyli bedzie to kwadrat gdzie w srodku znajduje startowy tamplate
|
|
||||||
Point p4 = Point(p2.x + 60, p2.y + 60);
|
|
||||||
|
|
||||||
Mat searchRegion = frame(Rect(p3, p4)); //wycinek obrazu na ktorym szukamy
|
|
||||||
rawTemplate = imgTemplate = imgTemplate.clone(); //kopia wzorca
|
|
||||||
rawSearch = searchRegion = searchRegion.clone(); //kopia obszaru
|
|
||||||
matchImage(searchRegion, imgTemplate); // szukaj wzorca
|
|
||||||
|
|
||||||
char key = -1;
|
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)); //wylicz pozycje znaleźionego wzorca na podstawie duzego wycinka
|
|
||||||
/*
|
|
||||||
(0,0) frame.x
|
|
||||||
---------------------------
|
|
||||||
| p3(p1-30) |
|
|
||||||
| |----------| |f
|
|
||||||
| | p1 | |r
|
|
||||||
| | |--| | |a
|
|
||||||
| | |--|p2 | |m
|
|
||||||
| |--------- |p4(p2+60)|e
|
|
||||||
| |.
|
|
||||||
| |y
|
|
||||||
| |
|
|
||||||
| |
|
|
||||||
|-------------------------|
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
cap >> frame;
|
cap >> frame;
|
||||||
flip(frame, frame, 1);
|
flip(frame, frame, 1);
|
||||||
|
Point p1 = Point(matchLoc.x, matchLoc.y);
|
||||||
p1.x = matchLoc.x; //wspolzedne templata
|
Point p2 = Point(matchLoc.x + imgTemplate.cols, matchLoc.y + imgTemplate.rows);
|
||||||
p1.y = matchLoc.y;
|
|
||||||
|
|
||||||
p2.x = matchLoc.x + imgTemplate.cols; // druga para pobieram szerokosc i wysoksc tmaplata poniewaz chce miec taki sam obszar
|
|
||||||
p2.y = matchLoc.y + imgTemplate.rows;
|
|
||||||
|
|
||||||
imgTemplate = searchRegion.clone();//kopuje caly wycinek z obszaru poszukiwac
|
|
||||||
imgTemplate = imgTemplate(Rect(p1, p2)); // przycinam go do zadanych rozmiarow trzeba tak zrobic aby nie stracic obrzu z searchRegion
|
|
||||||
|
|
||||||
|
|
||||||
searchRegion = frame.clone(); //analogicznie jak wyzej
|
imgTemplate = searchRegion.clone();
|
||||||
|
imgTemplate = imgTemplate(Rect(p1, p2));
|
||||||
|
|
||||||
|
|
||||||
|
searchRegion = frame.clone();
|
||||||
searchRegion = searchRegion(Rect(p3, p4));
|
searchRegion = searchRegion(Rect(p3, p4));
|
||||||
|
|
||||||
matchImage(searchRegion, imgTemplate); // poszukuj nowego wzorca
|
|
||||||
|
|
||||||
|
|
||||||
|
matchImage(searchRegion, imgTemplate);
|
||||||
solution = Rect(Point(matchLoc.x + p3.x, matchLoc.y + p3.y), Point(matchLoc.x + imgTemplate.cols + p3.x, matchLoc.y +
|
solution = Rect(Point(matchLoc.x + p3.x, matchLoc.y + p3.y), Point(matchLoc.x + imgTemplate.cols + p3.x, matchLoc.y +
|
||||||
imgTemplate.rows + p3.y)); //jak wyzej
|
imgTemplate.rows + p3.y));
|
||||||
//solution.Rect_(p1.x + p3.x, p1.y + p3.x, p2.x + p3.x, p2.y + p3.y);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* sprawdzanie czy rectangle ktory sie rysuje na obrazie nie wychodzi po zaniego efekt widoczny podczas dzilania programu
|
if ((x = solution.x - searchSize) <= 0)
|
||||||
* wodac jak niebieski kwadrat plywa przy krawedziach kaltaki oraz czerwnoy kwadrat sie automatycznie zmniejsza jezeli jest blizej krawedzi
|
|
||||||
* co powoduje ze program nie lata po niewiadomej pamieci i nie sypier errorami :D
|
|
||||||
*/
|
|
||||||
x = solution.x - region;
|
|
||||||
y = solution.y - region;
|
|
||||||
x1 = solution.x + solution.width + region;
|
|
||||||
y1 = solution.y + solution.height + region;
|
|
||||||
|
|
||||||
if (x <= 0)
|
|
||||||
x = 0;
|
x = 0;
|
||||||
else
|
else
|
||||||
if (x >= frameX)
|
if ((x = solution.x - searchSize) >= frameX)
|
||||||
x = frameX;
|
x = frameX;
|
||||||
if (y <= 0)
|
if ((y = solution.y - searchSize) <= 0)
|
||||||
y = 0;
|
y = 0;
|
||||||
else
|
else
|
||||||
if (y >= frameY)
|
if ((y = solution.y - searchSize) >= frameY)
|
||||||
y = frameY;
|
y = frameY;
|
||||||
if (x1 <= 0)
|
if ((x1 = solution.x + solution.width + searchSize) <= 0)
|
||||||
x1 = 0;
|
x1 = 0;
|
||||||
else
|
else
|
||||||
if (x1 >= frameX)
|
if ((x1 = solution.x + solution.width + searchSize) >= frameX)
|
||||||
x1 = frameX;
|
x1 = frameX;
|
||||||
if (y1 <= 0)
|
if ((y1 = solution.y + solution.height + searchSize) <= 0)
|
||||||
y1 = 0;
|
y1 = 0;
|
||||||
else
|
else
|
||||||
if (y1 >= frameY)
|
if ((y1 = solution.y + solution.height + searchSize) >= frameY)
|
||||||
y1 = frameY;
|
y1 = frameY;
|
||||||
|
|
||||||
|
|
||||||
p3.x = x; //przypisanie do starych wartosci w taki sposob jest wygodniej bo dziala a podajac sama wspolrzedne mialem problem z wykmineniem kolejnosci wiec mi sie niechcialo tego robic no i jest czytelniej
|
p3 = Point(x, y);
|
||||||
p3.y = y;
|
p4 = Point(x1, y1);
|
||||||
p4.x = x1;
|
if ((solution.x + solution.width == frameX || solution.y + solution.height ==
|
||||||
p4.y = y1;
|
frameY || solution.x == 0 || solution.y == 0)) {
|
||||||
|
Point center = searchHand(frame);
|
||||||
rectangle(frame, p3, p4, Scalar(0, 0, 255), 2, 8, 0); //rysowanie na frame , scalar to jest wybor koloru , nastepne argumenty to jakies tam sytle i grubosci lini
|
setRegion(center);
|
||||||
|
}
|
||||||
|
rectangle(frame, p3, p4, Scalar(0, 0, 255), 2, 8, 0);
|
||||||
rectangle(frame, solution, Scalar(255, 0, 0), 2, 8, 0);
|
rectangle(frame, solution, Scalar(255, 0, 0), 2, 8, 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
x = (int) (solution.x + solution.x + solution.width) / 2; //srodka znalexionego wzorca
|
x = (int) (solution.x + solution.x + solution.width) / 2;
|
||||||
y = (int) (solution.y + solution.y + solution.height) / 2;
|
y = (int) (solution.y + solution.y + solution.height) / 2;
|
||||||
if(mouseOn)
|
|
||||||
mouse.setMouse(x / (double) frame.cols * res.first, y / (double) frame.rows * res.second, pointerSpeed);
|
//mouse.setMouse(x / (double) frame.cols * res.first, y / (double) frame.rows * res.second, 3);
|
||||||
if(pointerOn)
|
//mouse.drawArc(x / (double) frame.cols * res.first, y / (double) frame.rows * res.second, 3);
|
||||||
mouse.drawArc(x / (double) frame.cols * res.first, y / (double) frame.rows * res.second, pointerSpeed);
|
|
||||||
|
|
||||||
|
|
||||||
imshow("result3", searchRegion); //prezetacja wynikow
|
imshow("result3", searchRegion);
|
||||||
imshow("result", frame);
|
imshow("result", frame);
|
||||||
imshow("output", imgTemplate);
|
imshow("output", imgTemplate);
|
||||||
|
imshow("bin", Mat(red));
|
||||||
|
|
||||||
key = waitKey(1);
|
key = waitKey(1);
|
||||||
|
|
||||||
@@ -218,12 +292,6 @@ int main(int, char**) {
|
|||||||
imgTemplate = rawTemplate.clone();
|
imgTemplate = rawTemplate.clone();
|
||||||
searchRegion = frame.clone();
|
searchRegion = frame.clone();
|
||||||
break;
|
break;
|
||||||
case 'p':
|
|
||||||
mouseOn = mouseOn ? false : true;
|
|
||||||
break;
|
|
||||||
case 'm':
|
|
||||||
pointerOn = pointerOn ? false : true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,8 +101,7 @@ void Media::drawArc(int _x, int _y, double speed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Media::draw() {
|
void Media::draw() {
|
||||||
|
|
||||||
|
|
||||||
XFillRectangle(dpy, root_window, gc, currXR, currYR, 50, 50);
|
XFillRectangle(dpy, root_window, gc, currXR, currYR, 50, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user