Steps for Sobel Boundary Detection:
1. Calculate the horizontal and vertical changes:
2. Calculate the Approximate Gradient of each pixel in the image:
Or sometimes it is simplified:
# Include "opencv2/imgproc. HPP "# include" opencv2/highgui. HPP "# include <stdlib. h> # include <stdio. h> using namespace CV; int main (INT argc, char ** argv) {mat SRC, src_gray; MAT grad; char * window_name = "Sobel demo-simple edge detector "; int scale = 1; // default value: int Delta = 0; // default value: int ddepth = cv_16s; // prevent the output image depth from overflowing with int C; // load the image src = imread (argv [1]); If (! SRC. data) {return-1;} // Gaussian Blur gaussianblur (SRC, SRC, size (3, 3), 0, 0, border_default); // converts the image to a grayscale cvtcolor (SRC, src_gray, cv_rgb2gray); // create a window named window (window_name, cv_window_autosize); // generate grad_x and grad_y mat grad_x, grad_y; MAT abs_grad_x, abs_grad_y; // gradient x direction gradient 1, 0: X direction derivative calculation // Scharr (src_gray, grad_x, ddepth, 1, 0, scale, Delta, border_default); Sobel (src_gray, grad_x, ddepth, 1, 0, 3, scale, Delta, border_default); convertscaleabs (grad_x, abs_grad_x); // gradient y direction gradient 0, 1: derivative in Y direction: // Scharr (src_gray, grad_y, ddepth, 0, 1, scale, Delta, border_default); Sobel (src_gray, grad_y, ddepth, 0, 1, 3, scale, Delta, border_default); convertscaleabs (grad_y, abs_grad_y); // approximate overall gradient addweighted (abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad); imshow (window_name, GRAD); waitkey (0); Return 0 ;}