SW Matras 07


Marcin Matras, gr 25A, Mechatronika
Systemu wizyjne
OPEN CV  lab 8
Wykorzystanie bibliotek OpenCV do rysowania na ekranie prostych figur geometrycznych oraz do
implementacji programu, którego zadaniem będzie śledzenie dowolnie wybranego punktu na
ekranie
Program 1  Rysowanie figur
1.1 Kod programu
#include
#include
#include
IplImage *frame;
int radius;
int rColor;
int bColor;
int gColor;
void drawCircle_callback(int event, int x, int y, int flags, void *parameter) {
if(event == CV_EVENT_LBUTTONDOWN)
{
radius = rand()%20+1;
rColor = rand()%255+1;
bColor = rand()%255+1;
gColor = rand()%255+1;
cvCircle( frame, cvPoint(x, y),radius,CV_RGB(rColor,bColor,gColor),-1);
printf("\n x = %d y = %d ",x,y);
cvShowImage("Image", frame);
}
}
int main()
{
cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("Image",drawCircle_callback,0);
frame=cvCreateImage(cvSize(640,480),8,3);
cvWaitKey();
return 0;
};
1.2 Wynik działania programu
Program 2  Śledzenie punktu
2.1 Kod programu
#include
#include
#include
CvPoint2D32f *pt_mouse;
IplImage *frame;
int actual_points_nr = 0;
int max_points_nr = 20;
void choose_point_callback(int event, int x, int y, int flags, void *parameter)
{
if (event == CV_EVENT_LBUTTONDOWN && actual_points_nr < max_points_nr)
{
pt_mouse[actual_points_nr] = cvPoint2D32f(x, y);
cvCircle(frame, cvPoint(cvRound(pt_mouse[actual_points_nr].x), cvRound(pt_mouse[actual_points_nr].y)), 3, CV_RGB(255, 0, 0), -1, 8, 0);
cvShowImage("Image Sequence", frame);
actual_points_nr++;
}
}
int main()
{
int time_delay;
CvCapture *camera_capture;
pt_mouse = new CvPoint2D32f[max_points_nr];
cvNamedWindow("Image Sequence",0);
camera_capture = cvCaptureFromCAM(CV_CAP_ANY);
frame = cvCreateImage(cvSize((int)cvGetCaptureProperty(camera_capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(camera_capture, CV_CAP_PROP_FRAME_HEIGHT)), 8, 3);
time_delay = 30;
int t = 0;
while(t != 10)
{
frame = cvQueryFrame(camera_capture);
cvShowImage("Image Sequence", frame);
cvWaitKey(1000/6);
t++;
}
IplImage *frame2 = cvCreateImage(cvGetSize(frame), 8, 1);
IplImage *frame3 = cvCreateImage(cvGetSize(frame), 8, 1);
IplImage *temp_frame = cvCreateImage(cvGetSize(frame), 8, 1);
IplImage *pyr_a = cvCreateImage(cvSize(frame->width+8, frame->height/3), 8, 1);
IplImage *pyr_b = cvCreateImage(cvSize(frame->width+8, frame->height/3), 8, 1);
IplImage *temp_pyr = cvCreateImage(cvSize(frame->width+8, frame->height/3), 8, 1);
CvPoint2D32f *new_points = new CvPoint2D32f[max_points_nr];
CvPoint2D32f *temp_points = new CvPoint2D32f[max_points_nr];
int window = 17;
int iterations = 4;
char status[500];
for (int h = 0; h < max_points_nr; h++)
{
pt_mouse[h] = cvPoint2D32f(0, 0);
new_points[h] = cvPoint2D32f(0, 0);
temp_points[h] = cvPoint2D32f(0, 0);
}
cvSetMouseCallback("Image Sequence", choose_point_callback, 0);
printf("\nChoose points.\nPress any key to continue");
cvCvtColor(frame, frame3, CV_BGR2GRAY);
IplImage *frame_temp = cvCreateImageHeader(cvGetSize(frame), 8, 3);
while(cvWaitKey(10) != 27)
{
frame_temp = cvQueryFrame(camera_capture);
if(!frame_temp) break;
cvCvtColor(frame_temp, frame2, CV_BGR2GRAY);
cvCalcOpticalFlowPyrLK(frame3, frame2, pyr_a, pyr_b, pt_mouse, new_points, actual_points_nr, cvSize(window,window), iterations, status,
NULL, cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 30, 0.03), CV_LKFLOW_PYR_A_READY);
for (int i = 0; i < actual_points_nr; i++)
{
if (status[i] == 0)
{
new_points[i].x = -100;
new_points[i].y = -100;
}
cvCircle(frame_temp, cvPoint(cvRound(new_points[i].x), cvRound(new_points[i].y)), 3, CV_RGB(255, 0, 0), -1, 8, 0);
}
CV_SWAP(new_points, pt_mouse, temp_points);
CV_SWAP(pyr_b, pyr_a, temp_pyr);
CV_SWAP(frame2, frame3, temp_frame);
cvShowImage("Image Sequence", frame_temp);
}
}
2.2 Wynik działania programu
3. Wnioski na temat przetestowanego algorytmu
Program potrafi wykonać nadane mu zadanie, lecz nie robi tego idealnie. Śledząc obiekt, który się
porusza ma tendencje do gubienia obranego punktu. Ponadto jeśli obiekt na chwile zniknie to
program nie jest w stanie powrócić do tego punktu.


Wyszukiwarka

Podobne podstrony:
SW Matras (1)
SW Matras Marcin Lab04
sw gimnazjum 6
CO ZYSKUJE SAMOBÓJCA (Słowa mistyków Kościoła św )
ŻYCIE I MISJA ŚW BERNADETTY SOUBIROUS
Godzinki ku czci Św Michała Archanioła tekst
SW B
Zwycięstwo Chrystusa nad grzechem i szatanem w ujęciu św Leona Wielkiego
Ewangelia wg św Łukasza E lukasza16
SW 2
Cudowny Medalik Św Maksymilian Kolbe
sw gimnazjum 3
Ewangelia wg św Marka Ew Marka11
BT Wstęp do Pierwszego Listu św Piotra apostoła
CUDA ŚW OJCA PIO STYGMATYKA 20 WIEKU

więcej podobnych podstron