Загрузка
Prev Tutorial: Adding borders to your images
Next Tutorial: Laplace Operator
| Original author | Ana Huamán |
| Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn how to:
You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to express changes is by using derivatives. A high change in gradient indicates a major change in the image.
Assuming that the image to be operated is \(I\):
We calculate two derivatives:
You can also download it from here
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
cv::CommandLineParser parser(argc, argv,
"{@input |lena.jpg|input image}"
"{ksize k|1|ksize (hit 'K' to increase its value at run time)}"
"{scale s|1|scale (hit 'S' to increase its value at run time)}"
"{delta d|0|delta (hit 'D' to increase its value at run time)}"
"{help h|false|show help message}");
cout << "The sample uses Sobel or Scharr OpenCV functions for edge detection\n\n";
parser.printMessage();
<< ;
,, ;
;
= ;
= .<>();
= .<>();
= .<>();
= ;
= .<>();
= ( ::( ), );
( .() )
{
(, .());
;
}
(;;)
{
(, , (, ), , , );
(, , );
, ;
, ;
(, , , , , , , , );
(, , , , , , , , );
(, );
(, );
(, , , , , );
(, );
= ()();
( == )
{
;
}
( == || == )
{
= < ? + : -;
}
( == || == )
{
++;
}
( == || == )
{
++;
}
( == || == )
{
= ;
= -;
= ;
}
}
;
}You can also download it from here
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
class SobelDemoRun {
public void run(String[] args) {
// First we declare the variables we are going to use
Mat src, src_gray = new Mat();
Mat grad = new Mat();
String window_name = "Sobel Demo - Simple Edge Detector";
int = ;
= ;
= .;
(. == ){
..();
..();
.(-);
}
= .([]);
( .() ) {
..( + []);
.(-);
}
.( , , (, ), , , . );
.( , , . );
= (), = ();
= (), = ();
.( , , , , , , , , . );
.( , , , , , , , , . );
.( , );
.( , );
.( , , , , , );
.( , );
.();
.();
}
}
{
([] ) {
.(.);
().();
}
}You can also download it from here
"""
@file sobel_demo.py
@brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
"""
import sys
import cv2 as cv
def main(argv):
window_name = ('Sobel Demo - Simple Edge Detector')
scale = 1
delta = 0
ddepth = cv.CV_16S
if len(argv) < 1:
print ('Not enough parameters')
print ('Usage:\nmorph_lines_detection.py < path_to_image >')
return -1
# Load the image
src = cv.imread(argv[0], .)
#
:
( + [])
-
= .(, (, ), )
= .(, .)
= .(, , , , =, =, =, =.)
# -
# = .(,,,)
= .(, , , , =, =, =, =.)
= .()
= .()
= .(, , , , )
.(, )
.()
== :
(.[:]) // First we declare the variables we are going to use
Mat image,src, src_gray;
Mat grad;
const String window_name = "Sobel Demo - Simple Edge Detector";
int ksize = parser.get<int>("ksize");
int scale = parser.get<int>("scale");
int delta = parser.get<int>("delta");
int ddepth = CV_16S; String imageName = parser.get<String>("@input");
// As usual we load our source image (src)
image = imread( samples::findFile( imageName ), IMREAD_COLOR ); // Load an image
// Check if image is loaded fine
if( image.empty() )
{
printf("Error opening image: %s\n", imageName.c_str());
return EXIT_FAILURE;
} // Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT); // Convert the image to grayscale
cvtColor(src, src_gray, COLOR_BGR2GRAY); Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);We calculate the "derivatives" in x and y directions. For this, we use the function Sobel() as shown below: The function takes the following arguments:
Notice that to calculate the gradient in x direction we use: \(x_{order}= 1\) and \(y_{order} = 0\). We do analogously for the y direction.
// converting back to CV_8U
convertScaleAbs(grad_x, abs_grad_x);
convertScaleAbs(grad_y, abs_grad_y); addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);We try to approximate the gradient by adding both directional gradients (note that this is not an exact calculation at all! but it is good for our purposes).
imshow(window_name, grad);
char key = (char)waitKey(0);
At each point of the image we calculate an approximation of the gradient in that point by combining both results above:
Although sometimes the following simpler equation is used: