tar -xjf OpenCV-*.tar.bz2 && cd OpenCV-* && cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ./ && make && make install
cd samples/c/ && chmod +x build_all.sh && ./build_all.sh
#include <cv.h> #include <highgui.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char* argv[]) { CvCapture* capture = cvCaptureFromCAM(0);// CvCapture( , ), capture. cvCaptureFromCAM, 0 , . IplImage* image = cvQueryFrame( capture ); // ( image) cvNamedWindow("image window", 1); // image window for(;;) // { image = cvQueryFrame( capture ); // image cvShowImage("image window", image);// (image window) , cvWaitKey(10); // 10 . , . , -, , . } }
gcc -ggdb `pkg-config --cflags opencv` -o `basename test.c .c` test.c `pkg-config --libs opencv`
Again, to be honest, I do not quite understand what this team is doing. Well collects. Why exactly? char c = cvWaitKey(10); // . if (c == 113 || c == 81) //, . 113 81 - "q" - . { cvReleaseCapture( &capture ); // . cvDestroyWindow("capture"); // , ! return 0; // . }
CvFont font; // "" cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 1.0, 1.0, 1,1,8); // - , , struct timeval tv0; //- . int fps=0; int fps_sec=0; char fps_text[2]; int now_sec=0;// ... gettimeofday(&tv0,0); // now_sec=tv0.tv_sec; // if (fps_sec == now_sec) //, , { fps++; // , ( , .) } else { fps_sec=now_sec; // , snprintf(fps_text,254,"%d",fps); // FPS fps=0; // } cvPutText(image, fps_text, cvPoint(5, 20), &font, CV_RGB(255,255,255));// (image) 520, , , , , .
#include "opencv2/video/tracking.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc_c.h" #include <time.h> #include <stdio.h> #include <ctype.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <errno.h> int main(int argc, char** argv) { IplImage* image = 0; CvCapture* capture = 0; struct timeval tv0; int fps=0; int fps_sec=0; int now_sec=0; char fps_text[2]; CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 1.0, 1.0, 1,1,8); capture = cvCaptureFromCAM(0); cvNamedWindow( "Motion", 1 ); for(;;) { IplImage* image = cvQueryFrame( capture ); gettimeofday(&tv0,0); now_sec=tv0.tv_sec; if (fps_sec == now_sec) { fps++; } else { fps_sec=now_sec; snprintf(fps_text,254,"%d",fps); fps=0; } cvPutText(image, fps_text, cvPoint(5, 20), &font, CV_RGB(255,255,255)); cvShowImage( "Motion", image ); if( cvWaitKey(10) >= 0 ) break; } cvReleaseCapture( &capture ); cvReleaseImage(&image); cvDestroyWindow( "Motion" ); return 0; }
int dig_key=0;//, int region_coordinates[10][4]; // , . ... char c = cvWaitKey(20); // . if (c <=57 && c>= 48) //, { dig_key=c-48; //key "0123456789" // , . } cvSetMouseCallback( "Motion", myMouseCallback, (void*) image); //, myMouseCallback , Motion image if (region_coordinates[dig_key][0] != 0 && region_coordinates[dig_key][1] != 0 && region_coordinates[dig_key][2] == 0 && region_coordinates[dig_key][3] == 0) // . - . cvRectangle(image, cvPoint(region_coordinates[dig_key][0],region_coordinates[dig_key][1]), cvPoint(region_coordinates[dig_key][0]+1,region_coordinates[dig_key][1]+1), CV_RGB(0,0,255), 2, CV_AA, 0 ); if (region_coordinates[dig_key][0] != 0 && region_coordinates[dig_key][1] != 0 && region_coordinates[dig_key][2] != 0 && region_coordinates[dig_key][3] != 0) // - . cvRectangle(image, cvPoint(region_coordinates[dig_key][0],region_coordinates[dig_key][1]), cvPoint(region_coordinates[dig_key][2],region_coordinates[dig_key][3]), CV_RGB(0,0,255), 2, CV_AA, 0 ); void myMouseCallback( int event, int x, int y, int flags, void* param) // , { IplImage* img = (IplImage*) param; // . , switch( event ){ // case CV_EVENT_MOUSEMOVE: break; // . , , : printf("%dx %d\n", x, y); case CV_EVENT_LBUTTONDOWN: // if (region_coordinates[dig_key][0] != 0 && region_coordinates[dig_key][1] != 0 && region_coordinates[dig_key][2] == 0 && region_coordinates[dig_key][3] == 0) // ( - ), - { region_coordinates[dig_key][2]=x; //dig_key - , . . region_coordinates[dig_key][3]=y; } if (region_coordinates[dig_key][0] == 0 && region_coordinates[dig_key][1] == 0)// ( ), . { region_coordinates[dig_key][0]=x; region_coordinates[dig_key][1]=y; } break; } }
#include "opencv2/video/tracking.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc_c.h" #include <time.h> #include <stdio.h> #include <ctype.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <errno.h> int dig_key=0; int region_coordinates[10][4]; void myMouseCallback( int event, int x, int y, int flags, void* param) { IplImage* img = (IplImage*) param; switch( event ){ case CV_EVENT_MOUSEMOVE: //printf("%dx %d\n", x, y); break; case CV_EVENT_LBUTTONDOWN: //printf("%dx %d\n", region_coordinates[dig_key][0], region_coordinates[dig_key][1]); if (region_coordinates[dig_key][0] != 0 && region_coordinates[dig_key][1] != 0 && region_coordinates[dig_key][2] == 0 && region_coordinates[dig_key][3] == 0) { region_coordinates[dig_key][2]=x; region_coordinates[dig_key][3]=y; } if (region_coordinates[dig_key][0] == 0 && region_coordinates[dig_key][1] == 0) { region_coordinates[dig_key][0]=x; region_coordinates[dig_key][1]=y; } break; case CV_EVENT_RBUTTONDOWN: break; case CV_EVENT_LBUTTONUP: break; } } int main(int argc, char** argv) { IplImage* image = 0; CvCapture* capture = 0; struct timeval tv0; int fps=0; int fps_sec=0; int now_sec=0; char fps_text[2]; CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 1.0, 1.0, 1,1,8); capture = cvCaptureFromCAM(0); cvNamedWindow( "Motion", 1 ); for(;;) { IplImage* image = cvQueryFrame( capture ); gettimeofday(&tv0,0); now_sec=tv0.tv_sec; if (fps_sec == now_sec) { fps++; } else { fps_sec=now_sec; snprintf(fps_text,254,"%d",fps); fps=0; } cvSetMouseCallback( "Motion", myMouseCallback, (void*) image); if (region_coordinates[dig_key][0] != 0 && region_coordinates[dig_key][1] != 0 && region_coordinates[dig_key][2] == 0 && region_coordinates[dig_key][3] == 0) cvRectangle(image, cvPoint(region_coordinates[dig_key][0],region_coordinates[dig_key][1]), cvPoint(region_coordinates[dig_key][0]+1,region_coordinates[dig_key][1]+1), CV_RGB(0,0,255), 2, CV_AA, 0 ); if (region_coordinates[dig_key][0] != 0 && region_coordinates[dig_key][1] != 0 && region_coordinates[dig_key][2] != 0 && region_coordinates[dig_key][3] != 0) cvRectangle(image, cvPoint(region_coordinates[dig_key][0],region_coordinates[dig_key][1]), cvPoint(region_coordinates[dig_key][2],region_coordinates[dig_key][3]), CV_RGB(0,0,255), 2, CV_AA, 0 ); cvPutText(image, fps_text, cvPoint(5, 20), &font, CV_RGB(255,255,255)); cvShowImage( "Motion", image ); char c = cvWaitKey(20); if (c <=57 && c>= 48) { dig_key=c-48; //key "0123456789" } } cvReleaseCapture( &capture ); cvReleaseImage(&image); cvDestroyWindow( "Motion" ); return 0; }
FILE *settings_file; FILE* fd = fopen("regions.bin", "rb"); // . "rb" - if (fd == NULL) { printf("Error opening file for reading\n"); // FILE* fd = fopen("regions.bin", "wb"); // if (fd == NULL) { printf("Error opening file for writing\n"); } else { fwrite(region_coordinates, 1, sizeof(region_coordinates), fd); // - fclose(fd); // printf("File created, please restart program\n"); } return 0; } size_t result = fread(region_coordinates, 1, sizeof(region_coordinates), fd); // if (result != sizeof(region_coordinates)) // printf("Error size file\n"); // fclose(fd); // FILE* fd = fopen("regions.bin", "wb"); // . "wb" - if (fd == NULL) // printf("Error opening file for writing\n"); // fwrite(region_coordinates, 1, sizeof(region_coordinates), fd); // fclose(fd); //
cvCircle( dst, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 );
center = cvPoint( (comp_rect.x + comp_rect.width/2), (comp_rect.y + comp_rect.height/2) );
int i_mass; // for (i_mass = 0; i_mass <= 9; i_mass++) // , . { if( comp_rect.x + comp_rect.width/2 <= region_coordinates[i_mass][2] && comp_rect.x + comp_rect.width/2 >= region_coordinates[i_mass][0] && comp_rect.y + comp_rect.height/2 <= region_coordinates[i_mass][3] && comp_rect.y + comp_rect.height/2 >= region_coordinates[i_mass][1] ) //, , -. { cvRectangle(dst, cvPoint(region_coordinates[i_mass][0],region_coordinates[i_mass][1]), cvPoint(region_coordinates[i_mass][2],region_coordinates[i_mass][3]), CV_RGB(0,0,255), 2, CV_AA, 0 ); // , , , . printf("Detect motion in region %d\n",i_mass); // } }
Source: https://habr.com/ru/post/200804/
All Articles