Advertisement
Guest User

primary.cpp

a guest
Feb 5th, 2017
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <fstream>
  4. #include <raspicam/raspicam_cv.h>
  5. #include <opencv2/core/core.hpp>
  6. #include <opencv2/imgproc/imgproc.hpp>
  7. #include <opencv2/highgui/highgui.hpp>
  8.  
  9. void set_camera_resolution(raspicam::RaspiCam_Cv camera, int width, int height){
  10.     camera.set(CV_CAP_PROP_FRAME_WIDTH, width);
  11.     camera.set(CV_CAP_PROP_FRAME_HEIGHT, height);
  12. }
  13. void set_camera_fps(raspicam::RaspiCam_Cv camera, double fps){
  14.     camera.set(CV_CAP_PROP_FPS, fps);
  15. }
  16. void record_video(int length, std::string filename){
  17.  
  18. }
  19. void debug(std::string to_print, bool new_line = true){
  20.     if(new_line){
  21.         std::cout << to_print << std::endl;
  22.     }else{
  23.         std::cout << to_print;
  24.     }
  25. }
  26. cv::Mat thumbnail(cv::Mat input){
  27.     cv::Size size(100, 75);
  28.     cv::Mat resized;
  29.     cv::resize(input, resized, size, 0, 0, cv::INTER_CUBIC);
  30.     return resized;
  31. }
  32. cv::Mat snap_thumbnail(raspicam::RaspiCam_Cv camera){
  33.     cv::Mat image_captured;
  34.     debug("Before grabbing");
  35.     camera.grab();
  36.     debug("After grabbing, before retrieving");
  37.     camera.retrieve(image_captured);
  38.     debug("After retrieving");
  39.     return thumbnail(image_captured);
  40. }
  41. void motion_sensing_loop(raspicam::RaspiCam_Cv camera, int threshold_pixel_diff, int threshold_pixel_num){
  42.     cv::Mat image_previous = cv::Mat(snap_thumbnail(camera));
  43.     while(true){
  44.         cv::Mat image_current = cv::Mat(snap_thumbnail(camera));
  45.         uchar pixel_green_previous, pixel_green_current;
  46.         for(int y = 0; y < image_current.rows; y++){
  47.             cv::Vec3b* pixel_row_previous = image_previous.ptr<cv::Vec3b>(y);
  48.             cv::Vec3b* pixel_row_current = image_current.ptr<cv::Vec3b>(y);
  49.             for(int x = 0; x < image_current.cols; x++){
  50.                 pixel_green_previous = pixel_row_previous[x][1];
  51.                 pixel_green_current = pixel_row_current[x][1];
  52.                 std::string pixel_green_previous_string(reinterpret_cast<char*>(pixel_green_previous));
  53.                 std::string pixel_green_current_string(reinterpret_cast<char*>(pixel_green_current));
  54.                 debug("[", false);
  55.                 debug(pixel_green_previous_string, false);
  56.                 debug("] - [", false);
  57.                 debug(pixel_green_current_string, false);
  58.                 debug("]");
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. int main(int argc, char **argv){
  65.     if(argc != 2){
  66.        debug("Invalid arguments");
  67.         return 1;
  68.     }
  69.     std::string filename = argv[1];
  70.     debug("Filename: ", false);
  71.     debug(filename);
  72.  
  73.     raspicam::RaspiCam_Cv camera;
  74.    
  75.     camera.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  76.  
  77.     int resolution[] = {2592, 1944};
  78.  
  79.     debug("Camera properties set");
  80.  
  81.     bool initialized = camera.open();
  82.     if(!initialized){
  83.         debug("Couldn't initialize camera module");
  84.         return 1;
  85.     }else{
  86.         debug("Initialized camera");
  87.     }
  88.  
  89.     motion_sensing_loop(camera, 20, 20);
  90.  
  91.     /*debug("Taking picture");
  92.     cv::Mat image;
  93.     camera.grab();
  94.     camera.retrieve(image);
  95.  
  96.     cv::Size size(100, 75);
  97.  
  98.     cv::Mat resized;
  99.  
  100.     cv::resize(image, image, size, 0, 0, cv::INTER_CUBIC);
  101.     cv::imwrite("test.png", image);
  102.  
  103.     debug("Picture saved");*/
  104.  
  105.  
  106.     camera.release();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement