SW Matras 07, Mechatronika AGH IMIR, semestr 6, Systemy wizyjne


Marcin Matras, gr 25a

Open CV - lab 7

Detekcja linii prostych i okręgów

  1. Kod programu

#include <stdio.h>

#include <cv.h>

#include <highgui.h>

CvCapture* capture = NULL;

CvMemStorage* storage = NULL;

CvSeq* lines = 0;

int threshold = 10;

IplImage* frame = NULL;

IplImage* framecopy = NULL;

IplImage* cannyFrame = NULL;

IplImage* color_dst = NULL;

double res_angle = CV_PI/180;

double res_length = 1;

void init()

{

storage = cvCreateMemStorage(0);

capture = cvCaptureFromCAM(CV_CAP_ANY);

// capture = cvCaptureFromAVI("film.avi");

//CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));

frame = cvQueryFrame(capture);

framecopy = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

cannyFrame = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

cvCvtColor(frame, framecopy, CV_BGR2GRAY);

cvNamedWindow("main", CV_WINDOW_AUTOSIZE);

cvNamedWindow("lines", CV_WINDOW_AUTOSIZE);

}

void release()

{

cvDestroyAllWindows();

cvReleaseImage(&frame);

cvReleaseImage(&framecopy);

cvReleaseImage(&cannyFrame);

cvReleaseImage(&color_dst);

cvReleaseCapture(&capture);

cvReleaseMemStorage(&storage);

}

void lines_trackbar(int threshold)

{

cvCanny(framecopy, cannyFrame, 50, 200, 3);

if (threshold<1) threshold=1;

lines = cvHoughLines2(cannyFrame, storage, CV_HOUGH_STANDARD, res_length, res_angle, threshold, 0, 0);

int i = 0;

for (i=0; i<MIN(lines->total,100); i++)

{

float* line = (float*)cvGetSeqElem(lines, i);

float rho = line[0];

float theta = line[1];

CvPoint pt1, pt2;

double a = cos(theta), b = sin(theta);

double x0 = a*rho, y0 = b*rho;

pt1.x = cvRound(x0+1000*(-b));

pt1.y = cvRound(y0+1000*(a));

pt2.x = cvRound(x0-1000*(-b));

pt2.y = cvRound(y0-1000*(a));

cvLine(color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0);

}

}

void image_processing()

{

lines_trackbar(threshold);

}

void enable_trackbars()

{

cvCreateTrackbar("trackbar", "main", &threshold, 100, lines_trackbar);

}

void show_image()

{

cvShowImage("main", frame);

cvShowImage("lines", color_dst);

}

int main()

{

init();

enable_trackbars();

while(cvWaitKey(10) != 27)

{

frame = cvQueryFrame(capture);

cvCvtColor(frame, framecopy, CV_BGR2GRAY);

color_dst = cvCloneImage(frame);

image_processing();

show_image();

}

release();

return 0;

}

  1. Wynik

0x08 graphic

  1. Kod programu

#include <stdio.h>

#include <cv.h>

#include <highgui.h>

CvCapture* capture = NULL;

CvMemStorage* storage = NULL;

CvSeq* circles = NULL;

int threshold = 10;

IplImage* frame = NULL;

IplImage* framecopy = NULL;

IplImage* gray = NULL;

IplImage* result = NULL;

double dp=1;

double min_dist=10;

double param1=100;

double param2=100;

void init()

{

storage = cvCreateMemStorage(0);

capture = cvCaptureFromCAM(CV_CAP_ANY);

//CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));

frame = cvQueryFrame(capture);

framecopy = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

gray = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

cvNamedWindow("main", CV_WINDOW_AUTOSIZE);

cvNamedWindow("circles", CV_WINDOW_AUTOSIZE);

}

void release()

{

cvDestroyAllWindows();

cvReleaseImage(&frame);

cvReleaseImage(&framecopy);

cvReleaseImage(&gray);

cvReleaseImage(&result);

cvReleaseCapture(&capture);

cvReleaseMemStorage(&storage);

}

void circles_trackbar(int dp)

{

cvSmooth(frame, frame, CV_GAUSSIAN, 9, 9, 0, 0);

cvCvtColor(frame, gray, CV_BGR2GRAY);

if (dp<1) dp=1;

circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, dp, min_dist, param1, param2, 0, 1000);

int i = 0;

for (i=0;i<circles->total;i++)

{

float* p = (float*)cvGetSeqElem(circles, i);

cvCircle(result, cvPoint(cvRound(p[0]),cvRound(p[1])),cvRound(p[2]), CV_RGB(255, 0, 0), 3, 8, 0);

}

}

void image_processing()

{

circles_trackbar(dp);

}

void enable_trackbars()

{

cvCreateTrackbar("trackbar", "main", &threshold, 100, circles_trackbar);

}

void show_image()

{

cvShowImage("main", frame);

cvShowImage("circles", result);

}

int main()

{

init();

enable_trackbars();

while(cvWaitKey(10) != 27)

{

frame = cvQueryFrame(capture);

cvCvtColor(frame, framecopy, CV_BGR2GRAY);

result = cvCloneImage(frame);

image_processing();

show_image();

}

release();

return 0;

}

  1. Wynik programu

0x08 graphic

  1. Wnioski na temat działania transformat

0x08 graphic

0x08 graphic
0x08 graphic

0x08 graphic
0x08 graphic



Wyszukiwarka

Podobne podstrony:
DVC, Mechatronika AGH IMIR, semestr 6, Systemy wizyjne
Rodzaj i sposób obróbki, Mechatronika AGH IMIR, semestr 8, ZSW
Tokarka 1, Mechatronika AGH IMIR, semestr 8, ZSW
porozumienie 2010, Mechatronika AGH IMIR, semestr 6, Elementy wyk. robotów 2, ARTAS.SAM.v6.0.45.Mult
lab 4 chuso, Mechatronika AGH IMIR, semestr 6, Identyfikacja i analiza sygnałów 2, lab4
IiAS lab 1, Mechatronika AGH IMIR, semestr 6, Identyfikacja i analiza sygnałów 2, sprawozdania
Analiza i identyfikacja sygna, Mechatronika AGH IMIR, semestr 6, Identyfikacja i analiza sygnałów 2,
Sprawozdanie z praktyk(1), Mechatronika AGH IMIR, semestr 7
list intencyjny, Mechatronika AGH IMIR, semestr 6, Elementy wyk. robotów 2, ARTAS.SAM.v6.0.45.Multi-
promesa, Mechatronika AGH IMIR, semestr 6, Elementy wyk. robotów 2, ARTAS.SAM.v6.0.45.Multi-user.Pro
KT(1), Mechatronika AGH IMIR, semestr 6, KWW 2
Rodzaj i sposób obróbki, Mechatronika AGH IMIR, semestr 8, ZSW
Tokarka 1, Mechatronika AGH IMIR, semestr 8, ZSW
zag2, AGH IMIR, Semestr 2, PNOM
charakterystyki 2 2, Mechatronika AGH IMIR, rok 2, Metrologia sprawozdania, inncyh
metr-koło 4, Mechatronika AGH IMIR, rok 2, Metrologia sprawozdania, inncyh
Tabelki, AGH IMIR, I semestr, Zapis konstrukcji + grafika inżynierska
Teora sterowania lab2, Mechatronika AGH IMIR, rok 2, Teoria sterowania, lab2 grzybek
STAT.KONTR.JAKOŚCI, Mechatronika AGH IMIR, rok 2, Metrologia sprawozdania, inncyh

więcej podobnych podstron