Advertisement
Guest User

detector.cpp

a guest
Oct 24th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.43 KB | None | 0 0
  1. /*
  2.  * detector.cpp
  3.  *
  4.  *  Created on: 19.10.2016
  5.  *      Author: pzima
  6.  */
  7.  
  8. //#define KOMPILACJA_NA_SERWERZE
  9.  
  10. #include "detector.h"
  11.  
  12. detector::detector(detector_config cfg)
  13. {
  14.     this->config = cfg;
  15.     thr = std::thread(processing, this);
  16. }
  17.  
  18. void detector::push_task(std::string task)
  19. {
  20.     task_mutex.lock();
  21.  
  22.     this->tasks.push(task);
  23.  
  24.     task_mutex.unlock();
  25. }
  26.  
  27. std::string detector::pop_task()
  28. {
  29.     std::string task;
  30.     task_mutex.lock();
  31.     if(!tasks.empty())
  32.     {
  33.         task = tasks.front();
  34.         tasks.pop();
  35.     }
  36.     else
  37.     {
  38.         task = std::string("null");
  39.     }
  40.  
  41.     task_mutex.unlock();
  42.     return task;
  43. }
  44.  
  45. void detector::processing(void *ptr)
  46. {
  47.  
  48.     detector* cPtr = (detector*)ptr;
  49.  
  50.     std::string task;
  51.     while(true)
  52.     {
  53.         task = cPtr->pop_task();
  54.         if(task != std::string("null"))
  55.         {
  56.             std::cout << "Task : " << task << std::endl;
  57.             std::string erf_filename(cPtr->config.echo_raw_dir + task + ".erf");
  58.             if(cPtr->wrapper.open(erf_filename, OPEN_READ))
  59.             {
  60.                 cPtr->HEIGHT = cPtr->wrapper.get_ping_size();
  61.                 cPtr->WIDTH  = std::min(cPtr->wrapper.get_c0Length(), cPtr->wrapper.get_c1Length());
  62.                 cPtr->RATE   = cPtr->wrapper.get_rate();
  63.  
  64.                 cPtr->c_0 = new int8_t *[cPtr->WIDTH]; cPtr->c_1 = new int8_t *[cPtr->WIDTH];
  65.                 for(int i = 0; i < cPtr->WIDTH; i++)
  66.                 {
  67.                     cPtr->c_0[i] = new int8_t[cPtr->HEIGHT];
  68.                     cPtr->c_1[i] = new int8_t[cPtr->HEIGHT];
  69.                 }
  70.  
  71.                 cPtr->wrapper.get_pings(cPtr->c_0, cPtr->c_1, 0, cPtr->WIDTH);
  72.  
  73.                 cPtr->cut_background(cPtr->config.cut_off_db_thresh);
  74.                 cPtr->scan(0);
  75.                 cPtr->scan(1);
  76.  
  77.                 std::cout << cPtr->objects_0.size() << std::endl;
  78.                 std::cout << cPtr->objects_1.size() << std::endl;
  79.  
  80.                 cPtr->compare_objects();
  81.                 cPtr->check_objects();
  82.                 std::string png_filename(cPtr->config.echo_png_dir + task + ".png");
  83.                 cPtr->get_png(png_filename);
  84.  
  85.                 if(cPtr->pairs.size() > 0)
  86.                 {
  87.                     int id = cPtr->prepare_database();
  88.  
  89.                     cPtr->prepare_echograms(png_filename, id);
  90.                     cPtr->prepare_videos(task, id);
  91.                 }
  92.  
  93.  
  94.                 cPtr->objects_0.clear();
  95.                 cPtr->objects_1.clear();
  96.                 cPtr->pairs.clear();
  97.                 cPtr->wrapper.close();
  98.                 for(int i = 0; i < cPtr->WIDTH; i++)
  99.                 {
  100.                     delete [] cPtr->c_0[i];
  101.                     delete [] cPtr->c_1[i];
  102.                 }
  103.                 delete [] cPtr->c_0;
  104.                 delete [] cPtr->c_1;
  105.             }
  106.         }
  107.         else
  108.         {
  109.             std::cout << "Brak zadan..." << std::endl;
  110.         }
  111.  
  112.         usleep(1000000);
  113.     }
  114. }
  115.  
  116. void detector::cut_background(int db_thresh)
  117. {
  118.     for(int y = config.y_0_offset; y < config.y_1_offset; y++)
  119.     {
  120.         float back_probe_0 = 0.0f;
  121.         float back_probe_1 = 0.0f;
  122.  
  123.         for(int x = 0; x < WIDTH; x++)
  124.         {
  125.             back_probe_0 += c_0[x][y];
  126.             back_probe_1 += c_1[x][y];
  127.         }
  128.  
  129.         back_probe_0 /= WIDTH;
  130.         back_probe_1 /= WIDTH;
  131.  
  132.         for(int x = 0; x < WIDTH; x++)
  133.         {
  134.             if(c_0[x][y] < back_probe_0 + db_thresh) c_0[x][y] = BACKGROUND;
  135.             if(c_1[x][y] < back_probe_1 + db_thresh) c_1[x][y] = BACKGROUND;
  136.         }
  137.     }
  138. }
  139.  
  140. void detector::scan(int ch)
  141. {
  142.     bool peaks[WIDTH][HEIGHT];
  143.  
  144.     for(int y = config.y_0_offset; y < config.y_1_offset; y++)
  145.     {
  146.         for(int x = 0; x < WIDTH; x++)
  147.         {
  148.             peaks[x][y] = false;
  149.         }
  150.     }
  151.  
  152.     if(ch == 0)
  153.     {
  154.         for(int y = config.y_0_offset; y < config.y_1_offset; y++)
  155.         {
  156.             for(int x = 0; x < WIDTH; x++)
  157.             {
  158.                 if(c_0[x][y] > BACKGROUND + config.scan_db_thresh && !peaks[x][y])
  159.                 {
  160.                     int dimensions_tab[4];
  161.                     dimensions_tab[0] = x; dimensions_tab[1] = x;
  162.                     dimensions_tab[2] = y; dimensions_tab[3] = y;
  163.  
  164.                     if(flood_fish(x, y, dimensions_tab, 0, BACKGROUND + config.scan_db_thresh))
  165.                     {
  166.                         for(int pX = dimensions_tab[0]; pX < dimensions_tab[1]; pX++)
  167.                         {
  168.                             for(int pY = dimensions_tab[2]; pY < dimensions_tab[3]; pY++)
  169.                             {
  170.                                 peaks[pX][pY] = true;
  171.                             }
  172.                         }
  173.                         object obj;
  174.                         obj.x_0 = dimensions_tab[0];
  175.                         obj.x_1 = dimensions_tab[1];
  176.                         obj.y_0 = dimensions_tab[2];
  177.                         obj.y_1 = dimensions_tab[3];
  178.                         objects_0.push_back(obj);
  179.                     }
  180.                 }
  181.             }
  182.         }
  183.     }
  184.     else if(ch == 1)
  185.     {
  186.         for(int y = config.y_0_offset; y < config.y_1_offset; y++)
  187.         {
  188.             for(int x = 0; x < WIDTH; x++)
  189.             {
  190.                 if(c_1[x][y] > BACKGROUND + config.scan_db_thresh && !peaks[x][y])
  191.                 {
  192.                     int dimensions_tab[4];
  193.                     dimensions_tab[0] = x; dimensions_tab[1] = x;
  194.                     dimensions_tab[2] = y; dimensions_tab[3] = y;
  195.  
  196.                     if(flood_fish(x, y, dimensions_tab, 1, BACKGROUND + config.scan_db_thresh))
  197.                     {
  198.                         for(int pX = dimensions_tab[0]; pX < dimensions_tab[1]; pX++)
  199.                         {
  200.                             for(int pY = dimensions_tab[2]; pY < dimensions_tab[3]; pY++)
  201.                             {
  202.                                 peaks[pX][pY] = true;
  203.                             }
  204.                         }
  205.                         object obj;
  206.                         obj.x_0 = dimensions_tab[0];
  207.                         obj.x_1 = dimensions_tab[1];
  208.                         obj.y_0 = dimensions_tab[2];
  209.                         obj.y_1 = dimensions_tab[3];
  210.                         objects_1.push_back(obj);
  211.                     }
  212.                 }
  213.             }
  214.         }
  215.     }
  216. }
  217.  
  218. bool detector::flood_fish(int x, int y, int *dimensions_tab, int ch, int db_thresh)
  219. {
  220.     if(ch == 0)
  221.     {
  222.         if(x < 1 || y < config.y_0_offset || x > (WIDTH - 1) || y > config.y_1_offset || c_0[x][y] < db_thresh || c_0[x][y] > -1) { return false; }
  223.         else
  224.         {
  225.             c_0[x][y] = c_0[x][y] * (-1);
  226.             if(x < dimensions_tab[0]) { dimensions_tab[0] = x; }
  227.             if(x > dimensions_tab[1]) { dimensions_tab[1] = x; }
  228.             if(y < dimensions_tab[2]) { dimensions_tab[2] = y; }
  229.             if(y > dimensions_tab[3]) { dimensions_tab[3] = y; }
  230.  
  231.             for(int _x = -10; _x <= 10; _x++)
  232.             {
  233.                 for(int _y = -5; _y <= 5; _y++)
  234.                 {
  235.                     if((x + _x) > 0 && (x + _x) < WIDTH && (y + _y) > config.y_0_offset && (y + _y) < config.y_1_offset)
  236.                     {
  237.                         flood_fish(x + _x, y + _y, dimensions_tab, ch, db_thresh);
  238.                     }
  239.                 }
  240.             }
  241.             return true;
  242.         }
  243.     }
  244.     else if(ch == 1)
  245.     {
  246.         if(x < 1 || y < config.y_0_offset || x > (WIDTH - 1) || y > config.y_1_offset || c_1[x][y] < db_thresh || c_1[x][y] > -1) { return false; }
  247.         else
  248.         {
  249.             c_1[x][y] = c_1[x][y] * (-1);
  250.             if(x < dimensions_tab[0]) { dimensions_tab[0] = x; }
  251.             if(x > dimensions_tab[1]) { dimensions_tab[1] = x; }
  252.             if(y < dimensions_tab[2]) { dimensions_tab[2] = y; }
  253.             if(y > dimensions_tab[3]) { dimensions_tab[3] = y; }
  254.  
  255.             for(int _x = -10; _x <= 10; _x++)
  256.             {
  257.                 for(int _y = -5; _y <= 5; _y++)
  258.                 {
  259.                     if((x + _x) > 0 && (x + _x) < WIDTH && (y + _y) > config.y_0_offset && (y + _y) < config.y_1_offset)
  260.                     {
  261.                         flood_fish(x + _x, y + _y, dimensions_tab, ch, db_thresh);
  262.                     }
  263.                 }
  264.             }
  265.             return true;
  266.         }
  267.     }
  268.     else return false;
  269. }
  270.  
  271. void detector::compare_objects()
  272. {
  273.     /* wyrzuc zbyt male  i dwa razy wyzsze niz dluzsze*/
  274.     objects_0.erase(std::remove_if(objects_0.begin(), objects_0.end(), [](object o){
  275.         if(o.x_1 - o.x_0 <= 3){ return true; }
  276.         if(o.y_1 - o.y_0 <= 2){ return true; }
  277.         //if(fabs(o.y_1 - o.y_0) > 2 * fabs(o.x_1 - o.x_0)){ return true; }
  278.         return false;
  279.     }), objects_0.end());
  280.     objects_1.erase(std::remove_if(objects_1.begin(), objects_1.end(), [](object o){
  281.         if(o.x_1 - o.x_0 <= 3){ return true; }
  282.         if(o.y_1 - o.y_0 <= 2){ return true; }
  283.         //if(fabs(o.y_1 - o.y_0) > 2 * fabs(o.x_1 - o.x_0)){ return true; }
  284.         return false;
  285.     }), objects_1.end());
  286.     /* wyrzuc obiekty niegeste < 15%*/
  287.     objects_0.erase(std::remove_if(objects_0.begin(), objects_0.end(), [=](object o){
  288.         int iter = 0;
  289.         for(int x = o.x_0; x < o.x_1; x++)
  290.         {
  291.             for(int y = o.y_0; y < o.y_1; y++)
  292.             {
  293.                 if(c_0[x][y] > 0) iter++;
  294.             }
  295.         }
  296.         int WH = (o.x_1 - o.x_0) * (o.y_1 - o.y_0);
  297.         if((float)(iter) / (float)(WH) < config.min_density) { return true; }
  298.         return false;
  299.     }), objects_0.end());
  300.     objects_1.erase(std::remove_if(objects_1.begin(), objects_1.end(), [=](object o){
  301.         int iter = 0;
  302.         for(int x = o.x_0; x < o.x_1; x++)
  303.         {
  304.             for(int y = o.y_0; y < o.y_1; y++)
  305.             {
  306.                 if(c_1[x][y] > 0) iter++;
  307.             }
  308.         }
  309.         int WH = (o.x_1 - o.x_0) * (o.y_1 - o.y_0);
  310.         if((float)(iter) / (float)(WH) < config.min_density) { return true; }
  311.         return false;
  312.     }), objects_1.end());
  313.  
  314.     /* zrob pary */
  315.     if(objects_0.size() < objects_1.size() || objects_0.size() == objects_0.size())
  316.     {
  317.         for(uint i = 0; i < objects_0.size(); i++)
  318.         {
  319.             float x_0 = (objects_0[i].x_0 + objects_0[i].x_1) / 2;
  320.             float y_0 = (objects_0[i].y_0 + objects_0[i].y_1) / 2;
  321.  
  322.             for(uint j = 0; j < objects_1.size(); j++)
  323.             {
  324.                 float x_1 = (objects_1[j].x_0 + objects_1[j].x_1) / 2;
  325.                 float y_1 = (objects_1[j].y_0 + objects_1[j].y_1) / 2;
  326.                 float distance = std::sqrt(std::pow(x_1 - x_0, 2) + std::pow(y_1 - y_0, 2));
  327.                 if(distance < config.max_dist)
  328.                 {
  329.                     p para;
  330.                     para.o_0 = objects_0[i];
  331.                     para.o_1 = objects_1[j];
  332.                     pairs.push_back(para);
  333.                 }
  334.             }
  335.         }
  336.     }
  337.     else
  338.     {
  339.         for(uint i = 0; i < objects_1.size(); i++)
  340.         {
  341.             float x_0 = (objects_1[i].x_0 + objects_1[i].x_1) / 2;
  342.             float y_0 = (objects_1[i].y_0 + objects_1[i].y_1) / 2;
  343.  
  344.             for(uint j = 0; j < objects_0.size(); j++)
  345.             {
  346.                 float x_1 = (objects_0[j].x_0 + objects_0[j].x_1) / 2;
  347.                 float y_1 = (objects_0[j].y_0 + objects_0[j].y_1) / 2;
  348.                 float distance = std::sqrt(std::pow(x_1 - x_0, 2) + std::pow(y_1 - y_0, 2));
  349.                 if(distance < config.max_dist)
  350.                 {
  351.                     p para;
  352.                     para.o_0 = objects_0[i];
  353.                     para.o_1 = objects_1[j];
  354.                     pairs.push_back(para);
  355.                 }
  356.             }
  357.         }
  358.     }
  359. }
  360.  
  361. void detector::check_objects()
  362. {
  363.     for(uint i = 0; i < pairs.size(); i++)
  364.     {
  365.         /* c_0 */
  366.         for(int x = pairs[i].o_0.x_0; x < pairs[i].o_0.x_1; x++)
  367.         {
  368.             c_0[x][pairs[i].o_0.y_0] = -5;
  369.             c_0[x][pairs[i].o_0.y_1] = -5;
  370.         }
  371.         for(int y = pairs[i].o_0.y_0; y < pairs[i].o_0.y_1; y++)
  372.         {
  373.             c_0[pairs[i].o_0.x_0][y] = -5;
  374.             c_0[pairs[i].o_0.x_1][y] = -5;
  375.         }
  376.         /* c_1 */
  377.         for(int x = pairs[i].o_1.x_0; x < pairs[i].o_1.x_1; x++)
  378.         {
  379.             c_1[x][pairs[i].o_1.y_0] = -5;
  380.             c_1[x][pairs[i].o_1.y_1] = -5;
  381.         }
  382.         for(int y = pairs[i].o_1.y_0; y < pairs[i].o_1.y_1; y++)
  383.         {
  384.             c_1[pairs[i].o_1.x_0][y] = -5;
  385.             c_1[pairs[i].o_1.x_1][y] = -5;
  386.         }
  387.     }
  388. }
  389.  
  390. void detector::get_png(std::string filename)
  391. {
  392.     const int R_PAL[120] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,38,56,79,100,127,151,181,208,226,233,240,248,255,255,255,255,255};
  393.     const int G_PAL[120] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,6,12,19,28,37,50,62,78,87,95,102,109,117,124,131,138,146,153,160,168,175,182,189,197,204,211,219,212,197,175,157,132,108,78,54,24};
  394.     const int B_PAL[120] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,15,22,29,36,44,51,58,66,73,80,81,80,74,66,60,49,40,26,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  395.  
  396.     cv::Mat echogram(HEIGHT * 2, WIDTH, CV_8UC3, cv::Scalar(0,0,0));
  397.  
  398.     for(int y = 0; y < HEIGHT; y++)
  399.     {
  400.         for(int x = 0; x < WIDTH; x++)
  401.         {
  402.             int pwr;
  403.             if(c_0[x][y] < 0){ pwr = 120 - (c_0[x][y] * (-1)); }
  404.             else { pwr = 120 - (c_0[x][y]); }
  405.             if(pwr < 0) pwr = 0;
  406.             if(pwr > 120) pwr = 119;
  407.  
  408.             echogram.at<cv::Vec3b>(y,x)[0] = B_PAL[pwr];
  409.             echogram.at<cv::Vec3b>(y,x)[1] = G_PAL[pwr];
  410.             echogram.at<cv::Vec3b>(y,x)[2] = R_PAL[pwr];
  411.         }
  412.     }
  413.     for(int y = 0; y < HEIGHT; y++)
  414.     {
  415.         for(int x = 0; x < WIDTH; x++)
  416.         {
  417.             int pwr;
  418.             if(c_1[x][y] < 0){ pwr = 120 - (c_1[x][y] * (-1)); }
  419.             else { pwr = 120 - (c_1[x][y]); }
  420.             if(pwr < 0) pwr = 0;
  421.             if(pwr > 120) pwr = 119;
  422.  
  423.             echogram.at<cv::Vec3b>(y + HEIGHT,x)[0] = B_PAL[pwr];
  424.             echogram.at<cv::Vec3b>(y + HEIGHT,x)[1] = G_PAL[pwr];
  425.             echogram.at<cv::Vec3b>(y + HEIGHT,x)[2] = R_PAL[pwr];
  426.         }
  427.     }
  428.     cv::imwrite(filename, echogram);
  429. }
  430.  
  431. char* detector::insert_record(std::string datetime, float width, float height, float speed, int direction)
  432. {
  433.     char systemCall[255];
  434.     sprintf(systemCall, "./addons/insert2 1 1 %f %f %i %f %s 2>&1", height, width, direction, speed, datetime.c_str());
  435.  
  436.     std::cout << datetime.c_str() << " dodaje rybke. H = " << height << " W = " << width << " S = " << speed << std::endl;
  437.  
  438.     char *output = new char[255];
  439.     FILE * stream;
  440.     const int max_buffer = 256;
  441.     char buffer[max_buffer];
  442.  
  443.     stream = popen(systemCall, "r");
  444.     if(stream)
  445.     {
  446.         while(!feof(stream))
  447.         {
  448.             if(fgets(buffer, max_buffer, stream) != NULL)
  449.             {
  450.                 sprintf(output, "%s", buffer);
  451.             }
  452.         }
  453.         pclose(stream);
  454.     }
  455.     return output;
  456. }
  457.  
  458. std::string detector::get_datetime(long timestamp)
  459. {
  460.     std::stringstream date_str;
  461.     boost::posix_time::ptime pt_1 = boost::posix_time::from_time_t(timestamp);
  462.     boost::gregorian::date d = pt_1.date();
  463.     auto td = pt_1.time_of_day();
  464.     date_str << d.year() << "-" << std::setw(2) << std::setfill('0') << d.month().as_number() << "-" << std::setw(2) << std::setfill('0') << d.day() << " " << std::setw(2) << std::setfill('0') << td.hours() << ":" << std::setw(2) << std::setfill('0') << td.minutes() << ":"  << std::setw(2) << std::setfill('0') << td.seconds();
  465.  
  466.     return date_str.str();
  467. }
  468.  
  469. int detector::prepare_database()
  470. {
  471.     int firstId = 0;
  472.     for(uint i = 0; i < pairs.size(); i++)
  473.     {
  474.         /* string date, float objWidth, float objHeight, float objSpeed, float objDirection */
  475.         int center_0    = (int)((pairs[i].o_0.x_0 + pairs[i].o_0.x_1) / 2);
  476.         int center_1    = (int)((pairs[i].o_1.x_0 + pairs[i].o_1.x_1) / 2);
  477.         int width_0     = pairs[i].o_0.x_1 - pairs[i].o_0.x_0;
  478.         int width_1     = pairs[i].o_1.x_1 - pairs[i].o_1.x_0;
  479.         int direction   = 0;
  480.  
  481.         float time      = fabs((center_1 - center_0) / RATE);
  482.         float speed     = 0.1 / time;
  483.         float width     = (((width_0 + width_1) / 2) / RATE) * speed;
  484.         float height    = width / 3;
  485.  
  486.         if(center_0 > center_1){ direction = 1; }
  487.  
  488.         /* licz date */
  489.         time_t t    = std::time(NULL);
  490.         int center  = (int)((center_0 + center_1) / 2);
  491.         int deltaT  = (int)((WIDTH - center) / 100);
  492.         t           = t - deltaT + 7200;
  493.         std::string datetime = get_datetime(t);
  494.  
  495.         if(i == 0)
  496.         {
  497.             firstId = atoi(insert_record(datetime, width, height, speed, direction));
  498.         }
  499.         else
  500.         {
  501.             insert_record(datetime, width, height, speed, direction);
  502.         }
  503.     }
  504.     return firstId;
  505. }
  506.  
  507. void detector::prepare_echograms(std::string filename, int id)
  508. {
  509.     cv::Mat image;
  510.     image = cv::imread(filename, CV_LOAD_IMAGE_COLOR);
  511.  
  512.     for(uint i = 0; i < pairs.size(); i++)
  513.     {
  514.         cv::Rect roi_0;
  515.         cv::Rect roi_1;
  516.         if(pairs[i].o_0.x_0 < 100)
  517.         {
  518.             roi_0.x       = 0;
  519.             roi_0.y       = 0;
  520.             roi_0.width   = 200;
  521.             roi_0.height  = HEIGHT;
  522.  
  523.             roi_1.x       = 0;
  524.             roi_1.y       = HEIGHT;
  525.             roi_1.width   = 200;
  526.             roi_1.height  = HEIGHT;
  527.         }
  528.         else if(pairs[i].o_0.x_0 > WIDTH - 100)
  529.         {
  530.             roi_0.x       = WIDTH - 200;
  531.             roi_0.y       = 0;
  532.             roi_0.width   = 200;
  533.             roi_0.height  = HEIGHT;
  534.  
  535.             roi_1.x       = WIDTH - 200;
  536.             roi_1.y       = HEIGHT;
  537.             roi_1.width   = 200;
  538.             roi_1.height  = HEIGHT;
  539.         }
  540.         else
  541.         {
  542.             roi_0.x       = pairs[i].o_0.x_0 - 100;
  543.             roi_0.y       = 0;
  544.             roi_0.width   = 200;
  545.             roi_0.height  = HEIGHT;
  546.  
  547.             roi_1.x       = pairs[i].o_0.x_0 - 100;
  548.             roi_1.y       = HEIGHT;
  549.             roi_1.width   = 200;
  550.             roi_1.height  = HEIGHT;
  551.         }
  552.         cv::Mat echo_0 = image(roi_0);
  553.         cv::Mat echo_1 = image(roi_1);
  554.  
  555.         cv::imwrite(std::string(config.fish_echo_dir + std::to_string(i + id) + "_0.png"), echo_0);
  556.         cv::imwrite(std::string(config.fish_echo_dir + std::to_string(i + id) + "_1.png"), echo_1);
  557.     }
  558. }
  559.  
  560. void detector::prepare_videos(std::string filename, int id)
  561. {
  562.     std::vector<std::string> videos;
  563.     for(int i = 0; i < config.x_cams; i++)
  564.     {
  565.         videos.push_back(std::string(config.video_dir + filename + "_" + std::to_string(i) + ".mp4"));
  566.     }
  567.     int V_LEN = (int)(WIDTH / 4);
  568.  
  569.     if(pairs.size() > 0)
  570.     {
  571.         bool **FRAMES;
  572.         FRAMES = new bool *[pairs.size()];
  573.         for(uint i = 0; i < pairs.size(); i++)
  574.         {
  575.             FRAMES[i] = new bool[V_LEN];
  576.         }
  577.  
  578.         for(uint i = 0; i < pairs.size(); i++)
  579.         {
  580.             for(int j = 0; j < V_LEN; j++)
  581.             {
  582.                 FRAMES[i][j] = false;
  583.             }
  584.         }
  585.  
  586.         for(uint i = 0; i < pairs.size(); i++)
  587.         {
  588.             int startX = 0;
  589.             int stopX = 0;
  590.             if(pairs[i].o_0.x_0 < 1000)
  591.             {
  592.                 startX  = 0;
  593.                 stopX   = 500;
  594.             }
  595.             else if(pairs[i].o_0.x_0 > WIDTH - 1000)
  596.             {
  597.                 startX  = (int)((WIDTH - 2000) / 4);
  598.                 stopX   = (int)(WIDTH / 4);
  599.             }
  600.             else
  601.             {
  602.                 startX = (int)((pairs[i].o_0.x_0 - 1000) / 4);
  603.                 stopX = (int)((pairs[i].o_0.x_0 + 1000) / 4);
  604.             }
  605.             //for(int j = startX; j < startX + 500; j++)
  606.             for(int j = startX; j < stopX; j++)
  607.             {
  608.                 FRAMES[i][j] = true;
  609.             }
  610.         }
  611.         std::thread thr[config.x_cams];
  612.         for(int CAM = 0; CAM < config.x_cams; CAM++)
  613.         {
  614.             thr[CAM] = std::thread(prepare_videos_thr, pairs.size(), id, videos[CAM], CAM, FRAMES, config, pairs);
  615.         }
  616.         for(int CAM = 0; CAM < config.x_cams; CAM++)
  617.         {
  618.             thr[CAM].join();
  619.         }
  620.  
  621.         for(uint i = 0; i < pairs.size(); i++)
  622.         {
  623.             delete [] FRAMES[i];
  624.         }
  625.         delete [] FRAMES;
  626.     }
  627. }
  628.  
  629. void detector::prepare_videos_thr(uint count_obj, int id, std::string video_name, int thr_id, bool **FRAMES, detector_config cfg, std::vector<p> pairs)
  630. {
  631.     cv::VideoWriter *writer[count_obj];
  632.     for(uint i = 0; i < pairs.size(); i++)
  633.     {
  634.         writer[i] = new cv::VideoWriter(std::string(cfg.fish_video_dir + std::to_string(i + id) + "_" + std::to_string(thr_id) + ".avi"),
  635.                                         CV_FOURCC('M','P','4','2'), 25, cvSize(704,576), 1);
  636.     }
  637.     int iter = 0;
  638.  
  639.     cv::VideoCapture cap_0(video_name);
  640.     cv::Mat frame, resized_frame, mog_frame;
  641. #ifdef KOMPILACJA_NA_SERWERZE
  642.     cv::Ptr<cv::BackgroundSubtractor> mog = cv::createBackgroundSubtractorMOG2(50, 16, false);
  643. #endif
  644.     bool frame_empty = false;
  645.     while(!frame_empty)
  646.     {
  647.         iter++;
  648.         cap_0.read(frame);
  649.         for(uint i = 0; i < pairs.size(); i++)
  650.         {
  651.             if(FRAMES[i][iter])
  652.             {
  653.                 writer[i]->write(frame);
  654.                 /* gif */
  655. #ifdef KOMPILACJA_NA_SERWERZE
  656.                 cv::resize(frame, resized_frame, cv::Size(cfg.gif_width, cfg.gif_height));
  657.                 mog->apply(resized_frame, mog_frame);
  658.                
  659.                 if(cv::countNonZero(mog_frame) > cfg.gif_threshold)
  660.                 {
  661.                     cv::imwrite(resized_frame, std::string(cfg.gif_temp_frames_dir + std::to_string(i + id) + "/" + ));
  662.                 }
  663. #endif
  664.             }
  665.         }
  666.         if(frame.empty()) { frame_empty = true; }
  667.     }
  668. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement