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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 with default values (size = sm and
  23. * fps = 10)
  24. */
  25. Camera::Camera():Camera(sm, 10){
  26. }
  27. /**
  28. * Create an object for accessing camera
  29. * @param size Size of picture to grab (@see captureSize)
  30. * @param fps speed of sampling
  31. */
  32. Camera::Camera(int size, int fps) {
  33. this->SetSize(size);
  34. #ifndef __FOR_PC__
  35. this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  36. this->cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
  37. this->cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
  38. this->cap.set(CV_CAP_PROP_FPS, fps);
  39. #endif /* __FOR_PC__ */
  40. }
  41. /**
  42. * Open camera
  43. * @return True if camera is open, false otherwise
  44. */
  45. bool Camera::Open() {
  46. bool status = false;
  47. #ifdef __FOR_PC__
  48. if (this->cap.open(0)) {
  49. //this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  50. this->cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
  51. this->cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
  52. status = true;
  53. }
  54. #else
  55. if (this->cap.open()) {
  56. cout << "Camera warmup 2sec" << endl << flush;
  57. sleep(2);
  58. cout << "Start capture" << endl << flush;
  59. status = true;
  60. }
  61. #endif /* __FOR_PC__ */
  62. return status;
  63. }
  64. /**
  65. * Close and release camera
  66. */
  67. void Camera::Close() {
  68. this->cap.release();
  69. }
  70. /**
  71. * Define size for sampled picture
  72. * @param size Size of picture (@see captureSize)
  73. */
  74. void Camera::SetSize(int size) {
  75. this->size = size;
  76. switch (size) {
  77. case xs:
  78. this->width = 480;
  79. this->height = 360;
  80. break;
  81. case sm:
  82. this->width = 640;
  83. this->height = 480;
  84. break;
  85. case md:
  86. this->width = 1024;
  87. this->height = 768;
  88. break;
  89. case lg:
  90. this->width = 1280;
  91. this->height = 960;
  92. break;
  93. default:
  94. this->width = 480;
  95. this->height = 360;
  96. }
  97. }
  98. /**
  99. * Grab next image from camera
  100. * @return Image taken from camera
  101. */
  102. Img Camera::Grab() {
  103. ImageMat frame;
  104. #ifdef __FOR_PC__
  105. cap >> frame;
  106. Img capture = Img(frame);
  107. #else
  108. cap.grab();
  109. cap.retrieve(frame);
  110. #ifdef __INVERSE_COLOR__
  111. cvtColor(frame, frame, CV_BGR2RGB);
  112. #endif // __INVERSE_COLOR__
  113. Img capture = Img(frame);
  114. #endif /* __FOR_PC__ */
  115. return capture;
  116. }
  117. /**
  118. * Get opening status for camera
  119. * @return true if camera is open, false otherwise
  120. */
  121. bool Camera::IsOpen() {
  122. return cap.isOpened();
  123. }
  124. /**
  125. * Get width of sampled image
  126. * @return Width of sampled picture
  127. */
  128. int Camera::GetWidth() const {
  129. return width;
  130. }
  131. /**
  132. * Get height of sampled image
  133. * @return height of sampled picture
  134. */
  135. int Camera::GetHeight() const {
  136. return height;
  137. }