#include #include #include #include "cv.h" #include "highgui.h" #include #include #include #include #define BW_THRESHOLD 16 using namespace std; using namespace cv; void output_pixels(IplImage* Image1); FILE *fp; int main( int argc, const char** argv ) { CvCapture* capture = 0; IplImage* dst; int frame_num=0; int blocksize=35; //int odd_time; //if (odd) odd_time=1, else odd_time=0 char file_name[20]; struct timespec ts; int clk_id = CLOCK_REALTIME; if(argc>1) blocksize = atoi(argv[1]); if(blocksize>143) blocksize=143; if(blocksize<3) blocksize=3; if(blocksize%2==0) blocksize=blocksize+1; capture = cvCaptureFromCAM( 0 ); //0=default, -1=any camera, 1..99=your camera cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 176 ); cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 144); dst = cvCreateImage(cvSize(176, 144), 8, 1 ); if(!capture) cout << "No camera detected" << endl; if( capture ) { // cout << "In capture ..." << endl; while(1) { clock_gettime(clk_id, &ts); int t_500ms = ts.tv_nsec / 500000000; //Unit is 0.5s sprintf(file_name,"/tmp/data%d.txt",t_500ms); fp = fopen(file_name,"w"); IplImage* iplImg = cvQueryFrame( capture ); cvCvtColor(iplImg, dst, CV_RGB2GRAY); //dst is a gray image. cvAdaptiveThreshold(dst, dst, 4, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, blocksize, 5 ); //The value of a white pixel is 4, and the value of a black pixel is 0. //cvSaveImage( "pic.jpg", dst); output_pixels(dst); fclose(fp); while(1) { clock_gettime(clk_id, &ts); int new_t_500ms = ts.tv_nsec / 500000000; //Unit is 0.5s if(t_500ms != new_t_500ms) break; //Wait the next 0.5 seconda } //2 frames per second } } } void output_pixels(IplImage* Image1) { int Y1, Y2, Y3, Y4, Y; int BW_Y; for(int i=0;i<95;i=i+2) //2 Y map to 1 Y of Nokia 5110 //The max Y of Nokia is 47 => //The max i is 47*2=94 { for(int j=4;j<172;j=j+2) //2 X map to 1 X of Nokia 5110 //The max X of Nokia is 84 => //One pixel has 3 j values //j=12(=4*3)~515(i=172*3-1) { Y1=(int) Image1->imageData[i*Image1->widthStep+j]; Y2=(int) Image1->imageData[i*Image1->widthStep+j+1]; Y3=(int) Image1->imageData[(i+1)*Image1->widthStep+j]; Y4=(int) Image1->imageData[(i+1)*Image1->widthStep+j+1]; Y=(Y1+Y2+Y3+Y4); //Y=0~16 //printf("%d %d %d %d,",Y1,Y2,Y3,Y4); if(Y>=BW_THRESHOLD) BW_Y=1; //White else BW_Y=0; fprintf(fp,"%d",BW_Y); } fprintf(fp,"\n"); } }