float FindAngle(const IplImage* rgb_img,int threshold,int hor)
{
float ret = -1.0;
CvSize size = cvGetSize(rgb_img);
CvMemStorage* storage = cvCreateMemStorage(0);
IplImage* img_gray = cvCreateImage(size,IPL_DEPTH_8U,1);
IplImage* img_sobel_bw = cvCreateImage(size,IPL_DEPTH_8U,1);
cvCvtColor(rgb_img,img_gray,CV_BGR2GRAY);
cvSmooth(img_gray,img_gray,CV_MEDIAN);
GrayStretch(img_gray,img_gray);
//求边缘图
if(hor)
ReturnSobelImage(img_gray,img_sobel_bw,0,1);
else
ReturnSobelImage(img_gray,img_sobel_bw,1,0);
/*cvShowImage("img_sobel",img_sobel_bw);
cvWaitKey(0);*/
//利用哈弗变换求图中的直线
CvSeq* result = cvHoughLines2(
img_sobel_bw,
storage,
CV_HOUGH_STANDARD,
1,
CV_PI/180,
threshold);
if(result->total != 0)
{
float* line = (float*)cvGetSeqElem(result,0);
ret = (float)CV_PI/2 - line[1];
//IplImage* img_draw = cvCloneImage(img_gray);
//cvZero(img_draw);
//for(int i=0;i<result->total;i++)
//{
// float* l = (float*)cvGetSeqElem(result,i);
// cvLine(img_draw,cvPoint(0,l[0]/sin(l[1])),cvPoint(l[0]/cos(l[1]),0),cvScalar(255,255,255),1);
// /*CvPoint* line = (CvPoint*)cvGetSeqElem(result,i);
// cvLine(img_draw,line[0],line[1],cvScalar(255,255,255),1);*/
//}
//
//cvShowImage("img_draw",img_draw);
//cvWaitKey(0);
}
cvReleaseMemStorage(&storage);
cvReleaseImage(&img_gray);
cvReleaseImage(&img_sobel_bw);
return ret;
}