Laplace Operator
Примеры с cv2.imshow требуют сборки OpenCV с GUI. В ноутбуке с headless-версией используйте вывод изображений через Matplotlib.
Laplace Operator
Prev Tutorial: Sobel Derivatives
Next Tutorial: Canny Edge Detector
| Original author | Ana Huamán |
| Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn how to:
You can observe that the second derivative is zero! So, we can also use this criterion to attempt to detect edges in an image. However, note that zeros will not only appear in edges (they can actually appear in other meaningless locations); this can be solved by applying filtering where needed.
The Laplacian operator is defined by:
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
int main( int argc, char** argv )
{
// Declare the variables we are going to use
Mat src, src_gray, dst;
int kernel_size = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
const char* window_name = "Laplace Demo";
const char* imageName = >= ? [] : ;
= ( ::( ), );
(.()){
();
();
-;
}
( , , (, ), , , );
( , , );
;
( , , , , , , );
( , );
( , );
();
;
}
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
class LaplaceDemoRun {
public void run(String[] args) {
// Declare the variables we are going to use
Mat src, src_gray = new Mat(), dst = new Mat();
int kernel_size = 3;
int = ;
= ;
= .;
= ;
= ((. > ) ? [] : );
= .(, .);
( .() ) {
..();
..();
.(-);
}
.( , , (, ), , , . );
.( , , . );
= ();
.( , , , , , , . );
.( , );
.( , );
.();
.();
}
}
{
([] ) {
.(.);
().();
}
}"""
@file laplace_demo.py
@brief Sample code showing how to detect edges using the Laplace operator
"""
import sys
import cv2 as cv
def main(argv):
# [variables]
# Declare the variables we are going to use
ddepth = cv.CV_16S
kernel_size = 3
window_name = "Laplace Demo"
# [variables]
# [load]
imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
= .(..(), .) #
#
:
()
()
-
# []
# []
#
= .(, (, ), )
# []
# []
#
= .(, .)
# []
#
.(, .)
# []
#
= .(, , =)
# []
# []
#
= .()
# []
# []
.(, )
.()
# []
== :
(.[:]) // Declare the variables we are going to use
Mat src, src_gray, dst;
int kernel_size = 3;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
const char* window_name = "Laplace Demo"; // Declare the variables we are going to use
Mat src, src_gray = new Mat(), dst = new Mat();
int kernel_size = 3;
int scale = 1;
int delta = 0;
int ddepth = CvType.CV_16S;
String window_name = "Laplace Demo"; # Declare the variables we are going to use
ddepth = cv.CV_16S
kernel_size = 3
window_name = "Laplace Demo" const char* imageName = argc >=2 ? argv[1] : "lena.jpg";
src = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
// Check if image is loaded fine
if(src.empty()){
printf(" Error opening image\n");
printf(" Program Arguments: [image_name -- default lena.jpg] \n");
return -1;
} String imageName = ((args.length > 0) ? args[0] : "../data/lena.jpg");
src = Imgcodecs.imread(imageName, Imgcodecs.IMREAD_COLOR); // Load an image
// Check if image is loaded fine
if( src.empty() ) {
System.out.println("Error opening image");
System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
System.exit(-1);
} imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR) # Load an image
# Check if image is loaded fine
if src is None:
print ('Error opening image')
print ('Program Arguments: [image_name -- default lena.jpg]')
return -1 // Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
GaussianBlur( src, src, Size(3, 3), 0, 0, BORDER_DEFAULT ); // Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
Imgproc.GaussianBlur( src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT ); # Remove noise by blurring with a Gaussian filter
src = cv.GaussianBlur(src, (3, 3), 0) cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale // Convert the image to grayscale
Imgproc.cvtColor( src, src_gray, Imgproc.COLOR_RGB2GRAY ); # Convert the image to grayscale
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT ); Imgproc.Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, Core.BORDER_DEFAULT ); # Apply Laplace function
dst = cv.Laplacian(src_gray, ddepth, ksize=kernel_size) // converting back to CV_8U
convertScaleAbs( dst, abs_dst ); // converting back to CV_8U
Core.convertScaleAbs( dst, abs_dst ); # converting back to uint8
abs_dst = cv.convertScaleAbs(dst) imshow( window_name, abs_dst );
waitKey(0); HighGui.imshow( window_name, abs_dst );
HighGui.waitKey(0); cv.imshow(window_name, abs_dst)
cv.waitKey(0)