코코야이야기
[c++] OpenCV 직선검출 본문
소스코드
//영상에서 cvHoughLines2 함수로 직선검출
#include "cv.h"
#include "highgui.h"
int main()
{
IplImage *srcImage;
if((srcImage = cvLoadImage("square3.jpg",CV_LOAD_IMAGE_GRAYSCALE)) ==NULL)
return -1;
IplImage *edgeImage = cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,1);
IplImage* dstImage = cvCreateImage(cvGetSize(srcImage),IPL_DEPTH_8U,3);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* seqLines;
int k;
cvCanny(srcImage,edgeImage,50,100,3);
seqLines = cvHoughLines2(edgeImage,storage,CV_HOUGH_STANDARD,1,CV_PI/180,100,0,0);
//cvHoughLines2(영상,검출된 직선의 메모리 공간 - 행렬형태저장,method,원점으로부터의 거리의 간격, x축과의 각도=라디안간격, 직선을 검출하기 위한 임계값, param1=0.param2=0)
/*
method - CV_HOUGH_STANDARD : 직선검출
- CV_HOUGH_PROBABILISTIC : 양끝점이 있는 선분을 검출
- CV_HOUGH_MULTI_SCALE : STANDARD와 같음
*/
//seqLines = cvHoughLines2(edgeImage,storage,CV_HOUGH_MULTI_SCALE,4,CV_PI/180,100,4,2);
printf("seqLines->total = %d\n",seqLines->total);
cvCvtColor(srcImage,dstImage,CV_GRAY2BGR);
for(k=0;k<seqLines->total;k++)
{
float* line;
float rho, theta; //원점에서 (rho,theta)에 의한 직선과 수직으로 만나는 좌표 점(x0,y0)을 x0=rho*c, y0=rho*s로 계산
float c,s;
float x0,y0;
line = (float*)cvGetSeqElem(seqLines,k);
rho = line[0];
theta = line[1];
printf("line[%4d]=(rho,theta)=(%f,%f)\n",k,rho,theta);
//drawing line
c = cos(theta);
s = sin(theta);
x0 = rho*c;
y0 = rho*s;
CvPoint pt1, pt2;
pt1.x = cvRound(x0 + 1000*(-s));
pt1.y = cvRound(y0 + 1000*(c));
pt2.x = cvRound(x0 - 1000*(-s));
pt2.y = cvRound(y0 - 1000*(c));
cvLine(dstImage,pt1,pt2,CV_RGB(255,0,0),2,8);
}
/*
//to detect line segments
seqLines = cvHoughLines2(edgeImage,storage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,100,10,10);
printf("seqLines -> total = %d\n",seqLines->total);
cvCvtColor(srcImage,dstImage,CV_GRAY2BGR);
for(k=0;k<seqLines->total;k++)
{
CvPoint* lineSeg = (CvPoint*)cvGetSeqElem(seqLines,k);
printf("P1(%4d,%4d) - P2(%4d,%4d)\n",lineSeg[0].x, lineSeg[0].y, lineSeg[1].x, lineSeg[1].y);
cvLine(dstImage,lineSeg[0], lineSeg[1], CV_RGB(255,0,0), 1, 8);
}
*/
cvNamedWindow("dstImage",CV_WINDOW_AUTOSIZE);
cvShowImage("dstImage",dstImage);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&srcImage);
cvReleaseImage(&dstImage);
cvReleaseMemStorage(&storage);
cvReleaseImage(&edgeImage);
return 0;
}
출력영상
'프로그래밍 > OpenCV' 카테고리의 다른 글
[c++] OpenCV 특징점검출 (0) | 2015.09.24 |
---|---|
[c++] OpenCV 원검출 (0) | 2015.09.24 |
[c++] OpenCV 영상엣지검출 (0) | 2015.09.23 |
[c++] OpenCV 트랙바추가 (0) | 2015.09.22 |
[c++] OpenCV 도형그리기5 (0) | 2015.09.22 |