appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::CameraCv Class Reference

OpenCv implementation of the camera class. The video is streamed via OpenCv calls. More...

#include <CameraCv.h>

Inheritance diagram for rnr::CameraCv:
rnr::Camera

Public Member Functions

 CameraCv (const std::string &strVideoDevName="/dev/video0", const CamRes &resVideo=CamResQVGA, const CamRes &resImage=CamResVGA)
 
virtual ~CameraCv ()
 Destructor.
 
virtual int startVideo (const CamRes &resVideo=CamResDft)
 Start the camera streaming video. More...
 
virtual int stopVideo ()
 Stop the camera from streaming video. More...
 
virtual int grabFrame (cv::Mat &frame)
 Grab a image frame from the video stream. More...
 
virtual int clickImage (cv::Mat &img, const CamRes &resImage=CamResDft)
 Take a still image. More...
 
virtual void autoFocus ()
 Auto-focus camera.
 
cv::VideoCapture & getCaptureObj ()
 Get OpenCV captured object. More...
 
- Public Member Functions inherited from rnr::Camera
 Camera (const std::string &strVideoDevName=VideoDevDft, const CamRes &resVideo=CamResQVGA, const CamRes &resImage=CamResVGA)
 
virtual ~Camera ()
 Destructor. More...
 
bool isCameraRunning () const
 Test if the camera is on and running. More...
 
bool isTakingAnImage () const
 Test if a still image is currently being taken. More...
 
bool isFatal () const
 Test if camera object is in a fatal condition. More...
 
CamRes getVideoResolution () const
 Get the current video resolution. More...
 
CamRes getImageResolution () const
 Get the current still image resolution. More...
 

Protected Member Functions

virtual CamRes setCameraResolution (const CamRes &res)
 Set the camera resolution in either video or still image mode. More...
 

Protected Attributes

cv::VideoCapture m_capture
 video capture object
 
- Protected Attributes inherited from rnr::Camera
std::string m_strVideoDevName
 video device name
 
int m_nVideoIndex
 video index
 
CamRes m_resCurrent
 current camera resolution
 
CamRes m_resVideo
 current video resolution
 
CamRes m_resImage
 current still image resolution
 
bool m_bCameraRunning
 camera is [not] on and running video
 
bool m_bTakingImage
 taking an image is [not] finished
 
bool m_bFatal
 camera instance is in a fatal state
 

Additional Inherited Members

- Static Public Member Functions inherited from rnr::Camera
static bool isEqResolution (const CamRes &res1, const CamRes &res2)
 Check is two camera resolutions are equal. More...
 
static int getVideoIndex (const std::string &strVideoDevName)
 Get the video index associated with the device. More...
 

Detailed Description

OpenCv implementation of the camera class. The video is streamed via OpenCv calls.

Definition at line 83 of file CameraCv.h.

Member Function Documentation

int CameraCv::clickImage ( cv::Mat &  img,
const CamRes resImage = CamResDft 
)
virtual

Take a still image.

Parameters
[in,out]imgSnap shot image taken.
resImageStill image resolution. If equal to CamResDft, then the initial/last image resolution setting is used.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 188 of file CameraCv.cxx.

References rnr::CamResDft, rnr::Camera::grabFrame(), rnr::CamRes::height, rnr::Camera::isCameraRunning(), rnr::Camera::isEqResolution(), rnr::Camera::m_bTakingImage, rnr::Camera::m_resCurrent, rnr::Camera::m_resImage, rnr::Camera::m_resVideo, rnr::Camera::setCameraResolution(), rnr::Camera::startVideo(), rnr::Camera::stopVideo(), and rnr::CamRes::width.

Referenced by rnr::CameraGst::grabFrame().

189 {
190  CamRes resVideoPrev = m_resVideo;
191  bool bCamWasRunning = isCameraRunning();
192  CamRes res = resImage;
193  int rc;
194 
195  m_bTakingImage = true;
196 
197  // default is the current image resolution
198  if( isEqResolution(res, CamResDft) )
199  {
200  res = m_resImage;
201  }
202 
203  //
204  // Camera not running - start camera at image resolution.
205  //
206  if( !isCameraRunning() )
207  {
208  if( (rc = startVideo(res)) < 0 )
209  {
210  m_bTakingImage = false;
211  return RC_ERROR;
212  }
213  }
214 
215  //
216  // Camera already running, but at a different resolution. Set camera at image
217  // resolution.
218  //
219  else if( !isEqResolution(res, m_resCurrent) )
220  {
221  setCameraResolution(res);
222  }
223 
225 
226  //
227  // Grab a video frame.
228  //
229  if( grabFrame(img) < 0 )
230  {
231  LOGERROR("Cannot grab a snap shot.");
232  m_bTakingImage = false;
233  return RC_ERROR;
234  }
235 
236  //
237  // Success
238  //
239  m_resImage.width = img.cols;
240  m_resImage.height = img.rows;
241 
242  LOGDIAG3("Took a %dx%d still image.", m_resImage.width, m_resImage.height);
243 
244  m_bTakingImage = false;
245 
246  m_resVideo = resVideoPrev;
247 
249  {
251  }
252 
253  if( !bCamWasRunning )
254  {
255  stopVideo();
256  }
257 
258  return OK;
259 }
static bool isEqResolution(const CamRes &res1, const CamRes &res2)
Check is two camera resolutions are equal.
Definition: Camera.h:257
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
virtual int startVideo(const CamRes &resVideo=CamResDft)
Start the camera streaming video.
Definition: CameraCv.cxx:89
const CamRes CamResDft
default resolution
Definition: Camera.h:89
bool m_bTakingImage
taking an image is [not] finished
Definition: Camera.h:299
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
virtual CamRes setCameraResolution(const CamRes &res)
Set the camera resolution in either video or still image mode.
Definition: CameraCv.cxx:265
virtual int stopVideo()
Stop the camera from streaming video.
Definition: CameraCv.cxx:144
Camera resolution structure.
Definition: Camera.h:79
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
CamRes m_resImage
current still image resolution
Definition: Camera.h:297
virtual int grabFrame(cv::Mat &frame)
Grab a image frame from the video stream.
Definition: CameraCv.cxx:160
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
cv::VideoCapture& rnr::CameraCv::getCaptureObj ( )
inline

Get OpenCV captured object.

Returns
Returns CvCapture*.

Definition at line 149 of file CameraCv.h.

References m_capture.

150  {
151  return m_capture;
152  }
cv::VideoCapture m_capture
video capture object
Definition: CameraCv.h:155
int CameraCv::grabFrame ( cv::Mat &  frame)
virtual

Grab a image frame from the video stream.

Parameters
[in,out]Theimage frame matrix. May be resized.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 160 of file CameraCv.cxx.

References rnr::Camera::isCameraRunning().

161 {
162  if( !isCameraRunning() )
163  {
164  LOGERROR("No video capture object.");
165  return RC_ERROR;
166  }
167 
168  // grab a frame into internal buffer
169  else if( !m_capture.grab() )
170  {
171  LOGERROR("Frame grab() failed.");
172  return RC_ERROR;
173  }
174 
175  // convert grabbed frame to RGB image (not saved)
176  else if( !m_capture.retrieve(frame) )
177  {
178  LOGERROR("Frame retrieve() failed.");
179  return RC_ERROR;
180  }
181 
182  else
183  {
184  return OK;
185  }
186 }
cv::VideoCapture m_capture
video capture object
Definition: CameraCv.h:155
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
CamRes CameraCv::setCameraResolution ( const CamRes res)
protectedvirtual

Set the camera resolution in either video or still image mode.

Parameters
resTarget camera resolution.
Returns
Actual resolution set.

Reimplemented from rnr::Camera.

Definition at line 265 of file CameraCv.cxx.

References rnr::CamRes::height, rnr::Camera::m_resCurrent, and rnr::CamRes::width.

Referenced by rnr::CameraGst::clickImage(), and rnr::CameraGst::startVideo().

266 {
267  m_capture.set(CV_CAP_PROP_FRAME_WIDTH, res.width);
268  m_capture.set(CV_CAP_PROP_FRAME_HEIGHT, res.height);
269 
270  m_resCurrent = res;
271 
272  return m_resCurrent;
273 }
cv::VideoCapture m_capture
video capture object
Definition: CameraCv.h:155
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
int CameraCv::startVideo ( const CamRes resVideo = CamResDft)
virtual

Start the camera streaming video.

Parameters
resVideoVideo resolution. If equal to CamResDft, then the initial/last video resolution setting is used.
Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 89 of file CameraCv.cxx.

References rnr::CamResDft, rnr::CamResQVGA, rnr::Camera::grabFrame(), rnr::CamRes::height, rnr::Camera::isCameraRunning(), rnr::Camera::isEqResolution(), rnr::Camera::m_bCameraRunning, rnr::Camera::m_bFatal, rnr::Camera::m_nVideoIndex, rnr::Camera::m_resCurrent, rnr::Camera::m_resVideo, rnr::Camera::m_strVideoDevName, rnr::Camera::setCameraResolution(), and rnr::CamRes::width.

Referenced by rnr::CameraGst::clickImage().

90 {
91  CamRes res = resVideo;
92  Mat img;
93 
94  // default is the current video resolution
95  if( isEqResolution(res, CamResDft) )
96  {
97  res = m_resVideo;
98  }
99 
100  errno = 0;
101 
102  // start video
103  if( !isCameraRunning() )
104  {
105  // start OpenCV video capture
106  if( !m_capture.open(m_nVideoIndex) )
107  {
108  LOGSYSERROR("%s: Could not open video device.",
109  m_strVideoDevName.c_str());
110  m_bFatal = true;
111  return RC_ERROR;
112  }
113  }
114 
115  m_bCameraRunning = true;
116 
117  // always set video resolution else camera may default to an alternate size
119 
120  //
121  // Grab a frame into internal buffer to get the actual resolution.
122  //
123  if( grabFrame(img) < 0 )
124  {
125  LOGERROR("Cannot determine captured image size, using defaults");
127  }
128 
129  // set actual resolution
130  else
131  {
132  m_resVideo.width = img.cols;
133  m_resVideo.height = img.rows;
134  }
135 
136  // update current to match video
138 
139  LOGDIAG3("Capturing %dx%d video.", m_resVideo.width, m_resVideo.height);
140 
141  return OK;
142 }
static bool isEqResolution(const CamRes &res1, const CamRes &res2)
Check is two camera resolutions are equal.
Definition: Camera.h:257
cv::VideoCapture m_capture
video capture object
Definition: CameraCv.h:155
CamRes m_resCurrent
current camera resolution
Definition: Camera.h:295
const CamRes CamResDft
default resolution
Definition: Camera.h:89
CamRes m_resVideo
current video resolution
Definition: Camera.h:296
bool m_bCameraRunning
camera is [not] on and running video
Definition: Camera.h:298
const CamRes CamResQVGA
Quarter VGA 320 x 240 res.
Definition: Camera.h:90
virtual CamRes setCameraResolution(const CamRes &res)
Set the camera resolution in either video or still image mode.
Definition: CameraCv.cxx:265
Camera resolution structure.
Definition: Camera.h:79
int width
width in pixels
Definition: Camera.h:81
int height
height in pixels
Definition: Camera.h:82
int m_nVideoIndex
video index
Definition: Camera.h:294
std::string m_strVideoDevName
video device name
Definition: Camera.h:293
virtual int grabFrame(cv::Mat &frame)
Grab a image frame from the video stream.
Definition: CameraCv.cxx:160
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224
bool m_bFatal
camera instance is in a fatal state
Definition: Camera.h:300
int CameraCv::stopVideo ( )
virtual

Stop the camera from streaming video.

Returns
On success, OK(0) is returned.
On error, RC_ERROR(-1) is returned.

Reimplemented from rnr::Camera.

Definition at line 144 of file CameraCv.cxx.

References rnr::Camera::isCameraRunning(), and rnr::Camera::m_bCameraRunning.

Referenced by rnr::CameraGst::clickImage(), and rnr::CameraGst::~CameraGst().

145 {
146  if( !isCameraRunning() )
147  {
148  LOGDIAG3("Video capture already stoped.");
149  return OK;
150  }
151 
152  // capture frame will be released here also
153  m_capture.release();
154 
155  m_bCameraRunning = false;
156 
157  LOGDIAG3("Video capturing stopped.");
158 }
cv::VideoCapture m_capture
video capture object
Definition: CameraCv.h:155
bool m_bCameraRunning
camera is [not] on and running video
Definition: Camera.h:298
bool isCameraRunning() const
Test if the camera is on and running.
Definition: Camera.h:224

The documentation for this class was generated from the following files: