SW Matras 07 (1)

SW Matras 07 (1), Mechatronika AGH IMIR, semestr 6, Systemy wizyjne

[ Pobierz całość w formacie PDF ]
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;
}
2. Wynik
3. 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;
}
4. Wynik programu
5. Wnioski na temat działania transformat
[ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • jutuu.keep.pl