往文件中写数据~ 从文件中读取数据~ 文件读取数据,测试直线拟合~
2013-01-05 13:19
148 查看
随时打开文件, add 新的数据。
循环操作,存储了很多数据“ 如下:
[29, 35]
[29, 33]
[30, 34]
[29, 33]
[29, 34]
[29, 34]
[29, 34]
[28, 33]
[30, 34]
[30, 34]
[29, 33]
[28, 33]
[29, 33]
[29, 33]
[29, 34]
[29, 33]
程序中利用文件保存数据,方便在其他地方存取使用。
(blog 质量越来越低了=_+ 吼吼 可是记录这里比较方便呢。随时拿来使用~~~~)
打开文件,读取数据。
读取上述数据:
【额。。。。上述数据········不会读取出来=_= 好挫, 于是赶紧滴重新保存了一下数据,格式为:】
29 35
29 33
30 34
29 33
29 34
29 34
29 34
28 33
30 34
30 34
29 33
28 33
ok 做这个,主要是为了测试。
-----
吼吼
反正,读取了我的数据,加入到上周测试的那个 直线拟合 中后,如下:
PS:
遇到的 一个很纠结的问题, 开始的时候 没有加
这一句,就只能在最后显示一帧图像。
额,刚开始我还以为是跑的太快,来不及显示呢。
所以还
#include<windows.h>
Sleep(100);
了一下 O_o
好挫啊,这哪里像是学过opencv的人啊。。。竟然第一个想到的是这个。。。。
不过话又说回来,我们只知道这个
是等待20ms, 那么与Sleep(20) 为什么不行呢?为什么Sleep 还是一片灰色,无法看到图片呢。。。。
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/proud.gif)
吼吼,查了查才知道 不能小觑 cvWaitKey 啊··
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/mad.gif)
参考cvWaitKey http://blog.csdn.net/zm_nong/article/details/7519238
File *fp; if((fp=fopen("data.txt","a"))==NULL) { printf("Create File failure"); exit(1); } fprintf(fp,"[%d, %d]\n", x, y); fclose(fp);
循环操作,存储了很多数据“ 如下:
[29, 35]
[29, 33]
[30, 34]
[29, 33]
[29, 34]
[29, 34]
[29, 34]
[28, 33]
[30, 34]
[30, 34]
[29, 33]
[28, 33]
[29, 33]
[29, 33]
[29, 34]
[29, 33]
程序中利用文件保存数据,方便在其他地方存取使用。
(blog 质量越来越低了=_+ 吼吼 可是记录这里比较方便呢。随时拿来使用~~~~)
打开文件,读取数据。
读取上述数据:
【额。。。。上述数据········不会读取出来=_= 好挫, 于是赶紧滴重新保存了一下数据,格式为:】
29 35
29 33
30 34
29 33
29 34
29 34
29 34
28 33
30 34
30 34
29 33
28 33
FILE *fp = fopen("data.txt", "r"); while( !feof(fp) ) { v++; int x, y; fscanf(fp,"%d",&x); fscanf(fp,"%d",&y); ·········不停的读取int型数据,知道文件结束。
ok 做这个,主要是为了测试。
-----
吼吼
反正,读取了我的数据,加入到上周测试的那个 直线拟合 中后,如下:
#include "stdafx.h" #include "stdio.h" #include "cv.h" #include "highgui.h" void put_data_into_array(CvPoint dataarray[], CvPoint data, int n) { for(int i = 0; i < n - 1; i++) dataarray[i] = dataarray[i+1]; dataarray[n - 1] = data; } int _tmain(int argc, _TCHAR* argv[]) { IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 ); cvNamedWindow( "fitline", 1 ); CvPoint pt1, pt2; //直线的两个端点 CvPoint* points = (CvPoint*)malloc( 6 * sizeof(points[0])); //存放随机产生的点点,数目为count CvMat pointMat = cvMat( 1, 6, CV_32SC2, points ); //点集, 存储count个随机点points float line[4]; //输出的直线参数。2D 拟合情况下,它是包含 4 个浮点数的数组 (vx, vy, x0, y0) //其中 (vx, vy) 是线的单位向量而 (x0, y0) 是线上的某个点 float t; FILE *fp = fopen("data.txt", "r"); int v = 0; while( !feof(fp) ) { v++; int x, y; fscanf(fp,"%d",&x); fscanf(fp,"%d",&y); put_data_into_array(points, cvPoint(x, y), 6); printf("[%d, %d]\n", x, y); if( v > 6 ) { // find the optimal line 曲线拟合 cvFitLine( &pointMat, CV_DIST_L1, 1, 0.001, 0.001, line ); //画出线段的两个端点(避免线太短,以线上一个随机点向两侧延伸line[0]*t ) t = (float)(img->width + img->height) ; pt1.x = cvRound(line[2] - line[0]*t); pt1.y = cvRound(line[3] - line[1]*t); pt2.x = cvRound(line[2] + line[0]*t); pt2.y = cvRound(line[3] + line[1]*t); cvZero( img ); cvLine( img, pt1, pt2, CV_RGB(0,255,0), 3, CV_AA, 0 ); cvCircle( img, cvPoint(x, y), 2, CV_RGB(255, 0, 0), CV_FILLED, CV_AA, 0 ); char key = cvWaitKey(20); cvShowImage( "fitline", img ); } } fclose(fp); free( points ); cvWaitKey(-1); cvDestroyWindow( "fitline" ); return 0; }
PS:
遇到的 一个很纠结的问题, 开始的时候 没有加
char key = cvWaitKey(20)
这一句,就只能在最后显示一帧图像。
额,刚开始我还以为是跑的太快,来不及显示呢。
所以还
#include<windows.h>
Sleep(100);
了一下 O_o
好挫啊,这哪里像是学过opencv的人啊。。。竟然第一个想到的是这个。。。。
不过话又说回来,我们只知道这个
cvWaitKey(20)
是等待20ms, 那么与Sleep(20) 为什么不行呢?为什么Sleep 还是一片灰色,无法看到图片呢。。。。
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/proud.gif)
吼吼,查了查才知道 不能小觑 cvWaitKey 啊··
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/mad.gif)
参考cvWaitKey http://blog.csdn.net/zm_nong/article/details/7519238
;
相关文章推荐
- 自动化测试数据驱动之xml文件读取
- Erlang静态键值对(key-value)数据分别写入erl文件与ETS表读取的测试
- Jmeter使用CSV文件读取大量测试数据
- 数据读取 ,在cmd dos 命令下输入文件地址 进行测试
- linux命令dd:数据备份与恢复,切割还原大文件,测试读取写入磁盘速度
- 封装properties从配置文件读取测试用例输入数据
- Excel大文件时读取内存溢出的解决方案,测试50M的Excel通过。
- ios开发系列之文件的写入读取,NSUserDefaults存储数据,NSFileManager操作文件和文件夹
- 从XML文件中读取数据绑定到DropDownList
- vbs读取文件内的信息将对应数据拷贝到其他路径
- POI 读取excle文件的数据
- Matlab批量读取元胞数组中数据保存到txt文件中(WIDER FACE数据集中,读取人脸标注矩形框信息)
- properties文件读取测试3
- TP5+PHPexcel导入xls,xlsx文件读取数据
- Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
- 用Java简单的读取pdf文件中的数据
- FilePersistenceTest 从文件读取数据
- 用c#读取excel文件缺少第一行数据解决方法
- Java 高效读取大数据文件—最优方法
- PHP读取并输出XML文件数据的简单实现方法