The programs were tested with the docker image described in the provided Dockerfile. This pulls and builds the latest version of OpenCV and OpenCV_contrib (4.3.0 at the time of writing) To build the image with a given name "img_name", run in your local terminal:
docker build -t <img_name> <Dockerfile path>
You can then run a docker container based on the image:
docker run -it --rm -v $(realpath OpenCV_demos):/home/ --net=host --env="DISPLAY" --volume "$HOME/.Xauthority:/root/.Xauthority:rw" --device=/dev/video0:/dev/video0 <img_name> /bin/bash
This will allow us to access the OpenCV_demos directory from the container, launch windows through the hosts X-server and use the hosts default camera. Note that to access the camera we need root access in the container. If the camera is not needed you can add the option "-u :" when executin docker run. A typical choice is "-u 1000:1000", running in the container as the host ID.
mkdir build && cd build
cmake ..
make
A basic implementation of the Harris corner detector
where (u, v) are the shifts around a point p in the image, (x, y) are in a neighborhood around p and I_x, I,y are the derivatives of the image at the point p in the horizontal and vertical direction.
In short, the algorithm steps are:
- Computing the derivatives by filtering the image with the Sobel kernel
- Computing the per-element products of the derivatives
- Filtering the products with a Gaussian kernel and constructing the M matrix
- Compute the response of the detector R (shown below)
- Apply non-maximum suppression to yield the corners
Apply the method by running:
cd build
./harris <img_path>
Application of the Canny edge detector in a camera feed. The main steps are:
- Denoise image by applying a Gaussian filter (might not be needed for cheaper cameras with soft lens)
- Compute the gradient in polar coordinates
- Apply non-maximum suppression to get rid of spurious response to edge detection
- Apply double thresholding on the gradient values, categorizing them as non-edges, weak and strong edges
- Iteratively determine if weak edges are connected to strong ones. These are promoted into strong edges, while the rest are discarded
To run the detector run the command below. The method is applied to your default camera feed. Use the trackbar to set the lower threshold value (the upper threshold is set to two times the lower threshold value).
cd build
./canny_test
A basic motion detction implementation.
- Compute the intensity differense of two consecutive greyscale frames
- Apply the canny edge detector and dilate the strong edges
- Find the contours of the candidate movement regions
- If the area of a contour is above a threshold then count the contour as valid movement and draw the best fitting ellipse around the area
We can adjust both the canny detector threshold and the contour area threshold via trackbars on the window. The demo can be used with either live feed from a camera (assumed located in /dev/video[cam_index]) or a video file provided by the user.
./motion_detector -type=cam -cam_index=<integer>(default: 0)
./motion_detector -type=vid -vid_filename=<path to video>