Created
September 17, 2016 13:41
-
-
Save ollewelin/c00ddd31618f1c21c93114593586a5d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Generate Negative samples BGR RGB swapped don't know why this was default OpenCV must have changed on my raspbeery pi suddenly after some updates | |
| //File: test_prog.cpp | |
| //You must make a director /positive_data/ where this program could put the pictures in | |
| #include <stdio.h> | |
| #include <ctime> | |
| #include <iostream> | |
| #include <raspicam/raspicam_cv.h> | |
| #include <opencv2/opencv.hpp> | |
| using namespace std; | |
| using namespace cv; | |
| //default capture width and height | |
| //const int FRAME_WIDTH = 640; | |
| //const int FRAME_HEIGHT = 480; | |
| const int FRAME_WIDTH = 320; | |
| const int FRAME_HEIGHT = 240; | |
| const string WindowName = "Image viewer"; | |
| int main () { | |
| //JPG File store | |
| int ret; | |
| char ch; | |
| int files_number = 1000; | |
| // FILE *store_files[files_number]; | |
| FILE *temp_file; | |
| char filename[20]; | |
| //------------- | |
| int camera_start_up_frames = 300; | |
| // FILE *fp[files_number]; | |
| namedWindow(WindowName); | |
| time_t timer_begin,timer_end; | |
| raspicam::RaspiCam_Cv Camera; | |
| cv::Mat image, image_BGR; | |
| int nCount=10; | |
| //set camera params | |
| Camera.set( CV_CAP_PROP_FORMAT, CV_8UC3 ); | |
| Camera.set( CV_CAP_PROP_FRAME_WIDTH, FRAME_WIDTH); | |
| Camera.set( CV_CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT); | |
| //Open camera | |
| cout<<"Opening Camera..."<<endl; | |
| if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;} | |
| //Start capture | |
| cout<<"Capturing "<<nCount<<" frames ...."<<endl; | |
| time ( &timer_begin ); | |
| for ( int i=0; i<camera_start_up_frames; i++ ) { | |
| Camera.grab(); | |
| Camera.retrieve ( image); | |
| imshow(WindowName, image); | |
| printf("Count down to start sample: %d\n", camera_start_up_frames-i); | |
| waitKey(1); | |
| } | |
| for ( int i=0; i<files_number; i++ ) { | |
| Camera.grab(); | |
| Camera.retrieve ( image_BGR); | |
| cv::cvtColor(image_BGR,image,cv::COLOR_BGR2RGB); | |
| imshow(WindowName, image); | |
| cv::imwrite("temporary_file.JPG",image); | |
| //--- Save JPG files ---- | |
| sprintf(filename, "./negative_data/%d.JPG", i); | |
| ret = rename("temporary_file.JPG", filename); | |
| if(ret == 0) | |
| { | |
| printf("File renamed successfully"); | |
| printf("Image save af file: %d", i); | |
| printf(".JPG\n" ); | |
| } | |
| else | |
| { | |
| printf("Error: unable to rename the file \n"); | |
| printf(" You may make a /negative_data/ directory \n"); | |
| } | |
| //--------------------- | |
| //cout<<"Image saved at ./x.JPG"<<endl; | |
| waitKey(100); | |
| cv::imwrite("temporary_file.JPG",image); | |
| } | |
| cout<<"Stop camera..."<<endl; | |
| Camera.release(); | |
| //show time statistics | |
| time ( &timer_end ); | |
| double secondsElapsed = difftime ( timer_end,timer_begin ); | |
| cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl; | |
| //save image | |
| // cv::imwrite("raspicam_cv_image.jpg",image); | |
| cout<<"Image saved at raspicam_cv_image.jpg"<<endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment