openCV (Open SourceComputer Vision) is a powerful library used for image processing and computer vision tasks. This guide covers several key methods with examples and parameter descriptions for each.
cv::imread() reads an image from a file and returns it as a
cv::Mat
object.
cv::Mat image = cv::imread("path_to_image", cv::IMREAD_COLOR);
filename
(string): Path to the image file.flags
(int, optional): Image reading flags:
cv::IMREAD_COLOR
: Loads the image in the BGR format (default).cv::IMREAD_GRAYSCALE
: Loads the image as a grayscale image.cv::IMREAD_UNCHANGED
: Loads the image including alpha channel.cv::Mat img = cv::imread("example.jpg", cv::IMREAD_GRAYSCALE);
if(img.empty()) {
std::cerr << "Image loading failed!";
}
cv::imshow() displays an image in a specified window.
cv::imshow("WindowName", imageMat);
winname
(string): Name of the window where the image is displayed.mat
(cv::Mat): Image to be displayed.cv::imshow("Example Image", img);
cv::waitKey(0);
cv::GaussianBlur() smooths an image using a Gaussian filter to reduce noise and detail.
cv::GaussianBlur(src, dst, cv::Size(ksizeX, ksizeY), sigmaX, sigmaY);
src
(cv::Mat): Source image.dst
(cv::Mat): Destination image where the result is stored.ksize
(cv::Size): Gaussian kernel size (width, height), must be odd numbers.sigmaX
(double): Gaussian kernel standard deviation in X direction.sigmaY
(double, optional): Gaussian kernel standard deviation in Y direction (if 0,
same as sigmaX).Parameter | Type | Description |
---|---|---|
lowThreshold | double | Lower boundary for hysteresis thresholding. |
highThreshold | double | Upper boundary for hysteresis thresholding. |
apertureSize | int | Size of the Sobel operator kernel (optional). |
L2gradient | bool | Use more accurate L2 norm (optional). |
cv::Mat blurred;
cv::GaussianBlur(img, blurred, cv::Size(5,5), 1.5);
cv::Canny() performs edge detection using the Canny algorithm.
cv::Canny(src, edges, threshold1, threshold2, apertureSize);
src
(cv::Mat): Input single-channel (grayscale) image.edges
(cv::Mat): Output binary image with detected edges.threshold1
(double): Lower threshold for the hysteresis procedure.threshold2
(double): Upper threshold for the hysteresis procedure.apertureSize
(int, optional): Aperture size for Sobel operator (default: 3).Parameter | Type | Description |
---|---|---|
lowThreshold | double | Lower boundary for hysteresis thresholding. |
highThreshold | double | Upper boundary for hysteresis thresholding. |
apertureSize | int | Size of the Sobel operator kernel (optional). |
L2gradient | bool | Use more accurate L2 norm (optional). |
Hysteresis thresholding uses two thresholds: lowThreshold and highThreshold. Pixels with gradient magnitudes above the high threshold are marked as strong edges, while pixels below the low threshold are discarded. Pixels between the thresholds are considered weak edges and are only included if they are connected to strong edges, ensuring that edges are continuous and accurate.
In the context of OpenCV’s Canny
function, hysteresis refers to the double-thresholding step that decides which gradient magnitudes correspond to “true” edges. Instead of a single cutoff, Canny uses two thresholds:
threshold1
(the low threshold)
— any pixel with gradient magnitude below this is immediately discarded.
threshold2
(the high threshold)
— any pixel with gradient magnitude above this is immediately marked as a “strong” edge.
A single threshold can either miss faint but continuous edges (if set too high) or include a lot of noise (if set too low). Hysteresis overcomes this by:
// src: input grayscale image (CV_8U or CV_16U)
// dst: output binary edge map (edges drawn in white on black background)
double lowThresh = 50.0; // threshold1
double highThresh = 150.0; // threshold2
int apertureSize = 3; // Sobel kernel size (must be odd: 3, 5, or 7)
bool L2gradient = false; // false: use L1 norm, true: use L2 norm
// Apply Canny with hysteresis thresholding
cv::Canny(src, dst, lowThresh, highThresh, apertureSize, L2gradient);
// 1. Compute gradient magnitude G and direction θ at each pixel.
// 2. Non-maximum suppression: thin edges by keeping only local maxima along θ.
// 3. Double-threshold (hysteresis):
// - If G(x,y) ≥ highThresh → mark as STRONG_EDGE.
// - If G(x,y) < lowThresh → mark as NO_EDGE.
// - If lowThresh ≤ G(x,y) < highThresh → mark as WEAK_EDGE.
// 4. Edge tracking by connectivity:
// - For each WEAK_EDGE pixel, check 8 neighbours.
// - If at least one neighbour is a STRONG_EDGE, promote this pixel to STRONG_EDGE.
// - Otherwise, suppress it (set to NO_EDGE).
// 5. Final binary map: all STRONG_EDGE pixels become white (255), others black (0).
Recall: Canny uses two thresholds— low (t₁) and high (t₂)—and only keeps “weak” edges (t₁ ≤ gradient < t₂) if they connect to a “strong” edge (gradient ≥ t₂).
t₁=20
, t₂=40
t₁=50
, t₂=100
t₁=50
, t₂=150
t₁=100
, t₂=200
// Very permissive: captures texture & noise
cv::Canny(src, dst1, 20, 40);
// Permissive: captures clear edges + some details
cv::Canny(src, dst2, 50, 100);
// Balanced: favors true object outlines
cv::Canny(src, dst3, 50, 150);
// Strict: only strongest edges
cv::Canny(src, dst4, 100, 200);
Key takeaway: - Lower thresholds → more edges (including noise & texture). - Higher thresholds → fewer, stronger edges (filtering out faint details). - The gap between t₁ and t₂ (hysteresis) lets you keep meaningful weak edges while discarding isolated noise.
1:2
and 1:3
.
A smaller ratio (e.g. 1:2
) is more permissive, capturing finer details but risking noise.
L2gradient=true
) gives slightly more accurate edge strength than the default sum of absolute derivatives (L1
), at a small performance cost.
By understanding hysteresis thresholding, you can more effectively adjust your two thresholds to balance noise suppression against edge completeness in your application.
cv::Mat edges;
cv::Canny(img, edges, 100, 200);
cv::imshow("Edges", edges);
cv::VideoCapture() captures frames from a video file or camera device.
cv::VideoCapture cap(0); // closes default camera device
index
(int): Device index (0 is usually the default camera).filename
(string): Path to the video file.cv::VideoCapture cap(0);
if(!cap.iscloseed()) {
std::cerr << "Camera failed to close!";
}
cv::Mat frame;
cap >> frame; // capture frame
cv::imshow("Camera", frame);
Thresholding methods convert images into binary or simplified grayscale images based on intensity values. Below are the key thresholding methods provided by openCV.
Applies a fixed-level threshold to each pixel in a grayscale image, producing a binary or thresholded image.
double cv::threshold(src, dst, thresh, maxValue, thresholdType);
src
(cv::Mat): Source grayscale image.dst
(cv::Mat): Destination image for thresholded output.thresh
(double): Threshold intensity value.maxValue
(double): Value to assign pixels exceeding threshold.thresholdType
(int): Thresholding method used:
cv::THRESH_BINARY
cv::THRESH_BINARY_INV
cv::THRESH_TRUNC
cv::THRESH_TOZERO
cv::THRESH_TOZERO_INV
cv::Mat binary;
cv::threshold(img, binary, 127, 255, cv::THRESH_BINARY);
cv::imshow("Thresholded Image", binary);
Applies adaptive thresholding to an image, calculating thresholds for small regions dynamically rather than globally.
cv::adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C);
src
(cv::Mat): Source grayscale image.dst
(cv::Mat): Destination image after adaptive thresholding.maxValue
(double): Value to assign pixels exceeding adaptive threshold.adaptiveMethod
(int): Method for calculating adaptive threshold:
cv::ADAPTIVE_THRESH_MEAN_C
– mean of neighborhood areacv::ADAPTIVE_THRESH_GAUSSIAN_C
– weighted sum (Gaussian)thresholdType
(int): Usually cv::THRESH_BINARY
or
cv::THRESH_BINARY_INV
.blockSize
(int): Size of neighborhood area used (must be odd, e.g., 3, 5, 7).C
(double): Constant subtracted from calculated adaptive threshold.cv::Mat adaptThresh;
cv::adaptiveThreshold(img, adaptThresh, 255,
cv::ADAPTIVE_THRESH_GAUSSIAN_C,
cv::THRESH_BINARY, 11, 2);
cv::imshow("Adaptive Threshold", adaptThresh);
Otsu's method automatically determines the optimal threshold value by analyzing the image histogram to maximize between-class variance.
cv::threshold(src, dst, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
src
(cv::Mat): Source grayscale image.dst
(cv::Mat): Destination image after thresholding.0
(double): Ignored since threshold is automatically determined.255
(double): Max value assigned to pixels above calculated threshold.thresholdType
: Must include cv::THRESH_OTSU
.cv::Mat otsuImg;
cv::threshold(img, otsuImg, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
cv::imshow("Otsu Thresholding", otsuImg);
The Hough Circle Transform is a feature extraction technique used to detect circles in a grayscale image. OpenCV implements this through the method cv::HoughCircles().
cv::HoughCircles() detects circles in a grayscale image using the Hough Gradient method.
cv::HoughCircles(image, circles, method, dp, minDist, param1, param2, minRadius, maxRadius);
image
(cv::Mat): 8-bit, single-channel grayscale input image (blurred recommended).
circles
(cv::Mat): A vector of detected circles. Each circle is encoded as a 3-element
float vector (x, y, radius)
.method
(int): Detection method. Currently only cv::HOUGH_GRADIENT
is
supported.dp
(double): Inverse ratio of accumulator resolution to image resolution. Use 1 to keep
the same resolution.minDist
(double): Minimum distance between the centers of the detected circles.param1
(double): First method-specific parameter (higher threshold for Canny edge
detector).param2
(double): Second method-specific parameter (accumulator threshold for circle
detection).minRadius
(int): Minimum circle radius.maxRadius
(int): Maximum circle radius.cv::Mat gray, blurred;
cv::cvtColor(inputImage, gray, cv::COLOR_BGR2GRAY);
cv::GaussianBlur(gray, blurred, cv::Size(9, 9), 2, 2);
std::vectorcv::Vec3f circles;
cv::HoughCircles(blurred, circles, cv::HOUGH_GRADIENT, 1,
gray.rows/8, // minDist
100, // param1 (Canny high threshold)
30, // param2 (accumulator threshold)
0, 0); // minRadius, maxRadius
for(size_t i = 0; i < circles.size(); i++) {
cv::Vec3i c = circles[i];
cv::circle(inputImage, cv::Point(c[0], c[1]), c[2], cv::Scalar(0,255,0), 2);
cv::circle(inputImage, cv::Point(c[0], c[1]), 2, cv::Scalar(0,0,255), 3);
}
cv::imshow("Detected Circles", inputImage);
cv::waitKey(0);
Contours are useful in shape analysis, object detection, and recognition. OpenCV provides methods to find and draw contours in binary images.
Detects contours from a binary image. Contours are curves joining all continuous points along a boundary with the same intensity.
cv::findContours(image, contours, hierarchy, mode, method);
image
(cv::Mat): 8-bit single-channel binary source image (modified by the
function).contours
(std::vector<std::vector<cv::Point>>): Output vector of
contours, each represented as a vector of points.hierarchy
(std::vector<cv::Vec4i>): Optional output vector containing
hierarchy information.mode
(int): Contour retrieval mode:
cv::RETR_EXTERNAL
: Retrieves only external contours.cv::RETR_LIST
: Retrieves all contours without establishing hierarchy.cv::RETR_TREE
: Retrieves all and reconstructs full hierarchy.method
(int): Contour approximation method:
cv::CHAIN_APPROX_NONE
: Stores all points.cv::CHAIN_APPROX_SIMPLE
: Compresses horizontal, vertical, and diagonal
segments.cv::Mat gray, binary;
cv::cvtColor(inputImage, gray, cv::COLOR_BGR2GRAY);
cv::threshold(gray, binary, 100, 255, cv::THRESH_BINARY);
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(binary, contours, hierarchy,
cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
cv::Mat contourOutput = inputImage.clone();
cv::drawContours(contourOutput, contours, -1, cv::Scalar(0, 255, 0), 2);
cv::imshow("Contours", contourOutput);
cv::waitKey(0);
Draws contours and individual contour segments on an image.
cv::drawContours(image, contours, contourIdx, color, thickness);
image
(cv::Mat): Image to draw the contours on.contours
(std::vector<std::vector<cv::Point>>): All detected contours.
contourIdx
(int): Index of the contour to draw (-1 draws all).color
(cv::Scalar): Line color (e.g., cv::Scalar(0,255,0)
for green).
thickness
(int): Thickness of the contour lines.// Already called findContours above
cv::Mat output = inputImage.clone();
cv::drawContours(output, contours, -1, cv::Scalar(255, 0, 0), 2);
cv::findContours()
modifies the source image, so clone it beforehand if needed
later.cv::arcLength()
and
cv::contourArea()
.param1
and param2
need tuning based on lighting and circle
contrast in the input image.(x_center, y_center, radius)
.Ensure openCV is properly installed and configured in your development environment. Methods such as imshow() require cv::waitKey() to refresh windows.
Threshold methods typically require grayscale input. Use cv::cvtColor() to convert color images to grayscale before thresholding:
cv::Mat gray; cv::cvtColor(colorImage, gray, cv::COLOR_BGR2GRAY);