gpt4 book ai didi

opencv - 将数据保存到opencv中的文件

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:42 24 4
gpt4 key购买 nike

我需要将连接组件的区域保存为文本文件,并且我编写了如下代码,但我不知道哪里出错了。谁能帮我找出问题所在?

imagefet=cvCreateImage(cvGetSize(lab),IPL_DEPTH_8U,3);
CvMemStorage* contour_storage = cvCreateMemStorage(0);
CvSeq* contours;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.6f, 0.6f, 0, 2);
cvFindContours(lab, contour_storage, &contours, sizeof (CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
cvZero(imagefet);

FILE *file=fopen("mytxt.txt","W");
for( int ncount=1; contours != NULL; contours = contours->h_next, ncount++)
{
CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
cvDrawContours( imagefet, contours, color, CV_RGB(255,255,255), -1, CV_FILLED, 8 ,cvPoint(0,0));
int area = abs(cvContourArea(contours, CV_WHOLE_SEQ));
fprintf(file,"%d",area);
}
fclose(file);

谢谢

最佳答案

您对错误的描述不是很具体。

如果您的文件无法打开,我建议您使用小写的 w 作为模式标志。我从未见过使用大写字母,我不知道这是否被允许。

如果您的文件中全是数字而没有空格,那是因为您没有写出任何空格。尝试向 fprintf 调用添加换行符 \n:

fprintf(file,"%d\n",area);        

在这方面,您应该真正使用文本模式来创建文件 "wt" 而不是 "w"

您应该做的另一件事是测试文件是否打开成功,方法是在调用fopen 后检查file 是否为NULL。

关于opencv - 将数据保存到opencv中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14431507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com