вот пример кода с использованием LSD с opencv
#include "lsd.h"
void Test_LSD(IplImage* img)
{
IplImage* grey = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvCvtColor(img, grey, CV_BGR2GRAY);
image_double image;
ntuple_list out;
unsigned int x,y,i,j;
image = new_image_double(img->width,img->height);
for(x=0;x<grey->width;x++)
for(y=0;y<grey->height;y++)
{
CvScalar s= cvGet2D(grey,y,x);
double pix= s.val[0];
image->data[ x + y * image->xsize ]= pix; /* image(x,y) */
}
/* call LSD */
out = lsd(image);
//out= lsd_scale(image,1);
/* print output */
printf("%u line segments found:\n",out->size);
vector<Line> vec;
for(i=0;i<out->size;i++)
{
//for(j=0;j<out->dim;j++)
{
//printf("%f ",out->values[ i * out->dim + j ]);
Line line;
line.x1= out->values[ i * out->dim + 0];
line.y1= out->values[ i * out->dim + 1];
line.x2= out->values[ i * out->dim + 2];
line.y2= out->values[ i * out->dim + 3];
vec.push_back(line);
}
//printf("\n");
}
IplImage* black= cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);
cvZero(black);
draw_lines(vec,black);
/*cvNamedWindow("img", 0);
cvShowImage("img", img);*/
cvSaveImage("lines_detect.png",black/*img*/);
/* free memory */
free_image_double(image);
free_ntuple_list(out);
}
или так
IplImage* get_lines(IplImage* img,vector<Line>& vec_lines)
{
//to grey
//IplImage* grey = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
//cvCvtColor(img, grey, CV_BGR2GRAY);
image_double image;
ntuple_list out;
unsigned int x,y,i,j;
image = new_image_double(img->width,img->height);
for(x=0;x</*grey*/img->width;x++)
for(y=0;y</*grey*/img->height;y++)
{
CvScalar s= cvGet2D(/*grey*/img,y,x);
double pix= s.val[0];
image->data[ x + y * image->xsize ]= pix;
}
/* call LSD */
out = lsd(image);
//out= lsd_scale(image,1);
/* print output */
//printf("%u line segments found:\n",out->size);
//vector<Line> vec;
for(i=0;i<out->size;i++)
{
//for(j=0;j<out->dim;j++)
{
//printf("%f ",out->values[ i * out->dim + j ]);
Line line;
line.x1= out->values[ i * out->dim + 0];
line.y1= out->values[ i * out->dim + 1];
line.x2= out->values[ i * out->dim + 2];
line.y2= out->values[ i * out->dim + 3];
/*vec*/vec_lines.push_back(line);
}
//printf("\n");
}
IplImage* black= cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
cvZero(black);
for(int i=0;i<vec_lines.size();++i)
{
//if(vec[i].x1==vec[i].x2||vec[i].y1==vec[i].y2)
cvLine(black,cvPoint(vec_lines[i].x1,vec_lines[i].y1),cvPoint(vec_lines[i].x2,vec_lines[i].y2),CV_RGB(255,255,255),1, CV_AA);
}
/*cvNamedWindow("img", 0);
cvShowImage("img", img);*/
//cvSaveImage("lines_detect.png",black/*img*/);
/* free memory */
//cvReleaseImage(&grey);
free_image_double(image);
free_ntuple_list(out);
return black;
}