PiImageStreamer
Loading...
Searching...
No Matches
SWE4211RPi::NewCamera Class Reference
Inheritance diagram for SWE4211RPi::NewCamera:
Collaboration diagram for SWE4211RPi::NewCamera:

Public Member Functions

 NewCamera (int width, int height, std::string threadName)
 
virtual ~NewCamera ()
 
void taskMethod ()
 
cv::Mat * takePicture ()
 
- Public Member Functions inherited from SWE4211RPi::FreeRunningRepeatingTask
 FreeRunningRepeatingTask (std::string threadName, int priority=1)
 
virtual ~FreeRunningRepeatingTask ()
 
void run () final
 
virtual void printInformation ()
 
virtual double getCPUUsageInfo ()
 
virtual void resetThreadDiagnostics ()
 
- Public Member Functions inherited from SWE4211RPi::RunnableClass
 RunnableClass (std::string threadName, int priority=0)
 
virtual ~RunnableClass ()
 
virtual pid_t getThreadID ()
 
virtual void start () final
 
virtual void setPriority (int priority) final
 
virtual int getPriority () final
 
virtual bool isShutdown () final
 
virtual bool isStarted () final
 
virtual void stopThreadExecution ()
 
virtual void waitForShutdown ()
 

Private Attributes

cv::VideoCapture * capture
 
cv::Mat * lastFrame
 
cv::Mat capturedFrame
 
std::atomic< bool > frameRequested {false}
 
std::mutex mtx
 
bool startupCompleted = false
 

Additional Inherited Members

- Static Public Member Functions inherited from SWE4211RPi::RunnableClass
static void printThreads ()
 
static void resetAllThreadInformation ()
 
- Protected Member Functions inherited from SWE4211RPi::FreeRunningRepeatingTask
virtual void startChildRunnables () final
 
virtual void stopChildRunnables () final
 
virtual void startChildTasks ()
 
virtual void stopChildTasks ()
 
- Protected Attributes inherited from SWE4211RPi::RunnableClass
std::thread * myThread = NULL
 
pthread_t executingThread = 0
 
pid_t myOSThreadID = 0
 
std::string myName
 
std::atomic< bool > keepGoing {true}
 
std::atomic< bool > runCompleted {false}
 
std::atomic< bool > runStarted {false}
 
int priority = -1
 
- Static Protected Attributes inherited from SWE4211RPi::RunnableClass
static std::list< RunnableClass * > runningThreads
 

Constructor & Destructor Documentation

◆ NewCamera()

NewCamera::NewCamera ( int  width,
int  height,
std::string  threadName 
)

Construct a new instance of the camera class.

Parameters
widthThis is the width of the natively captured images. This is the resolution the camera generates.
heightThis is the height of the natively captured images. This is the resolution the camera generates.
threadNameThis is the name of the thread that is to be used to run the image capture.
  1. Call cv::setNumThreads(0); to set the number of threads to 0. This is given to you as it is odd code to write.
  2. Start by instantiating a VideoCapture object which will grab the images from the camera.

  3. Next, set the width, height, frames per second (FPS), and buffersize parameters of the capture. The buffersize should always be 1.

  4. Instantiate a new matrix that will hold the last frame.

  5. Check to see that the capture is opened. If if isn't,

    1. print out a failure method and exit the program with an error code.

◆ ~NewCamera()

NewCamera::~NewCamera ( )
virtual

This is the destructor for the camera. It will delete all dynamically allocated objects.

  1. Release the camera.

  2. Delete all allocated objects.

Member Function Documentation

◆ takePicture()

Mat * NewCamera::takePicture ( )

This method will request that the camera thread decode the next frame, and then return a copy of the frame decoded in response to the previous request. It does not wait for the camera. The caller is responsible for deleting the returned matrix.

Returns
The return will be a matrix of the picture that was last decoded by the camera thread. On the first call, no frame has been decoded yet, so the return will be an empty matrix.
  1. Request that the camera thread decode the next frame it grabs. That frame will be returned by the next call.

  2. Lock the mutex so that the camera thread cannot replace the last frame while it is being copied.

  3. Allocate a new matrix holding a deep copy of the last frame and return it. A deep copy is used so that the caller can modify the image (overlays, resizing) without affecting the camera's frame.
Here is the caller graph for this function:

◆ taskMethod()

void NewCamera::taskMethod ( )
virtual

This is the main thread for the camera. It will grab the next frame from the camera and, if a frame has been requested, decode it and make it available as the last frame.

This is the task method for the camera. It will grab every frame from the camera hardware, so that the camera's buffer always holds the newest frame, but it only pays the cost of decoding a frame when one has been requested by takePicture(). Because this runs on the camera's own thread, the decoding is kept out of the calling task.

  1. Grab the next frame from the camera. This blocks until the camera delivers a new frame. Grabbing does not decode the frame.
  2. If the grab succeeded and a frame has been requested, decode the grabbed frame into the working frame and then clear the request. This is done without holding the mutex, as the working frame is only used by this thread.

  3. If the decode succeeds, lock the mutex and swap the working frame with the last frame, making the new frame available without copying the pixel data.

  4. If the decode fails, restore the request so that the next grabbed frame is decoded instead.

  5. If no frame was requested, the grabbed frame is simply discarded when the next one is grabbed.

Implements SWE4211RPi::FreeRunningRepeatingTask.

Member Data Documentation

◆ capture

cv::VideoCapture* SWE4211RPi::NewCamera::capture
private

The video capture is an instance of the OpenCV Image capture class. It is instantiated in the constructor and used to capture images.

◆ capturedFrame

cv::Mat SWE4211RPi::NewCamera::capturedFrame
private

This is the working frame that the camera thread decodes into. It is only used by the camera thread, so decoding can happen without holding the mutex.

◆ frameRequested

std::atomic<bool> SWE4211RPi::NewCamera::frameRequested {false}
private

This flag is set by takePicture() to request that the camera thread decode the next frame it grabs. The camera thread clears it once it has taken on the request.

◆ lastFrame

cv::Mat* SWE4211RPi::NewCamera::lastFrame
private

This is the last frame that was captured by the camera. It is in an OpenCV Matrix format. It is shared between the camera thread and the callers of takePicture(), so it must only be accessed while holding mtx.

◆ mtx

std::mutex SWE4211RPi::NewCamera::mtx
private

This is a mutex within the camera class that prevents race conditions as the images are manipulated. It protects lastFrame as it is handed from the camera thread to the callers of takePicture().

◆ startupCompleted

bool SWE4211RPi::NewCamera::startupCompleted = false
private

This variable will determine if the startup is done or not. Startup basically means that the buffer is fully emptied.


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