int main()
{
image_double image;
ntuple_list out;
unsigned int xsize,ysize,depth;
int x,y,i,j,width,height,step;
uchar *p;
IplImage* img = 0;
IplImage* dst = 0;
img = cvLoadImage("D:\\Ahram.jpg",CV_LOAD_IMAGE_COLOR);
width = img->width;
height = img->height;
dst=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
cvCvtColor(img,dst,CV_RGB2GRAY);
width=dst->width;
height=dst->height;
step=dst->widthstep;
p=(uchar*)dst->imageData;
image=new_image_double(dst->width,dst->height);
xsize=dst->width;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
image->data[i+j*xsize]=p[i*step+j];
}
}
/* call LSD */
out = lsd(dst);
/* print output */
printf("%u line segments found:\n",out->size);
for(i=0;i<out->size;i++)
{
for(j=0;j<out->dim;j++)
printf("%f ",out->values[ i * out->dim + j ]);
printf("\n");
}
/* free memory */
free_image_double(image);
free_ntuple_list(out);
return 0;
}
N.B:它没有错误,但是当我运行它时,它发出了LSD内部错误:无效的图像输入
最佳答案
Each PGM image consists of the following:
1. A "magic number" for identifying the file type.
A pgm image's magic number is the two characters "P5".
2. Whitespace (blanks, TABs, CRs, LFs).
3. A width, formatted as ASCII characters in decimal.
4. Whitespace.
5. A height, again in ASCII decimal.
6. Whitespace.
7. The maximum gray value (Maxval), again in ASCII decimal.
Must be less than 65536, and more than zero.
8. A single whitespace character (usually a newline).
9. A raster of Height rows, in order from top to bottom.
Each row consists of Width gray values, in order from left to right.
Each gray value is a number from 0 through Maxval, with 0 being black
and Maxval being white. Each gray value is represented in pure binary
by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte.
Otherwise, it is 2 bytes. The most significant byte is first.
对于PGM类型P2,像素在文件上是可读的(ASCII),但对于P5,则不是,因为它们将以二进制格式存储。
您应该知道的重要一件事是,这种格式每个像素仅占用1个通道。这意味着PGM只能存储灰色缩放的图片。记住这一点!
现在,如果您使用OpenCV从文件中加载图像,则应使用 CV_LOAD_IMAGE_GRAYSCALE 加载它们:
IplImage* cv_img = cvLoadImage("chairs.png", CV_LOAD_IMAGE_GRAYSCALE);
if(!cv_img)
{
std::cout << "ERROR: cvLoadImage failed" << std::endl;
return -1;
}
但是,如果在此函数上使用任何其他标志,或者使用
cvCreateImage()
创建图像,或者要从相机或类似设备捕获帧,则需要使用cvCvtColor()
将每个帧转换为其灰度表示形式。我下载了lsd-1.5,并注意到那里有一个示例显示了如何使用该库。其中一个名为 lsd_cmd.c 的源代码文件会手动读取一个PGM文件,并用它组装一个
image_double
。实现此技巧的函数是read_pgm_image_double()
,它从PGM文件中读取像素并将其存储在image->data
中。这很重要,因为如果以下操作不起作用,则必须迭代IplImage
的像素并自己进行。将灰度图像成功加载到
IplImage* cv_img
后,您可以尝试使用以下方法创建所需的结构:image_double image = new_image_double(cv_img->width, cv_img->height);
image->data = (double) cv_img->imageData;
如果这不起作用,则需要检查我上面建议的文件,并遍历
cv_img->imageData
的像素,然后将它们一一复制(进行正确的类型转换)到image->data
。最后,使用完该资源后,请不要忘记释放该资源:
free_image_double(image);