Skip to content

Commit

Permalink
Added a camera lib project built with OpenCV
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Jun 14, 2024
1 parent 0a75b9f commit 0b16e08
Show file tree
Hide file tree
Showing 96 changed files with 53,502 additions and 0 deletions.
12 changes: 12 additions & 0 deletions camera_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 3.10)
project(camera_lib)
# find_package(OpenCV REQUIRED)
# set(OpenCV_DIR "c:/opencv/sources") # Update this path to the location of your OpenCV source code

# set(OpenCV_MODULES_CORE ON)
# set(OpenCV_MODULES_IMGPROC ON)
# set(OpenCV_MODULES_HIGHGUI ON)
# set(OpenCV_MODULES_VIDEOIO ON)

add_subdirectory(src)
add_subdirectory(example)
34 changes: 34 additions & 0 deletions camera_lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Slimmed Down OpenCV for Camera Applications
1. Download the OpenCV source code from the [OpenCV GitHub repository](https://github.com/opencv/opencv).
2. Open a terminal or command prompt, create a build directory, and navigate into it:

```bash
mkdir build
cd build
```
3. Run the following CMake command to configure the build, disabling unnecessary components:

```bash
cmake -DBUILD_SHARED_LIBS=ON -DBUILD_opencv_world=OFF -DBUILD_opencv_apps=OFF -DBUILD_opencv_calib3d=OFF -DBUILD_opencv_dnn=OFF -DBUILD_opencv_features2d=OFF -DBUILD_opencv_flann=OFF -DBUILD_opencv_gapi=OFF -DBUILD_opencv_ml=OFF -DBUILD_opencv_objdetect=OFF -DBUILD_opencv_photo=OFF -DBUILD_opencv_stitching=OFF -DBUILD_opencv_video=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_DOCS=OFF ..
```

4. Build the OpenCV library:

```bash
cmake --build . --config Release
```

5. Copy the library files `opencv_imgproc`, `opencv_core`, `opencv_imgcodecs`, `opencv_videoio`, and `opencv_highgui` to the `lib` directory.
6. Configure `src/CMakeLists.txt` to link against the OpenCV library.

```cmake
target_link_libraries(camera_lib opencv_core480 opencv_highgui480 opencv_videoio480 opencv_imgproc480)
```
7. Build the camera application:

```bash
mkdir build
cd build
cmake ..
cmake --build . --config Release
```
22 changes: 22 additions & 0 deletions camera_lib/example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.10)
project(camera_example)

link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../lib)
# Create the executable
add_executable(camera_example main.cpp)
target_include_directories(camera_example PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)
target_link_libraries(camera_example camera_lib)

add_custom_command(TARGET camera_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:camera_lib> $<TARGET_FILE_DIR:camera_example>)

# Find all DLL files in the specified directory
file(GLOB DLL_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../lib/*.dll")

# Copy the DLL files to the executable directory
foreach(DLL_FILE ${DLL_FILES})
add_custom_command(TARGET camera_example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${DLL_FILE}
$<TARGET_FILE_DIR:camera_example>)
endforeach()
34 changes: 34 additions & 0 deletions camera_lib/example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include "camera_lib.h"

int main()
{
if (open_camera(0) == 0)
{
std::cout << "Camera opened successfully." << std::endl;
while (true)
{
ImageData frame = get_frame();
if (frame.data)
{
draw_line(&frame, 50, 50, 200, 200, 2, 255, 0, 0);
draw_line(&frame, 50, 200, 200, 50, 2, 0, 255, 0);
draw_text(&frame, "Hello, OpenCV!", 50, 50, 1, 2, 0, 255, 0);

display_image(&frame);
if (wait_key(30) >= 0)
{ // Add a delay and check for key press
release_image(&frame);
break; // Exit the loop if any key is pressed
}
release_image(&frame);
}
}
close_camera();
}
else
{
std::cerr << "Failed to open camera." << std::endl;
}
return 0;
}
43 changes: 43 additions & 0 deletions camera_lib/include/camera_lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef CAMERA_LIB_H
#define CAMERA_LIB_H

#ifdef _WIN32
#ifdef CAMERA_LIB_EXPORTS
#define CAMERA_LIB_API __declspec(dllexport)
#else
#define CAMERA_LIB_API __declspec(dllimport)
#endif
#else
#define CAMERA_LIB_API
#endif

#include <cstdint>

enum PixelFormat
{
PIXEL_FORMAT_BGR,
PIXEL_FORMAT_GRAY
};

extern "C"
{
typedef struct
{
int width;
int height;
int stride;
PixelFormat pixel_format;
uint8_t *data;
} ImageData;

CAMERA_LIB_API int open_camera(int camera_index);
CAMERA_LIB_API void close_camera();
CAMERA_LIB_API ImageData get_frame();
CAMERA_LIB_API void release_image(ImageData *image);
CAMERA_LIB_API void display_image(const ImageData *image);
CAMERA_LIB_API void draw_line(ImageData *image, int x1, int y1, int x2, int y2, int thickness, int r, int g, int b);
CAMERA_LIB_API void draw_text(ImageData *image, const char *text, int x, int y, int font_scale, int thickness, int r, int g, int b);
CAMERA_LIB_API int wait_key(int delay);
}

#endif // CAMERA_LIB_H
Loading

0 comments on commit 0b16e08

Please sign in to comment.