int main(int argc, char *argv[]) { //画像をグレースケールとして読み込み img = cv::imread(argv[1], 0); //LSD用画像に変換>< double *dat = new double[img.rows * img.cols]; for(int y = 0; y < img.rows; y++) for(int x = 0; x < img.cols; x++) dat[y * img.cols + x] = img.at<unsigned char>(y, x); //LSD処理 lines = lsd(&n_lines, dat, img.cols, img.rows); //しきい値の最大値と最小値をもってくる int max_NFA = 0; for(int i = 0; i < n_lines; i++) max_NFA = std::max(max_NFA, static_cast<int>(lines[i * 7 + 6])); //結果描画用画像 cv::cvtColor(img, img, CV_GRAY2RGB); //結果表示用ウィンドウ cv::namedWindow("result_image"); cv::createTrackbar("NFA", "result_image", NULL, max_NFA, change_th_lsd); cv::setTrackbarPos("NFA", "result_image", max_NFA); //結果表示 cv::imshow("result_image", img); cv::waitKey(0); }
int main(int argc, char** argv) { ros::init(argc, argv, "simple_base_scan_follower"); ros::NodeHandle n; LaserScannerDetection lsd(n); ros::spin(); return 0; }
std::vector<cv::Vec4f> aLSD::applyLSDetector(cv::Mat image){ // _time_sessions[0] = // begin_time = std::clock(); cv::Mat temp; // convert BGR color to gray color TIME_COUNT(_time_values[0]){ cv::cvtColor(image, temp, CV_BGR2GRAY); } double *img_pointer; int X = image.cols; /* x image size */ int Y = image.rows; /* y image size */ TIME_COUNT(_time_values[1]){ /* create a simple image: left half black, right half gray */ img_pointer = (double *) malloc(X * Y * sizeof(double)); } if (img_pointer == NULL) { fprintf(stderr, "error: not enough memory\n"); exit(EXIT_FAILURE); } int x, y; TIME_COUNT(_time_values[2]){ for (x = 0; x < X; x++) for (y = 0; y < Y; y++) img_pointer[x + y * X] = temp.at<uint8_t>(y, x); /* image(x,y) */ } /* LSD call */ int n; double *out; TIME_COUNT(_time_values[3]){ out = lsd(&n, img_pointer, X, Y); } std::vector<cv::Vec4f> line_segments; TIME_COUNT(_time_values[4]){ for (int i = 0; i < n; i++) { cv::Vec4f line_segment(out[7 * i + 0], out[7 * i + 1], // point1 out[7 * i + 2], out[7 * i + 3]); // point2 line_segments.push_back(line_segment); } } /* free memory */ free((void *) img_pointer); free((void *) out); return line_segments; }
void CvxLSD::detect_lines(const vil_image_view<vxl_byte> & image, vcl_vector<LSDLineSegment> & line_segments) { assert(image.nplanes() == 3 || image.nplanes() == 1); vil_image_view<vxl_byte> grayImage; if (image.nplanes() == 3) { vil_convert_planes_to_grey(image, grayImage); } else { grayImage = image; } double * imageData = NULL; double * out = NULL; int width = grayImage.ni(); int height = grayImage.nj(); int n = 0; imageData = (double *) malloc( width * height * sizeof(double) ); assert(imageData); for(int j=0; j<height; j++) { for(int i=0; i<width; i++) { imageData[i + j * width] = grayImage(i, j); } } /* LSD call */ out = lsd(&n, imageData, width, height); /* print output */ // printf("%d line segments found:\n",n); line_segments.resize(n); for(int i=0; i<n; i++) { double x1 = out[7*i + 0]; double y1 = out[7*i + 1]; double x2 = out[7*i + 2]; double y2 = out[7*i + 3]; double line_width = out[7*i + 4]; double p = out[7*i + 5]; double tmp = out[7*i + 6]; line_segments[i].seg_ = vgl_line_segment_2d<double>(vgl_point_2d<double>(x1, y1), vgl_point_2d<double>(x2, y2)); line_segments[i].width_ = line_width; line_segments[i].angle_precision_ = p; line_segments[i].NFA_ = tmp; } free( (void *) imageData ); free( (void *) out ); }
int main(int argc, char **argv) { if (argc < 2 || argc > 2) { std::cout << "Usage: lsd_opencv_example imageName" << std::endl; return; } cv::Mat src = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR); cv::Mat tmp, src_gray; cv::cvtColor(src, tmp, CV_RGB2GRAY); tmp.convertTo(src_gray, CV_64FC1); int cols = src_gray.cols; int rows = src_gray.rows; image_double image = new_image_double(cols, rows); image->data = src_gray.ptr<double>(0); ntuple_list ntl = lsd(image); cv::Mat lsd = cv::Mat::zeros(rows, cols, CV_8UC1); cv::Point pt1, pt2; for (int j = 0; j != ntl->size ; ++j) { pt1.x = ntl->values[0 + j * ntl->dim]; pt1.y = ntl->values[1 + j * ntl->dim]; pt2.x = ntl->values[2 + j * ntl->dim]; pt2.y = ntl->values[3 + j * ntl->dim]; double width = ntl->values[4 + j * ntl->dim]; cv::line(lsd, pt1, pt2, cv::Scalar(255), width, CV_AA); } free_ntuple_list(ntl); cv::namedWindow("src", CV_WINDOW_AUTOSIZE); cv::imshow("src", src); cv::namedWindow("lsd", CV_WINDOW_AUTOSIZE); cv::imshow("lsd", lsd); cv::waitKey(0); cv::destroyAllWindows(); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nrhs != 1) mexErrMsgTxt("Invalid input arguments!"); else if (nlhs > 1) mexErrMsgTxt("Too many output arguments!"); double *inMatrix; double *outMatrix, *out; int m, n; int num; inMatrix = mxGetPr(prhs[0]); m = mxGetM(prhs[0]); n = mxGetN(prhs[0]); out = lsd(&num, inMatrix, m, n); plhs[0] = mxCreateDoubleMatrix(1, 7*num, mxREAL); outMatrix = mxGetPr(plhs[0]); memcpy(outMatrix, out, 7*num*sizeof(double)); }
int main(void) { double * image; double * out; int x,y,i,j,n; int X = 128; /* x image size */ int Y = 128; /* y image size */ /* create a simple image: left half black, right half gray */ image = (double *) malloc( X * Y * sizeof(double) ); if( image == NULL ) { mexErrMsgTxt("error: not enough memory\n"); } for(x=0;x<X;x++) for(y=0;y<Y;y++) image[x+y*X] = x<X/2 ? 0.0 : 64.0; /* image(x,y) */ /* LSD call */ out = lsd(&n,image,X,Y); /* print output */ mexPrintf("%d line segments found:\n",n); for(i=0;i<n;i++) { for(j=0;j<7;j++) mexPrintf("%f ",out[7*i+j]); mexPrintf("\n"); } /* free memory */ free( (void *) image ); free( (void *) out ); return EXIT_SUCCESS; }
void LineExtract::DetectLine(const Mat &Image, vector<LineFeature> &LineFeatures) { assert(Image.type() == CV_64F && Image.channels() == 1); Mat Gray; Gray = Image.clone(); double *lsd_out; int nout; lsd_out = lsd(&nout, (double *) (Gray.data), Gray.cols, Gray.rows); LineFeatures.resize(nout); double a, b, c, d; // lsd output x1,y1,x2,y2,width,p,-log10(NFA) int out_dim = 7; for (int i = 0; i < nout; ++i) { a = lsd_out[i * out_dim]; b = lsd_out[i * out_dim + 1]; c = lsd_out[i * out_dim + 2]; d = lsd_out[i * out_dim + 3]; if (std::sqrt((a - c) * (a - c) + (b - d) * (b - d)) >= setting::lineLenThreshold) { Vector2d start, end; start << a, b; end << c, d; LineFeatures[i] = LineFeature(start, end, i); } } cv::Mat xGradImg, yGradImg; int ddepth = CV_64F; cv::Sobel(Gray, xGradImg, ddepth, 1, 0, 3); // Gradient X cv::Sobel(Gray, yGradImg, ddepth, 0, 1, 3); // Gradient Y for (int i = 0; i < LineFeatures.size(); ++i) { LineFeatures[i].mDirection = LineFeatures[i].getGradient(xGradImg, yGradImg); ComputeLineDesc(LineFeatures[i], xGradImg, yGradImg); LOG(INFO) << "Line Feature " << i << endl; } }
int main(void) { int x, y, i, j, n; const int X = 128; // x image size const int Y = 128; // y image size // create a simple image: left half black, right half gray double *image = (double *)malloc(X * Y * sizeof(double)); if (NULL == image) { std::cerr << "error: not enough memory" << std::endl; return EXIT_FAILURE; } for (x = 0; x < X; ++x) for (y = 0; y < Y; ++y) image[x + y * X] = (x < X / 2) ? 0.0 : 64.0; // image(x,y) // LSD call double *out = lsd(&n, image, X, Y); // print output std::cout << n << " line segments found:" << std::endl; for (i = 0; i < n; ++i) { for (j = 0; j < 7; ++j) std::cout << out[7 * i + j] << ' '; std::cout << std::endl; } // free memory free((void *)image); free((void *)out); return EXIT_SUCCESS; }
void display( void ) { glClear( GL_COLOR_BUFFER_BIT ); // Очистка экрана float col1[3] = {0, 0.7, 1}; // голубой float col2[3] = {1, 1, 1}; // белый float col3[3] = {1, 0.8, 0.2}; // оранжевый float col4[3] = {1, 0.1, 0.3}; /* for(int i = 0; i < recsq.length(); i++) drawQuad((TRectangle*)(recsq.get(i)), col1); for(int i = 0; i < triangles.length(); i++) drawTriangle((Triangle*)(triangles.get(i)), col3); for(int i = 0; i < figures.length(); i++) drawQuad((TRectangle*)(figures.get(i)), col2); for(int i = 0; i < figures2.length(); i++) drawTriangle((Triangle*)(figures2.get(i)), col2); */ double * image; double * out; int x,y,i,j,n; int X = img.getw(); /* x image size */ int Y = img.geth(); /* y image size */ image = (double *) malloc( X * Y * sizeof(double) ); if( image == NULL ) { fprintf(stderr,"error: not enough memory\n"); exit(EXIT_FAILURE); } for(x=0;x<X;x++) for(y=0;y<Y;y++) image[x+y*X] = img.getpoint(x+1,y+1); /* image(x,y) */ /* LSD call */ out = lsd(&n,image,X,Y); TSafeVector lns; //linesq.deleteall(); int k = 0; for(i=0;i<n;i++) { //for(j=0;j<7;j++) //printf("%4.0f ",out[7*i+j]); lns.setat(new TLine(out[7*i+0],img.geth()-out[7*i+1],out[7*i+2],img.geth()-out[7*i+3]),k); ((TLine*)(lns.get(k)))->shift(-img.getw()/2,-img.geth()/2); k++; } img.show(); for(int i = 0; i < lns.length(); i++) drawLine((TLine*)(lns.get(i)), col1); calcRectangles(&lns, &figures); calcTriangles(&lns, &figures2); for(int i = 0; i < figures.length(); i++) drawQuad((TRectangle*)(figures.get(i)), col3); for(int i = 0; i < figures2.length(); i++) drawTriangle((Triangle*)(figures2.get(i)), col4); glutSwapBuffers(); free( (void *) image ); free( (void *) out ); /* Sleep(40); recalc(); glutPostRedisplay(); */ }
int main( int argc, char** argv){ int i,j; double fps = 1.0; CvCapture* capture; if ( argc == 1 ) { capture = cvCreateCameraCapture (0); } else { capture = cvCreateFileCapture (argv[1]); // fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); DOES NOT ALWAYS WORK } assert( capture != NULL ); //get capture properties int width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH); int height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT); //create OpenCV image structs IplImage *frame; IplImage *frameBW = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 1 ); IplImage *frameLSD = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 3 ); //create LSD image type double *image; image = (double *) malloc( width * height * sizeof(double) ); while (1) { frame = cvQueryFrame( capture ); if( !frame ) break; //convert to grayscale cvCvtColor( frame , frameBW, CV_RGB2GRAY); //cast into LSD image type uchar *data = (uchar *)frameBW->imageData; for (i=0;i<width;i++){ for(j=0;j<height;j++){ image[ i + j * width ] = data[ i + j * width]; } } //run LSD double *out; int n; out = lsd( &n, image, width, height ); //DO PROCESSING DRAWING ETC //draw segments on frame and frameLSD cvZero(frameLSD); for (j=0; j<n ; j++){ //define segment end-points CvPoint pt1 = cvPoint(out[ 0 + j * 7 ],out[ 1 + j * 7 ]); CvPoint pt2 = cvPoint(out[ 2 + j * 7 ],out[ 3 + j * 7 ]); // draw line segment on frame cvLine(frame,pt1,pt2,CV_RGB(0,255,0),1.5,8,0); cvLine(frameLSD,pt1,pt2,CV_RGB(255,255,255),out[ 5 + j * 7 ],8,0); } cvShowImage("FRAME WITH LSD",frame); cvShowImage("LSD",frameLSD); //free memory free( (void *) out ); char c = cvWaitKey(1.0/fps); if( c == 27 ) break; // ESC QUITS } //free memory free( (void *) image ); cvDestroyWindow( "FRAME WITH LSD"); cvDestroyWindow( "LSD"); cvReleaseCapture( &capture ); cvReleaseImage( &frame ); cvReleaseImage( &frameBW ); cvReleaseImage( &frameLSD );}
int main(int argc, char * argv[]) { system("bash dataprep.sh"); #pragma omp parallel for for(int n=1; n<argc; n++) { Mat srcImg=imread(argv[n],CV_LOAD_IMAGE_GRAYSCALE); Mat img=imread(argv[n],CV_LOAD_IMAGE_COLOR); string str=argv[n]; cout<<"\n\n processing the image: "<<str<<endl; if (!srcImg.data) { cout<<"failed to load the image!\n"; // return 0; continue; } for(int i=0; i< srcImg.rows; i++) { for(int j=0; j<5; j++) { srcImg.at<uchar>(i,j)=uchar(0); srcImg.at<uchar>(i,srcImg.cols-j-1)=uchar(0); } } for(int i=0; i<srcImg.cols; i++) { for(int j=0; j<5; j++) { srcImg.at<uchar>(j,i)=uchar(0); srcImg.at<uchar>(srcImg.rows-j-1,i)=uchar(0); } } //detect lsd lines in an input image; int cols=srcImg.rows; int rows=srcImg.cols; double* lsd_srcImg=new double[cols*rows]; for (int i=0; i<cols; i++) { for (int j=0; j<rows; j++) { lsd_srcImg[i+j*cols]=static_cast<double>(srcImg.at<uchar>(i,j)); } } double* lsd_dstImg; int n=0; lsd_dstImg=lsd(&n,lsd_srcImg,cols,rows); cout<<"finished the lsd detection!\n"; vector<LSDline> lsdLine; for (int i=0; i<n; i++) { LSDline lsdLine_tmp; lsdLine_tmp.lineBegin.y=lsd_dstImg[i*7+0]; lsdLine_tmp.lineBegin.x=lsd_dstImg[i*7+1]; lsdLine_tmp.lineEnd.y=lsd_dstImg[i*7+2]; lsdLine_tmp.lineEnd.x=lsd_dstImg[i*7+3]; lsdLine_tmp.width=lsd_dstImg[i*7+4]; lsdLine_tmp.p=lsd_dstImg[i*7+5]; lsdLine_tmp.log_nfa=lsd_dstImg[i*7+6]; lsdLine_tmp.tagBegin=1; lsdLine_tmp.tagEnd=1; cout<<lsdLine_tmp.lineBegin.x<<" "<<lsdLine_tmp.lineBegin.y<<" "<<lsdLine_tmp.lineEnd.x<<" "<<lsdLine_tmp.lineEnd.y<<endl; float distThreshold=12; if(sqrt((lsdLine_tmp.lineBegin.x-lsdLine_tmp.lineEnd.x)*(lsdLine_tmp.lineBegin.x-lsdLine_tmp.lineEnd.x)+ (lsdLine_tmp.lineBegin.y-lsdLine_tmp.lineEnd.y)*(lsdLine_tmp.lineBegin.y-lsdLine_tmp.lineEnd.y))>distThreshold) { lsdLine.push_back(lsdLine_tmp); } } cout<<"the detected lsd lines' number is: "<<lsdLine.size()<<endl; //define the img1 to display the detected LSD lines and junctions; Mat img1(img.size(),CV_8UC3,Scalar::all(0)); delete[] lsd_srcImg; displayLSDline(lsdLine,img1); //imwrite("img1.bmp",img1); vector<Ljunct> Jlist; vector<LsdJunction> lsdJunction; if(LSD2Junct(lsdLine,Jlist,lsdJunction,search_distance,img)) { cout<<"transform successfully!\n"; } else { cout<<"cannot form L-junctions from LSD lines!\n"; //for processing, we also need to write the the detect result; char c='_'; int name_end=str.find(c,0); string ori_name_tmp1=str.substr(7,name_end-7); // char* ch2=".bmp"; // int location=str.find(ch2,0); // string ori_name_tmp1 = str.substr(7,location-7); string dst_tmp = "./DetectResultOri/"+ori_name_tmp1; string ori_name_tmp="./OrigImg/"+ori_name_tmp1; // Mat oriImg_tmp = imread(ori_name_tmp.c_str(),CV_LOAD_IMAGE_COLOR); // imwrite(dst_tmp,oriImg_tmp); string filestring_tmp="./dstFile/"+str.substr(srcImgDir.size(),str.size()-4)+".jpg.txt"; ofstream file_out(filestring_tmp.c_str()); if(!file_out.is_open()) { cout<<"cannot open the txt file!\n"; } string imageName=str.substr(srcImgDir.size(),str.size()-4)+".jpg"; file_out<<imageName<<"\t"<<img.cols<<"\t"<<img.rows<<endl; continue; } //vector<string> code_string1; vector<Ljunct> Jlist_coding; vector<codeStringBoundingBox> code_string; code_string=encodingFromLsdJunction(lsdJunction, Jlist_coding,srcImg); classifyRoadMarking(code_string,srcImg); string str_tmp=str.substr(srcImgDir.size(),str.size()); cout<<"!!!!!the Jlist_coding size is: "<<Jlist_coding.size()<<endl<<endl; displayLjunct(Jlist_coding,img1,str_tmp); DrawBoundingBox(code_string,img,str_tmp); //drawing the bounding box in original image; char c='_'; int name_end=str.find(c,0); // string ori_name=str.substr(7,name_end-7); char* ch=".bmp"; int location=str.find(ch,0); cout<<"the find .bmp in "<<str<<" is in "<<location<<endl; string ori_name=str.substr(7,location-7); cout<<ori_name<<endl; string ori_img="./OrigImg/"+ori_name+".JPG"; Mat oriImg=imread(ori_img.c_str(),CV_LOAD_IMAGE_COLOR); if(!oriImg.data) { cout<<"cannot load the original image!\n"; //return 0; char ch; cin.get(ch); continue; } /* Point2f imgP1=Point2f(219,668); Point2f imgP2=Point2f(452,469); Point2f imgP3=Point2f(622,472); Point2f imgP4=Point2f(882,681); Point2f imgP5=Point2f(388,520); Point2f imgP6=Point2f(688,523); Point2f imgP7=Point2f(454,538); Point2f imgP8=Point2f(645,539); Point2f imgP9=Point2f(508,486); Point2f imgP10=Point2f(573,509); Point2f imgP[10]= {imgP1,imgP2,imgP3,imgP4,imgP5,imgP6,imgP7,imgP8,imgP9,imgP10}; Point2f objP1=Point2f(250,900); Point2f objP2=Point2f(250,100); Point2f objP3=Point2f(800,100); Point2f objP4=Point2f(800,900); Point2f objP5=Point2f(250,550); Point2f objP6=Point2f(800,550); Point2f objP7=Point2f(400,625); Point2f objP8=Point2f(650,625); Point2f objP9=Point2f(450,300); Point2f objP10=Point2f(600,475); Point2f objP[10]= {objP1,objP2,objP3,objP4,objP5,objP6,objP7,objP8,objP9,objP10}; */ vector<Point2f> imgP; imgP.push_back(Point2f(300,450)); imgP.push_back(Point2f(700,450)); imgP.push_back(Point2f(465,450)); imgP.push_back(Point2f(535,450)); imgP.push_back(Point2f(260,820)); imgP.push_back(Point2f(740,820)); vector<Point2f> objP; objP.push_back(Point2f(0,0)); objP.push_back(Point2f(1000,0)); objP.push_back(Point2f(400,0)); objP.push_back(Point2f(600,0)); objP.push_back(Point2f(400,1000)); objP.push_back(Point2f(600,1000)); //Mat H=getPerspectiveTransform(objP,imgP); Mat H=findHomography(objP,imgP,CV_RANSAC); DrawBoundingBox_Ori(code_string,oriImg,ori_name,H,str_tmp); } return 0; }
int main( int argc, char** argv){ /*colors*/ CvScalar green = CV_RGB(0,255,0); CvScalar white = CV_RGB(255,255,255); CvScalar black = CV_RGB(0,0,0); CvScalar red = CV_RGB(255,0,0); CvScalar blue = CV_RGB(0,0,255); cvNamedWindow( "opencv on acid",CV_WINDOW_AUTOSIZE); cvNamedWindow( "lsd",CV_WINDOW_AUTOSIZE); CvCapture* capture; if ( argc == 1 ) { capture = cvCreateCameraCapture (0); } else { capture = cvCreateFileCapture (argv[1]); } assert( capture != NULL ); IplImage* frame; image_double image; int width, height, i, j; while (1) { frame = cvQueryFrame( capture ); if( !frame ) break; /* get image properties */ width = frame->width; height = frame->height; /* create new image for the grayscale version */ IplImage* frameBW = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 1 ); /*convert to grayscale*/ cvCvtColor( frame , frameBW, CV_RGB2GRAY); /*cast into lsd image struct*/ image = new_image_double(width, height); uchar* data = (uchar*)frameBW->imageData; for (i=0;i<height;i++){ for(j=0;j<width;j++){ image->data[ j + i * width ] = data[j + i*width]; }; }; /*run lsd*/ ntuple_list ntl; ntl = lsd(image); free_image_double(image); /*filter lsd segments*/ int degrees_thr = 20; // degrees of threshold int distance_thr = 49; // 7 pixels ntuple_list ntlFilt = new_ntuple_list(5); filterSegments( ntl, ntlFilt , distance_thr); /********************/ //printf("A ver: %3.2f\n",ntl->values[1]); /*draw segments on frame and frameLsd*/ IplImage* frameLsd = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 3 ); cvSet(frameLsd, black, 0); for (j=0; j<ntl->size ; j++){ //define segment end-points CvPoint pt1 = cvPoint(ntl->values[ 0 + j * ntl->dim ],ntl->values[ 1 + j * ntl->dim ]); CvPoint pt2 = cvPoint(ntl->values[ 2 + j * ntl->dim ],ntl->values[ 3 + j * ntl->dim ]); // draw line on frame cvLine(frame,pt1,pt2,white,1.5,8,0); // draw line on frameLsd cvLine(frameLsd,pt1,pt2,white,1.5,8,0); } /*draw segments on actual frameLsdFilt*/ IplImage* frameLsdFilt = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 3 ); cvSet(frameLsdFilt, black, 0); j = 0; for (j=0; j<ntlFilt->size ; j++){ //define segment end-points CvPoint pt1 = cvPoint(ntlFilt->values[ 0 + j * ntlFilt->dim ],ntlFilt->values[ 1 + j * ntlFilt->dim ]); CvPoint pt2 = cvPoint(ntlFilt->values[ 2 + j * ntlFilt->dim ],ntlFilt->values[ 3 + j * ntlFilt->dim ]); // draw line on frameLsd cvLine(frameLsdFilt,pt1,pt2,white,1.5,8,0); } cvShowImage("opencv on acid",frame); cvShowImage("lsd",frameLsd); cvShowImage("lsd filtrado",frameLsdFilt); char c = cvWaitKey(25); if( c == 27 ) break; } cvReleaseCapture( &capture ); cvDestroyWindow( "opencv on acid"); cvDestroyWindow( "lsd"); cvDestroyWindow( "lsd filtrado"); }
//返回直线个数,计算k,b int lineDetect(double* k, double* b) { int i, j; UINT32T col; UINT32T row; image_double image_LSD = new_image_double(C, R); ntuple_list detected_lines; int dim; UINT8T pGray[SIZE]; double angletemp; double length; double temp_k, temp_b; int counter = 0; #ifdef WIN32 CvPoint start_pt; CvPoint end_pt; #endif // LSD算法检测直线 for (row = 1; row<(R - 1); row++) { for (col = 1; col<(C - 1); col++) { image_LSD->data[row*C + col] = image_Gray[row*C + col];//im_gray是灰度图像,没有颜色通道 } } detected_lines = lsd(image_LSD);//detected_lines中存储提取直线的首位坐标及宽度,具体意义见说明文档 free_image_double(image_LSD); // LSD结果显示 #ifdef WIN32 memset(pGray, 0, SIZE); cvSetData(image_1ch, pGray, C); #endif dim = detected_lines->dim; //printf("Number of lines detected = %d", detected_lines->size); //double angle[770]; //detected_lines->size = 770 for (j = 0; j < detected_lines->size; j++) { //cvLine(res_im,start_pt,end_pt,CV_RGB(j%255,(5*j)%255,(9*j)%255),1,CV_AA); //�� temp_k = (detected_lines->values[j*dim + 1] - detected_lines->values[j*dim + 3]) / (detected_lines->values[j*dim + 0] - detected_lines->values[j*dim + 2]); angletemp = (int)(atan(temp_k) * 180 / 3.1416); //检查倾角是否满足条件 if (angletemp>20 || angletemp < -20) continue; //判断长度是否满足条件 length = sqrt((detected_lines->values[j*dim + 2] - detected_lines->values[j*dim + 0]) * (detected_lines->values[j*dim + 2] - detected_lines->values[j*dim + 0]) + (detected_lines->values[j*dim + 3] - detected_lines->values[j*dim + 1])*(detected_lines->values[j*dim + 3] - detected_lines->values[j*dim + 1])); if (length < (double)C / 8) continue; //printf("j = %d, length = %f\n", j, length); //对满足两个条件的线段,求直线方程 k[counter] = temp_k; temp_b = detected_lines->values[j*dim + 1] - temp_k * detected_lines->values[j*dim + 0]; b[counter] = temp_b; counter++; #ifdef WIN32 start_pt = cvPoint((int)detected_lines->values[j*dim + 0], (int)detected_lines->values[j*dim + 1]); end_pt = cvPoint((int)detected_lines->values[j*dim + 2], (int)detected_lines->values[j*dim + 3]); cvLine(image_1ch, start_pt, end_pt, CV_RGB(0, 0, 255), 1, CV_AA, 0); #endif } #ifdef WIN32 cvNamedWindow("LSD", 0); cvShowImage("LSD", image_1ch); cvWaitKey(0); #endif return counter; }