隨時開啟檔案, 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])); //存放隨機產生的點點,數目為countCvMat pointMat = cvMat( 1, 6, CV_32SC2, points ); //點集, 儲存count個隨機點pointsfloat 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
;