cv::Sobel
- User
cv::Sobel
- Assistant
cv::Sobel
cv::Sobel is a function in OpenCV, an open-source computer vision and machine learning software library. The Sobel operator is used for edge detection in images. It works by computing an approximation of the gradient of the image intensity function. The Sobel operator combines Gaussian smoothing and differentiation, so it's more resistant to noise compared to other edge detection methods that do not use smoothing.
Here's how to use the cv::Sobel function in OpenCV:
The prototype for the cv::Sobel function typically looks something like this:
void cv::Sobel(
InputArray src,
OutputArray dst,
int ddepth,
int dx,
int dy,
int ksize = 3,
double scale = 1,
double delta = 0,
int borderType = BORDER_DEFAULT
);
-1 will use the same depth as the source. Common practice is to use CV_32F to avoid overflow.BORDER_DEFAULT, BORDER_REPLICATE, BORDER_REFLECT, etc.).Here's an example of how to use cv::Sobel to detect edges in an image:
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
// Load the input image
Mat src = imread("input.jpg", IMREAD_GRAYSCALE);
if (src.empty())
{
return -1; // Check for invalid input
}
// Create variables for the output
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y, grad;
// Apply Sobel operator in the horizontal direction
Sobel(src, grad_x, CV_16S, 1, 0, 3, 1, 0, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);
// Apply Sobel operator in the vertical direction
Sobel(src, grad_y, CV_16S, 0, 1, 3, 1, 0, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);
// Combine the two gradients
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
// Display the result
imshow("Sobel Edge Detection", grad);
waitKey(0);
return 0;
}
This code loads an input image in grayscale, applies the Sobel operator in both horizontal and vertical directions, and combines these gradients to produce a final edge map which it then displays.
Not yet included in a published release.