往文件中写数据~ 从文件中读取数据~ 文件读取数据,测试直线拟合~

14 篇文章 0 订阅

随时打开文件, add 新的数据。


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 还是一片灰色,无法看到图片呢。。。。得意

吼吼,查了查才知道 不能小觑  cvWaitKey  啊··生气 

参考cvWaitKey  http://blog.csdn.net/zm_nong/article/details/7519238




  ; 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值