Skip to content

Instantly share code, notes, and snippets.

@ollewelin
Created September 14, 2016 17:48
Show Gist options
  • Save ollewelin/79e1675633326d6b9172b055210d7fea to your computer and use it in GitHub Desktop.
Save ollewelin/79e1675633326d6b9172b055210d7fea to your computer and use it in GitHub Desktop.
//Generate Negative samples
//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>
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;
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);
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