cameras_opencv_demo.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/videoio.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/imgproc.hpp> // cv::Canny()
  5. #include <iostream>
  6. #include <csignal>
  7. #include <fstream>
  8. #include <zconf.h>
  9. #include <string>
  10. #include <chrono>
  11. #include "MvGmslCamera.h"
  12. using namespace cv;
  13. using std::string;
  14. using std::cout;
  15. using std::cerr;
  16. using std::endl;
  17. struct sample_context {
  18. string cam_devname;
  19. string pix_fmt;
  20. unsigned int cam_w{1280};
  21. unsigned int cam_h{720};
  22. unsigned char camera_no;
  23. struct sync_out_a_cfg_client_t stCameraCfgSend{};
  24. };
  25. static void
  26. print_usage(void) {
  27. printf("\n\tUsage: example [OPTIONS]\n\n"
  28. "\tExample: \n"
  29. "\t./cameras_opencv_demo -d /dev/video0 -s 1280x720\n\n"
  30. "\tSupported options:\n"
  31. "\t-d\t\tSet V4l2 video device node\n"
  32. "\t-s\t\tSet output resolution of video device\n"
  33. "\t-n\t\tSet sync and async camera no. for example: [-n 2-4] the forward one is sync cameras no 2,the after one is async cameras no 4 (8 sync cameras is setted by default.like [-n 8-0])\n"
  34. "\t-r\t\tSet sync and async camera freq for example: [-r 30-20] the forward one is sync cameras freq 30,the after one is async cameras freq 20(sync freq 30 is setted by default.like [-r 30-0]) \n"
  35. "\t-b\t\tSet which cameras you want to trigger.example: [-b 0x0f-0xf0] the forward one is sync cameras which you want trigger,the after one is async cameras which you want trigger(all 8 cameras is setted sync model by default.like[-b 0xff-0])\n"
  36. "\t-p\t\tSet async cameras is triggered at which angle in a circle,not set by default.\n"
  37. "\t-h\t\tPrint this usage\n\n"
  38. "\tNOTE: It runs infinitely until you terminate it with <ctrl+c>\n");
  39. }
  40. static bool
  41. parse_cmdline(struct sample_context *ctx, int argc, char **argv) {
  42. int c;
  43. int sync_camera_bit_draw = 0, async_camera_bit_draw = 0;
  44. if (argc < 2) {
  45. print_usage();
  46. exit(EXIT_SUCCESS);
  47. }
  48. while ((c = getopt(argc, argv, "d:s:r:n:b:f:p:h")) != -1) {
  49. switch (c) {
  50. case 'd':
  51. ctx->cam_devname = optarg;
  52. sscanf(ctx->cam_devname.c_str(), "/dev/video%hhu", &ctx->camera_no);
  53. break;
  54. case 's':
  55. if (sscanf(optarg, "%ux%u", &ctx->cam_w, &ctx->cam_h) != 2) {
  56. print_usage();
  57. return false;
  58. }
  59. break;
  60. case 'r':
  61. if (sscanf(optarg, "%hhu-%hhu", &ctx->stCameraCfgSend.sync_freq, &ctx->stCameraCfgSend.async_freq) !=
  62. 2) {
  63. print_usage();
  64. return false;
  65. }
  66. printf("sync_freq : %d async_freq:%d\n", ctx->stCameraCfgSend.sync_freq,
  67. ctx->stCameraCfgSend.async_freq);
  68. break;
  69. case 'n':
  70. if (sscanf(optarg, "%hhu-%hhu", &ctx->stCameraCfgSend.sync_camera_num,
  71. &ctx->stCameraCfgSend.async_camera_num) != 2) {
  72. print_usage();
  73. return false;
  74. }
  75. printf("sync_camera_num : %d async_camera_num:%d\n", (ctx->stCameraCfgSend.sync_camera_num),
  76. (ctx->stCameraCfgSend.async_camera_num));
  77. break;
  78. case 'b':
  79. if (sscanf(optarg, "%x-%x", &sync_camera_bit_draw, &async_camera_bit_draw) != 2) {
  80. print_usage();
  81. return false;
  82. }
  83. ctx->stCameraCfgSend.sync_camera_bit_draw = (unsigned char) sync_camera_bit_draw;
  84. ctx->stCameraCfgSend.async_camera_bit_draw = (unsigned char) async_camera_bit_draw;
  85. printf("sync_camera_bit_draw : %d async_camera_bit_draw:%d\n",
  86. ctx->stCameraCfgSend.sync_camera_bit_draw, ctx->stCameraCfgSend.async_camera_bit_draw);
  87. break;
  88. case 'p':
  89. if (sscanf(optarg, "%hhu-%hhu-%hhu-%hhu-%hhu-%hhu-%hhu-%hhu",
  90. &ctx->stCameraCfgSend.async_camera_pos[0], &ctx->stCameraCfgSend.async_camera_pos[1],
  91. &ctx->stCameraCfgSend.async_camera_pos[2],
  92. &ctx->stCameraCfgSend.async_camera_pos[3], &ctx->stCameraCfgSend.async_camera_pos[4],
  93. &ctx->stCameraCfgSend.async_camera_pos[5], &ctx->stCameraCfgSend.async_camera_pos[6],
  94. &ctx->stCameraCfgSend.async_camera_pos[7]) != 8) {
  95. print_usage();
  96. return false;
  97. }
  98. printf("pos:[0]:%hhu [1]:%hhu [2]:%hhu [3]:%hhu [4]:%hhu [5]:%hhu [6]:%hhu [7]:%hhu \n",
  99. ctx->stCameraCfgSend.async_camera_pos[0], ctx->stCameraCfgSend.async_camera_pos[1],
  100. ctx->stCameraCfgSend.async_camera_pos[2],
  101. ctx->stCameraCfgSend.async_camera_pos[3], ctx->stCameraCfgSend.async_camera_pos[4],
  102. ctx->stCameraCfgSend.async_camera_pos[5], ctx->stCameraCfgSend.async_camera_pos[6],
  103. ctx->stCameraCfgSend.async_camera_pos[7]);
  104. break;
  105. case 'f':
  106. ctx->pix_fmt = optarg;
  107. break;
  108. case 'h':
  109. print_usage();
  110. exit(EXIT_SUCCESS);
  111. break;
  112. default:
  113. print_usage();
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. int main(int argc, char *argv[]) {
  120. struct sample_context ctx = {};
  121. ctx.stCameraCfgSend.async_camera_num = 0;
  122. ctx.stCameraCfgSend.async_freq = 0;
  123. ctx.stCameraCfgSend.async_camera_bit_draw = 0;
  124. ctx.stCameraCfgSend.sync_camera_num = 8;
  125. ctx.stCameraCfgSend.sync_freq = 30;
  126. ctx.stCameraCfgSend.sync_camera_bit_draw = 0xff;
  127. ctx.cam_devname = "/dev/video0";
  128. ctx.cam_w = 1280;
  129. ctx.cam_h = 720;
  130. ctx.pix_fmt = "UYVY";
  131. if (!parse_cmdline(&ctx, argc, argv)) {
  132. return -1;
  133. }
  134. char Gstring[256];
  135. sprintf(Gstring, "v4l2src device=%s ! video/x-raw, width=%d, height=%d, format=UYVY ! queue ! appsink",
  136. ctx.cam_devname.c_str(), ctx.cam_w, ctx.cam_h);
  137. miivii::MvGmslCamera mvcam(ctx.stCameraCfgSend);
  138. cout << "Opening camera..." << endl;
  139. VideoCapture capture(Gstring, CAP_GSTREAMER);
  140. if (!capture.isOpened()) {
  141. cerr << "ERROR: Can't initialize camera capture" << endl;
  142. return 1;
  143. }
  144. Mat frame;
  145. Mat frameRaw;
  146. bool stop = false;
  147. uint64_t timestamp = 0;
  148. unsigned int colorConvert;
  149. if (ctx.pix_fmt == "YUYV") {
  150. colorConvert = COLOR_YUV2BGR_YUYV;
  151. } else {
  152. colorConvert = COLOR_YUV2BGR_UYVY;
  153. }
  154. while (!stop) {
  155. capture.read(frameRaw);
  156. /*必须要先获取到图像再去获取时间戳*/
  157. mvcam.GetGmslTimeStamp(ctx.camera_no, timestamp);
  158. cvtColor(frameRaw, frame, colorConvert);
  159. namedWindow("Video" + std::to_string(ctx.camera_no), 0);
  160. resizeWindow("Video" + std::to_string(ctx.camera_no), ctx.cam_w, ctx.cam_h);
  161. imshow("Video" + std::to_string(ctx.camera_no), frame);
  162. std::cout << "Timestamp from MIIVII : " << timestamp << std::endl;
  163. if (waitKey(1) == 27) {
  164. stop = true;
  165. }
  166. }
  167. }