Skip to content

Instantly share code, notes, and snippets.

@drabaioli
drabaioli / filter_webcam.cpp
Created November 25, 2020 10:46
Visualize image after kernel filtering
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
int main( int argc, char ** argv )
{
cv::VideoCapture cap;
cv::Mat frame;
constexpr int FILTER_SIZE = 3;
const std::string windowName{ "Filter image" };
@drabaioli
drabaioli / circular_buffer.h
Last active May 20, 2022 18:17
Circular buffer on top of std::array (push only)
#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H
#include <array>
// Array-based push-only circular buffer.
// Sample usage:
//
// CircularBuffer<int, 3> buff;
@drabaioli
drabaioli / image_collage.cpp
Created December 1, 2020 15:54
Stitch images into a MxN collage
#include <opencv2/opencv.hpp>
cv::Mat imageCollage( std::vector<cv::Mat> & array_of_images, int M, int N )
{
// All images should be the same size
const cv::Size images_size = array_of_images[0].size();
// Create a black canvas
cv::Mat image_collage( images_size.height * N, images_size.width * M, CV_8UC3, cv::Scalar( 0, 0, 0 ) );
for( int i = 0; i < N; ++i )
@drabaioli
drabaioli / split_image.cpp
Created December 1, 2020 15:56
Split image into an MxN grid
#include <opencv2/opencv.hpp>
std::vector<cv::Mat> splitImage( cv::Mat & image, int M, int N )
{
// All images should be the same size ...
int width = image.cols / M;
int height = image.rows / N;
// ... except for the Mth column and the Nth row
int width_last_column = width + ( image.cols % width );
int height_last_row = height + ( image.rows % height );
@drabaioli
drabaioli / opencv_rtp_capture.cpp
Created December 7, 2021 19:27
Capture an RTP stream in OpenCV with GStreamer backend
#include <opencv2/opencv.hpp>
int main( int argc, char ** argv )
{
cv::VideoCapture cap;
cv::Mat frame;
const std::string windowName{ "RTP Capture" };
cap.open( "udpsrc port=5000 caps = \"application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96\" ! rtph264depay ! decodebin ! videoconvert ! appsink", cv::CAP_GSTREAMER );
@drabaioli
drabaioli / opencv_capture_to_rtp_stream.cpp
Created December 7, 2021 19:29
Create an RTP stream from a webcam with OpenCV and GStreamer
#include <opencv2/opencv.hpp>
int main( int argc, char ** argv )
{
cv::VideoCapture cap;
cv::Mat frame;
const std::string windowName{ "RTP Stream" };
cap.open( 0 );
cap.set( cv::CAP_PROP_FRAME_WIDTH, 640 );