In opencv, the scharr filter exists in conjunction with the operation of the sobel operator. When the sobel kernel is 3, the result may produce more obvious errors. In response to this problem, Opencv provides the scharr function. This function only targets kernels of size 3, and the calculation rate is as fast as the sobel function, and the result is more accurate, but the noise resistance is not as good as the sobel function.
Use the scharr filter to calculate the image difference in the x or y direction, and its parameter variables are the same as sobel.
Function: Imgproc.Scharr(Mat src, Mat dst, int ddepth, int dx, int dy, double scale, double delta, int borderType)
Parameter description:
src: source image
dst: Detection result image
ddepth: The depth of the output image
dx: The difference order in the x direction
dy: The difference order in the y direction
scale: scaling factor
delta: The delta value optional before the result is stored in the output image, default is 0
borderType: boundary mode, default BORDER_DEFAULT
Sample code:
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat src = Imgcodecs.imread("F://2011031213205880528.jpg"); Mat dst = src.clone(); Mat dstx = src.clone(); Mat dsty = src.clone(); Imgproc.GaussianBlur(src, dst, new Size(3, 3), 0); Imgproc.cvtColor(dst, dst, Imgproc.COLOR_RGB2GRAY); Imgproc.Scharr(dst, dstx, -1, 1, 0, 1, 0, Core.BORDER_DEFAULT); Imgcodecs.imwrite("F://dstx.jpg", dstx); Imgproc.Scharr(dst, dsty, -1, 0, 1, 1, 0, Core.BORDER_DEFAULT); Imgcodecs.imwrite("F://dsty.jpg", dsty); Core.addWeighted(dstx, 0.5, dsty, 0.5, 0, dst); Imgcodecs.imwrite("F://dst.jpg", dst); } Source image:
Scharr in the X direction:
Scharr in Y direction:
After merging the gradients:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.