A window-based camera library in c++.
- Work with OpenCV::Mat
- Support all camera settings
- Support Exposure Fusion
- A looper to handle the camera cycle
- Internal fake camera (Stub) for testing
- Usage
- Looper
- Exposure Fusion
- Using camera without OpenCV
- Requirements
- Installation
- Deploy example codes
- License
#include <uvc_camera.h>
using namespace DirectShowCamera;
// Get a empty camera
UVCCamera camera = UVCCamera();
// Get available camera list. You can use getResolutions() to acquire the resolutions
std::vector<CameraDevice> cameraDeivceList = camera.getCameras();
// Open the first camera
camera.open(cameraDeivceList[0]);
// Start Capture
camera.startCapture();
// Wait 1 second
std::this_thread::sleep_for(std::chrono::seconds(1));
// Capture a frame
cv::Mat frame = camera.getMat();
// Close the camera
camera.close();
For more details, please see example code , Documentation and Software Diagrams
Looper is a loop thread worked in detach mode, making the camera life cycle easy to handle.
#include <uvc_camera_looper.h>
#include <iostream>
using namespace DirectShowCamera;
// Create a looper
UVCCameraLooper cameraLooper = UVCCameraLooper();
// Open the first camera
std::vector<CameraDevice> cameraDeivceList = cameraLooper.getCamera()->getCameras();
cameraLooper.getCamera()->open(cameraDeivceList[0]);
// Set a process to handle the captured image
cameraLooper.setCapturedProcess(
[](cv::Mat image)
{
std::cout << "Get a new image" << std::endl;
}
);
// Start looper to capture image continously
cameraLooper.start();
// 1 second
std::this_thread::sleep_for(std::chrono::seconds(1));
// Stop looper
cameraLooper.stop();
Exposure fusion is a technique to capture a high quality image by fusing multi-exposure sequence.
cv::Mat frame = camera.exposureFusion();
This library can be used without opencv.
- Find ds_libs_setting.h in the include/directshow_camera folder.
- Comment this line
//#define HAS_OPENCV
Then you can use the following code to get frame bytes in BGR order.
int frameSize;
unsigned char* image = new unsigned char[1208 * 760 * 3];
camera.getFrame(&image, &size);
Minimum C++ 17
vcpkg commit version: #14121
OpenCV version: 4.3.0 (Option)
-
Create libs folder in you project
-
Clone this repository into the libs folder
git clone https://github.com/kcwongjoe/directshow_camera.git
-
Add the code in your project CMakeLists.txt
# Set Window SDK Path. Change it to your Window SDK Path set(CMAKE_WINDOWS_KITS_10_DIR "C:/Program Files (x86)/Windows Kits/10") # Camera library add_subdirectory(./libs/directshow_camera) target_link_libraries(${PROJECT_NAME} PRIVATE directshow_camera )
-
Install OpenCV library in CMake from vcpgk (Option)
-
Add the code in your project CMakeLists.txt
# Set vcpkg path. Change here if the vcpkg folder is not located in your solution folder. set(VCPKG_PATH ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg) set(CMAKE_TOOLCHAIN_FILE ${VCPKG_PATH}/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file") # OpenCV # Install OpenCV from vcpkg. See cmake/InstallVcpkgOpenCV.cmake install_vcpkg_opencv() # Link OpenCV include_directories( ${OpenCV_INCLUDE_DIRS} ) link_directories( ${OpenCV_LIB_DIR} ) target_link_libraries(${PROJECT_NAME} PRIVATE OpenCV_LIBS )
-
Copy dll from vcpkg. You can use copy_dll.bat or build-in CMake module to copy necessary dll from vcpkg.
- Type
copy_dll.bat x86
orcopy_dll.bat x64
- Add the code in your project CMakeLists.txt
# dll will be copied when the target built. prebuild_copy_vcpkg_opencv_dll("<YOUR_TARGET>" "<COPY TO, e.g. ${CMAKE_BINARY_DIR}/examples>")
- Type
-
- Copy all files inside src folder and include/directshow_camera folder to your project.
-
Install Window SDK
- Install: https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/
- Go to config folder, Rename window_sdk_path_example.txt to window_sdk_path.txt and type your Window SDK path in the text file.
-
Clone this repository
git clone --recurse-submodules https://github.com/kcwongjoe/directshow_camera.git
-
Install vcpkg in the project folder
- Clone the vcpkg repo from https://github.com/Microsoft/vcpkg
- Unzip and rename folder as vcpkg
- Place in the solution folder
- Run bootstrap-vcpkg.bat in the vcpkg folder
- If your vcpkg folder was not located in the solution folder, you have to specify the path in the config folder. Go to config folder, Rename vcpkg_path_example.txt to vcpkg_path.txt and type your vcpkg path in the text file.
-
Install OpenCV by vcpkg
- Go to vcpkg folder
- Type
vcpkg install opencv:x86-windows
- Type
vcpkg install opencv:x64-windows
-
Run build.bat in Solution folder
Type
build x86
orbuild x64
-
Go to build folder and open visual studio solution.
-
Build solution in visual studio. Executable file will be constructed but you may encounter a missing dll error. Then, go to Solution folder and run copy_dll.bat to copy dll from vcpkg folder.
Type
copy_dll x86
orcopy_dll x64
This project is licensed under MIT license.