Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp> //openCV - библиотеки для работы с переферийными устройствами (веб-камера)
  3. #include <iostream>
  4.  
  5. using namespace cv;
  6. using namespace std;
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. struct timespec start, end, temp1, temp2,temp3,temp4,temp5,temp6;//Структуры для сохранения определенного времени
  11.  
  12. clock_gettime(CLOCK_MONOTONIC_RAW, &start); //засекаем время работы всей программы
  13.  
  14. VideoCapture cap(0); //'открываем поток' с камерой по умолчанию - веб-камера
  15.  
  16.  
  17. if (!cap.isOpened())
  18. {
  19.  
  20. cout << «Error. Can not open the camera" << endl;
  21. cin.get(); // функция для вывода данных с потока, аналог get,но нужна,чтобы консоль не закрывалаась сразу после завершения работы программы
  22. return -1;
  23. }
  24.  
  25. string window_name = «My window»; //создаем название окна
  26. namedWindow(window_name); //Создаем окно с этим названием
  27.  
  28. double time_rendering = 0;//обнуляем время
  29. double time_working = 0;//время работы программы от start до end
  30. unsigned long count = 0;//обнуляем число кадров
  31. double time_input=0;//для чтения кадров
  32. double time_output=0;//для вывода кадров
  33.  
  34.  
  35. while (true) //бесконечный цикл
  36. {
  37.  
  38.  
  39. Mat frame; // картинка-матрица множество пикселей двумерный массив цвет-каждый зеленый синий 1 пиксель-цвет r=0 до 255=g=b чем больше число,тем выше насыщенность
  40. clock_gettime(CLOCK_MONOTONIC_RAW, &temp3);
  41. if (!cap.read(frame))//считывание кадра, если мы не можем получить кадр-ошибка
  42. {
  43. cout << «Error. Video camera is disconnected" << endl;
  44. cin.get();
  45. break;
  46. }
  47. clock_gettime(CLOCK_MONOTONIC_RAW, &temp4);
  48. time_input += (temp4.tv_sec - temp3.tv_sec + 0.000000001 * (temp4.tv_nsec - temp3.tv_nsec)); clock_gettime(CLOCK_MONOTONIC_RAW, &temp1);
  49.  
  50.  
  51. for (int i = 0; i < frame.rows; i++)//длина изображения - высота штука для изменения цвета 0-red,1-green,2-blue
  52. for (int j = 0; j < frame.cols; j++)//ширина изображения
  53. if (frame.at<Vec3b>(i, j)[0] < frame.at<Vec3b>(i, j)[1] - 10 && //обращаемся к ij пикселю и сравниваем его с red
  54. frame.at<Vec3b>(i, j)[2] < frame.at<Vec3b>(i, j)[1] - 10 &&//бращаемся к ij пикселю и сравниваем его с blue
  55. frame.at<Vec3b>(i, j)[1] > 64)// green>64
  56. {
  57. frame.at<Vec3b>(i, j)[0] = 242;// blue
  58. frame.at<Vec3b>(i, j)[1] = 78;//green
  59. frame.at<Vec3b>(i, j)[2] = 100;//устанавливаем значения цвета, цвет состоит из 3 цветов, порядок цветов меняется }
  60. clock_gettime(CLOCK_MONOTONIC_RAW, &temp2);
  61. time_rendering += (temp2.tv_sec - temp1.tv_sec + 0.000000001 * (temp2.tv_nsec - temp1.tv_nsec));//время на обработку изображения
  62. clock_gettime(CLOCK_MONOTONIC_RAW, &temp5);
  63. imshow(window_name, frame); //показываем изображение в ранее созданное окно
  64. clock_gettime(CLOCK_MONOTONIC_RAW, &temp6);
  65. time_output += (temp6.tv_sec - temp5.tv_sec + 0.000000001 * (temp6.tv_nsec - temp5.tv_nsec));//время на обработку изображения
  66.  
  67. count++; //количество кадров увеличили
  68.  
  69.  
  70. if (waitKey(10) == 27)//ждем нажатия кнопки '27'-escape по таблице аски выход из цикла,конец считывания
  71. {
  72. cout << «Stop" << endl;
  73. break;
  74. }
  75. }
  76.  
  77. clock_gettime(CLOCK_MONOTONIC_RAW, &end);
  78. time_working = (end.tv_sec - start.tv_sec + 0.000000001 * (end.tv_nsec - start.tv_nsec));
  79.  
  80. cout << "fps: " << (count / time_working) << endl; //количесво кадров в секунду = кол-во кадров / время работы программы от start до end
  81. cout << "rendering time: " << (time_rendering / time_working * 100) << "%" << endl;// время на обработку изображения = time_rendering
  82. cout << "time_input: " << (time_input / time_working*100 )<< "%"<<endl; //
  83. cout << "time_output: " <<(time_output/time_working*100)<<"%"<<endl;//
  84.  
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement