cv::Sobel
- User
cv::Sobel
- Assistant
cv::Sobelis 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::Sobelfunction in OpenCV:Function Signature and Parameters
The prototype for the
cv::Sobelfunction 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 );- src: Input image.
- dst: Output image of the same size and the specified type.
- ddepth: The depth of the output image. If it is negative, it will be the same as the source image depth. Passing
-1will use the same depth as the source. Common practice is to useCV_32Fto avoid overflow. - dx: Order of the derivative x. It specifies the order of the derivative in the x-direction.
- dy: Order of the derivative y. It specifies the order of the derivative in the y-direction.
- ksize: Size of the extended Sobel kernel; it must be 1, 3, 5, or 7. A larger size will lead to a smoother image and better for detecting larger edges.
- scale: Optional scale factor for the computed derivative values; this is useful to scale the result back into a representable range for unsigned 8-bit or signed 16-bit images.
- delta: Optional bias added to the result before storing it; it is useful for adjusting the result before converting it back to the original depth.
- borderType: Pixel extrapolation method (
BORDER_DEFAULT,BORDER_REPLICATE,BORDER_REFLECT, etc.).
Example Usage
Here's an example of how to use
cv::Sobelto 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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.