No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

camera.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2018 dimercur
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "camera.h"
  18. #include "img.h"
  19. #include <unistd.h>
  20. using namespace cv;
  21. /**
  22. * Create an object for accessing camera
  23. * @param size Size of picture to grab (@see captureSize)
  24. * @param fps speed of sampling
  25. */
  26. Camera::Camera(int size, int fps) {
  27. this->SetSize(size);
  28. #ifndef __FOR_PC__
  29. this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  30. this->cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
  31. this->cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
  32. this->cap.set(CV_CAP_PROP_FPS, fps);
  33. #endif /* __FOR_PC__ */
  34. }
  35. /**
  36. * Open camera
  37. * @return True if camera is open, false otherwise
  38. */
  39. bool Camera::Open() {
  40. bool status = false;
  41. #ifdef __FOR_PC__
  42. if (this->cap.open(0)) {
  43. //this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  44. this->cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
  45. this->cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
  46. status = true;
  47. }
  48. #else
  49. if (this->cap.open()) {
  50. cout << "Camera warmup 2sec" << endl << flush;
  51. sleep(2);
  52. cout << "Start capture" << endl << flush;
  53. status = true;
  54. }
  55. #endif /* __FOR_PC__ */
  56. return status;
  57. }
  58. /**
  59. * Close and release camera
  60. */
  61. void Camera::Close() {
  62. this->cap.release();
  63. }
  64. /**
  65. * Define size for sampled picture
  66. * @param size Size of picture (@see captureSize)
  67. */
  68. void Camera::SetSize(int size) {
  69. this->size = size;
  70. switch (size) {
  71. case xs:
  72. this->width = 480;
  73. this->height = 360;
  74. break;
  75. case sm:
  76. this->width = 640;
  77. this->height = 480;
  78. break;
  79. case md:
  80. this->width = 1024;
  81. this->height = 768;
  82. break;
  83. case lg:
  84. this->width = 1280;
  85. this->height = 960;
  86. break;
  87. default:
  88. this->width = 480;
  89. this->height = 360;
  90. }
  91. }
  92. /**
  93. * Grab next image from camera
  94. * @return Image taken from camera
  95. */
  96. Img Camera::Grab() {
  97. ImageMat frame;
  98. #ifdef __FOR_PC__
  99. cap >> frame;
  100. Img capture = Img(frame);
  101. #else
  102. cap.grab();
  103. cap.retrieve(frame);
  104. #ifdef __INVERSE_COLOR__
  105. cvtColor(frame, frame, CV_BGR2RGB);
  106. #endif // __INVERSE_COLOR__
  107. Img capture = Img(frame);
  108. #endif /* __FOR_PC__ */
  109. return capture;
  110. }
  111. /**
  112. * Get opening status for camera
  113. * @return true if camera is open, false otherwise
  114. */
  115. bool Camera::IsOpen() {
  116. return cap.isOpened();
  117. }
  118. /**
  119. * Get width of sampled image
  120. * @return Width of sampled picture
  121. */
  122. int Camera::GetWidth() const {
  123. return width;
  124. }
  125. /**
  126. * Get height of sampled image
  127. * @return height of sampled picture
  128. */
  129. int Camera::GetHeight() const {
  130. return height;
  131. }