1 Star 0 Fork 0

何群山 / MM-OpencvCamera

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
OpenCVCamera.h 7.29 KB
一键复制 编辑 原始数据 按行查看 历史
何群山 提交于 2020-12-10 21:41 . OpenCVCamera details
///////////////////////////////////////////////////////////////////////////////
// FILE: OpenCVCamera.h
// PROJECT: Micro-Manager
// SUBSYSTEM: DeviceAdapters
//-----------------------------------------------------------------------------
// DESCRIPTION: Device adapter for OpenCV VideoCapture Camera/Video devices
//
// AUTHOR: Qunshan He, mountain.he@qq.com, 2020/12/09
//
// COPYRIGHT: Jilin University, Jilin Province, 2020
// LICENSE: This file is distributed under the BSD license.
// License text is included with the source distribution.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
#ifndef _OPENCV_CAMERA_
#define _OPENCV_CAMERA_
#include "../../MMDevice/DeviceBase.h"
#include "../../MMDevice/ImgBuffer.h"
#include "../../MMDevice/DeviceThreads.h"
#include <string>
#include <map>
#include <algorithm>
#include <stdint.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//////////////////////////////////////////////////////////////////////////////
// Error codes
//
#define ERROR_DEVICE_NOT_FOUND 11000
#define ERROR_OPEN_CAMERA_FAILED 11001
#define ERROR_DEVICE_NOT_SUPPORT 11002
#define ERROR_SET_PROPERTY_FAILED 110003
#define ERROR_UNKNOWN_MODE 11004
#define ERROR_FRAME_ACQUISITION_FAILED 11005
class MySequenceThread;
//////////////////////////////////////////////////////////////////////////////
// OpenCVCamera class
// Simulation of the Camera device
//////////////////////////////////////////////////////////////////////////////
class OpenCVCamera : public CCameraBase<OpenCVCamera>
{
public:
OpenCVCamera();
~OpenCVCamera();
// MMDevice API
// ------------
int Initialize();
int Shutdown();
void GetName(char* name) const;
bool Busy();
// MMCamera API
// ------------
int SnapImage(); // Snap a single image
int AcquireFrameAndSetImage(); // Capture a frame and set it into mm imageBuffer
int ConvertCvMatToImageBuffer(Mat & frame, ImgBuffer &image); // Convert cvMat frame to MM ImgBuffer image
const unsigned char* GetImageBuffer(); // pointer to buffer
unsigned GetImageWidth() const; // width
unsigned GetImageHeight() const; // height
unsigned GetBitDepth() const; // bits per pixel
unsigned GetImageBytesPerPixel() const; // Bytes(8bit) per pixel
long GetImageBufferSize() const; // Width*Height*BitDepth(bit) Width*Height*BytesPerPixel(Byte)
int IsExposureSequenceable(bool& isSequenceable) const;
int GetBinning() const;
int SetBinning(int binSize);
// Exposure
double GetExposure() const;
double GetSequenceExposure();
void SetExposure(double exp);
// ROI
int GetROI(unsigned& x, unsigned& y, unsigned& xSize, unsigned& ySize);
int SetROI(unsigned x, unsigned y, unsigned xSize, unsigned ySize);
int ClearROI();
// Continuous sequence acquisition.
// Default to sequence acquisition with a high number of images
int PrepareSequenceAcqusition() { return DEVICE_OK; }
int StartSequenceAcquisition(double interval);
int StartSequenceAcquisition(long numImages, double interval_ms, bool stopOnOverflow);
int StopSequenceAcquisition(); //Stop and wait for the thread finished
int RunSequenceOnThread(MM::MMTime startTime);
int InsertImage();
bool IsCapturing();
void OnThreadExiting() throw();
// Get the channels of img_
unsigned GetNumberOfComponents() const;
// Action interface
// ----------------
// Camera
// ----------
int OnCameraIndex(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnCameraNumber(MM::PropertyBase* pProp, MM::ActionType eAct);
// image
// ----------
// Exposure ms
int OnExposure(MM::PropertyBase* pProp, MM::ActionType eAct);
// Gain
int OnGain(MM::PropertyBase* pProp, MM::ActionType eAct);
// ROI
int OnRoi_X(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnRoi_Y(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnRoi_W(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnRoi_H(MM::PropertyBase* pProp, MM::ActionType eAct);
int OnBinning(MM::PropertyBase* pProp, MM::ActionType eAct);
// Image Pixel Type
int OnPixelType(MM::PropertyBase* pProp, MM::ActionType eAct);
// Resolution
int OnResize(MM::PropertyBase* pProp, MM::ActionType eAct);
private:
int updateCameraProps();
int getCameraNum();
int setPixelType(string pixelTypeValue);
void RGB3toRGB4(uchar* srcPixels, char* destPixels, int width, int height);
int ResizeImageBuffer();
// Camera Properties
VideoCapture cap_; // OpenCV VideoCapture Implementation
long int cameraID_; // camera index
long int cameraNum_; // valid camera number
bool initialized_; // camera is initialized
bool busy_; // camera is busy
bool stopOnOverflow_; // stop when acquisition overflow
// Image Properties
Mat frame; // OpenCV Mat
ImgBuffer img_; // image buffer
std::string pixelType; // image pixel type
int nComponents_; // Channels
int bitDepth_; // Bit depth
int binSize_; // Binning Size
char* cameraBuf; // camera buffer for image transfer
bool isSequenceable_;
bool sequenceRunning_;
long sequenceMaxLength_;
unsigned long sequenceIndex_;
double readoutUs_;
MM::MMTime readoutStartTime_;
MM::MMTime sequenceStartTime_;
std::vector<double> exposureSequence_;
// for roi and image range
int roiX_; // Offset X of roi
int roiY_; // Offset Y of roi
int roiWidth_; // Image roi width
int roiHeight_; // Image roi height
int maxWidth_; // Camera max width
int maxHeight_; // Camera max height
int resize_; // Image scale
// for exposure
double exposure_; // exposure time(ms)
// for gain
double gain_; // gain
// functions variables
long imageCounter_; // imageCounter
friend class MySequenceThread;
MySequenceThread * thd_;
MMThreadLock imgPixelsLock_;
};
//////////////////////////////////////////////////////////////////////////////
// MySequenceThread class
// Simulation of the Thread
//////////////////////////////////////////////////////////////////////////////
class MySequenceThread : public MMDeviceThreadBase
{
friend class COpenCVgrabber;
enum { default_numImages = 1, default_intervalMS = 100 };
public:
MySequenceThread(OpenCVCamera* pCam);
~MySequenceThread();
void Stop()/* {MMThreadGuard(this->stopLock_); stop_ = true;} */;
void Start(long numImages, double intervalMs);
bool IsStopped();
void Suspend();
bool IsSuspended();
void Resume();
double GetIntervalMs() { return intervalMs_; }
void SetLength(long images) { numImages_ = images; }
long GetLength() const { return numImages_; }
long GetImageCounter() { return imageCounter_; }
MM::MMTime GetStartTime() { return startTime_; }
MM::MMTime GetActualDuration() { return actualDuration_; }
private:
int svc(void) throw();
double intervalMs_;
long numImages_;
long imageCounter_;
bool stop_;
bool suspend_;
OpenCVCamera* camera_;
MM::MMTime startTime_;
MM::MMTime actualDuration_;
MM::MMTime lastFrameTime_;
MMThreadLock stopLock_;
MMThreadLock suspendLock_;
};
#endif // _OPENCV_CAMERA_
C++
1
https://gitee.com/qunshanhe/mm-opencv-camera.git
git@gitee.com:qunshanhe/mm-opencv-camera.git
qunshanhe
mm-opencv-camera
MM-OpencvCamera
master

搜索帮助