Programming

hough 적용

 2015. 7. 11. 23:15
반응형

#include <opencv2/core/core.hpp>  

#include <opencv2/highgui/highgui.hpp>

#include <opencv2/imgproc.hpp> // for hough line

#include <iostream>  


#define FRAMERATE 20

#define CANNY_THRESHOLD1 125

#define CANNY_THRESHOLD2 350

#define PI 3.1415926


using namespace cv;

using namespace std;


int main(int argc, char** argv)

{

VideoCapture vedioFrame("REC_2015_03_17_08_43_20_D.avi");

if (!vedioFrame.isOpened()){

cout << "동영상 불러오기 실패";

}

Mat img;

while (1){

vedioFrame >> img;

if (img.empty()) break; //동영상 끝났을 때

if (waitKey(FRAMERATE) == 27) break; // 27은 esc의 아스키코드

//캐니 알고리즘

Mat contours;

Canny(img, contours, CANNY_THRESHOLD1, CANNY_THRESHOLD2);


//선 감지 위한 허프 변환

vector<Vec2f> lines;

HoughLines(contours, lines, 1, PI / 180, 80);

//선그리기

Mat result(contours.rows, contours.cols, CV_8U, Scalar(255));

cout << "Lines detected: " << lines.size() << endl;


//선 벡터를 반복해 선 그리기

vector<Vec2f>::const_iterator it = lines.begin();

while (it != lines.end()){

float rho = (*it)[0]; // 0번째 rho거리

float theta = (*it)[1]; //1번째 세타 각도

if (theta<PI / 4. || theta>3.*PI / 4){

Point pt1(rho / cos(theta), 0); // 첫 행에서 해당선 교차점

Point pt2((rho - result.rows*sin(theta)) / cos(theta), result.rows); // 마지막 행에서 해당 선의 교차점

line(contours, pt1, pt2, Scalar(255), 1); // 하얀선으로 그리기


}

else{ //수평행

Point pt1(0, rho / sin(theta));

Point pt2(result.cols, (rho - result.cols*cos(theta) / sin(theta)));

line(contours, pt1, pt2, Scalar(255), 1);

}

//cout << "line: (" << rho << "," << theta << ")\n";

++it;

}


//창 사이즈 줄이기

Mat img_dst;

Mat contours_dst;

resize(img, img_dst, Size(),0.5,0.5);

resize(contours, contours_dst, Size(),0.5,0.5);

//창 띄우기

imshow("original video", img_dst);

imshow("canny edge", contours_dst);

}



destroyAllWindows();

}

반응형