A C application for real-time camera capture, edge detection, optical flow, scale/rotate/crop, display, and video recording using CUDA, Video4Linux2, SDL2.
This project provides a camera interface for:
- Edge detection, optical flow, scale/rotate/crop using CUDA
- Real-time video display with SDL2
- Frame capture and saving as PGM image files
- Automatic conversion of captured frames to MP4 video
- Linux operating system with Video4Linux2 support
- SDL2 library
- ffmpeg (for video conversion)
- CMake (version 3.10 or higher)
- C compiler with C11 support
- CUDA
# Install V4L2
sudo apt-get update
sudo apt-get install libv4l-dev v4l-utils
# Install SDL2
sudo apt-get install libsdl2-dev
# Install ffmpeg
sudo apt-get install ffmpeg
# Install CMake
sudo apt-get install cmake
# Install GCC (C compiler)
sudo apt-get install build-essential
# Install CUDA
# First, verify your GPU supports CUDA
lspci | grep -i nvidia
# Download and install CUDA from NVIDIA's website
# Visit: https://developer.nvidia.com/cuda-downloads
# Select your Linux distribution and follow the instructions
mkdir -p build
cd build
cmake ..
make
mkdir -p build
cd build
cmake .. [OPTIONS]
make
Where [OPTIONS]
can include:
-DLIVE=ON
: Enable real-time viewer mode (default: OFF)-DRCS=OFF
: Disable rotate/crop/scale processing (default: ON)-DEDGE_DETECTOR=OFF
: Disable edge detection processing (default: ON)-DOPTICAL_FLOW=OFF
: Disable optical flow processing (default: ON)
Example to build with real-time mode enabled and optical flow disabled:
cmake .. -DLIVE=ON -DOPTICAL_FLOW=OFF
make
The executable will be created in the build/bin
directory.
./bin/cuda_project /dev/videoX
Where /dev/videoX
is your camera device (e.g., /dev/video0
).
(Visible using ls /dev
)
./bin/cuda_project /dev/videoX NUM_FRAMES [crop_x crop_y crop_width crop_height] [scale] [angle]
Where:
NUM_FRAMES
is the number of frames to capturecrop_x crop_y crop_width crop_height
are optional crop parameters (all 4 must be provided if cropping is desired)scale
is an optional scale factor (default: 1.0)angle
is an optional rotation angle in degrees (default: 0.0)
Crop parameters allow you to process only a specific rectangular region of the camera image instead of the entire frame:
crop_x
: X-coordinate of the top-left corner of the crop region (in pixels)crop_y
: Y-coordinate of the top-left corner of the crop region (in pixels)crop_width
: Width of the crop region (in pixels)crop_height
: Height of the crop region (in pixels)
For example, if your camera captures 640x480 images and you want to process only a 320x240 region starting at position (100,100), you would use: 100 100 320 240
If no crop parameters are provided, the entire camera image will be processed.
Example:
./bin/cuda_project /dev/video0 100 100 100 320 240 1.5 15.0
- ESC/Q: Close the application
- F: Toggle fullscreen
- S: Take a screenshot
- RCS Commands:
- ARROWUP/ARROWDOWN Scale up/Scale down
- ARROWLEFT/ARROWRIGHT Rotate left/Rotate right
- +/- Crop/Uncrop
- WASD Move crop region
- PGM image files are saved in the
build/img
directory (format:image-#####.pgm
) - The final video is saved as
output.mp4
- The final rcs video is saved as
output_processed_rcs.mp4
- The final edge detector video is saved as
output_processed_edge.mp4
- The final optical flow video is saved as
output_processed_optflow.mp4
src/
: Source code filesmain.c
: Main application entry pointcapture_camera.c
: Camera capture functionalitysdl_viewer.c
: SDL-based display implementationcuda/
: CUDA implementation filescuda_rcs.cu
: Rotate/Crop/Scale processingcuda_edgeDetector.cu
: Edge detection processingcuda_optflow.cu
: Optical flow processingcuda_utils.cu
: Utility functions for CUDA operations
threading/
: Thread implementation files for parallel processing
includes/
: Header filesmain.h
: Main application headercapture_camera.h
: Camera capture declarationscuda/
: CUDA header filesSDL/
: SDL-related headersutils/
: Utility headers
build/
: Build directory for CMake outputbin/
: Contains the compiled executableimg/
: Output directory for captured PGM images and output video
When built with -DLIVE=ON
, the application runs in real-time mode, showing processed camera feed with selected features.
Applies geometric transformations to camera frames using CUDA acceleration. Can be disabled with -DRCS=OFF
.
Applies edge detection algorithms to camera frames using CUDA acceleration. Can be disabled with -DEDGE_DETECTOR=OFF
.
Calculates and visualizes optical flow between consecutive frames using CUDA acceleration. Can be disabled with -DOPTICAL_FLOW=OFF
.
Uses multiple threads to parallelize frame processing, allowing for efficient use of CPU and GPU resources.