Tutorial Sobel Derivatives
This commit is contained in:
@@ -30,6 +30,7 @@ int main( int argc, char** argv )
|
||||
cout << "\nPress 'ESC' to exit program.\nPress 'R' to reset values ( ksize will be -1 equal to Scharr function )";
|
||||
|
||||
//![variables]
|
||||
// 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";
|
||||
@@ -40,11 +41,14 @@ int main( int argc, char** argv )
|
||||
//![variables]
|
||||
|
||||
//![load]
|
||||
String imageName = parser.get<String>("@input"); // by default
|
||||
String imageName = parser.get<String>("@input");
|
||||
// As usual we load our source image (src)
|
||||
image = imread( 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 1;
|
||||
}
|
||||
//![load]
|
||||
@@ -52,10 +56,12 @@ int main( int argc, char** argv )
|
||||
for (;;)
|
||||
{
|
||||
//![reduce_noise]
|
||||
// Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
|
||||
//![reduce_noise]
|
||||
|
||||
//![convert_to_gray]
|
||||
// Convert the image to grayscale
|
||||
cvtColor(src, src_gray, COLOR_BGR2GRAY);
|
||||
//![convert_to_gray]
|
||||
|
||||
@@ -72,6 +78,7 @@ int main( int argc, char** argv )
|
||||
//![sobel]
|
||||
|
||||
//![convert]
|
||||
// converting back to CV_8U
|
||||
convertScaleAbs(grad_x, abs_grad_x);
|
||||
convertScaleAbs(grad_y, abs_grad_y);
|
||||
//![convert]
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @file SobelDemo.java
|
||||
* @brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
|
||||
*/
|
||||
|
||||
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) {
|
||||
|
||||
//! [declare_variables]
|
||||
// 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 scale = 1;
|
||||
int delta = 0;
|
||||
int ddepth = CvType.CV_16S;
|
||||
//! [declare_variables]
|
||||
|
||||
//! [load]
|
||||
// As usual we load our source image (src)
|
||||
// Check number of arguments
|
||||
if (args.length == 0){
|
||||
System.out.println("Not enough parameters!");
|
||||
System.out.println("Program Arguments: [image_path]");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
// Load the image
|
||||
src = Imgcodecs.imread(args[0]);
|
||||
|
||||
// Check if image is loaded fine
|
||||
if( src.empty() ) {
|
||||
System.out.println("Error opening image: " + args[0]);
|
||||
System.exit(-1);
|
||||
}
|
||||
//! [load]
|
||||
|
||||
//! [reduce_noise]
|
||||
// Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
Imgproc.GaussianBlur( src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT );
|
||||
//! [reduce_noise]
|
||||
|
||||
//! [convert_to_gray]
|
||||
// Convert the image to grayscale
|
||||
Imgproc.cvtColor( src, src_gray, Imgproc.COLOR_RGB2GRAY );
|
||||
//! [convert_to_gray]
|
||||
|
||||
//! [sobel]
|
||||
/// Generate grad_x and grad_y
|
||||
Mat grad_x = new Mat(), grad_y = new Mat();
|
||||
Mat abs_grad_x = new Mat(), abs_grad_y = new Mat();
|
||||
|
||||
/// Gradient X
|
||||
//Imgproc.Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, Core.BORDER_DEFAULT );
|
||||
Imgproc.Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, Core.BORDER_DEFAULT );
|
||||
|
||||
/// Gradient Y
|
||||
//Imgproc.Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, Core.BORDER_DEFAULT );
|
||||
Imgproc.Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, Core.BORDER_DEFAULT );
|
||||
//! [sobel]
|
||||
|
||||
//![convert]
|
||||
// converting back to CV_8U
|
||||
Core.convertScaleAbs( grad_x, abs_grad_x );
|
||||
Core.convertScaleAbs( grad_y, abs_grad_y );
|
||||
//![convert]
|
||||
|
||||
//! [add_weighted]
|
||||
/// Total Gradient (approximate)
|
||||
Core.addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
|
||||
//! [add_weighted]
|
||||
|
||||
//! [display]
|
||||
HighGui.imshow( window_name, grad );
|
||||
HighGui.waitKey(0);
|
||||
//! [display]
|
||||
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
public class SobelDemo {
|
||||
public static void main(String[] args) {
|
||||
// Load the native library.
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
new SobelDemoRun().run(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
@file sobel_demo.py
|
||||
@brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
|
||||
"""
|
||||
import sys
|
||||
import cv2
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [variables]
|
||||
# First we declare the variables we are going to use
|
||||
window_name = ('Sobel Demo - Simple Edge Detector')
|
||||
scale = 1
|
||||
delta = 0
|
||||
ddepth = cv2.CV_16S
|
||||
## [variables]
|
||||
|
||||
## [load]
|
||||
# As usual we load our source image (src)
|
||||
# Check number of arguments
|
||||
if len(argv) < 1:
|
||||
print ('Not enough parameters')
|
||||
print ('Usage:\nmorph_lines_detection.py < path_to_image >')
|
||||
return -1
|
||||
|
||||
# Load the image
|
||||
src = cv2.imread(argv[0], cv2.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image: ' + argv[0])
|
||||
return -1
|
||||
## [load]
|
||||
|
||||
## [reduce_noise]
|
||||
# Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
src = cv2.GaussianBlur(src, (3, 3), 0)
|
||||
## [reduce_noise]
|
||||
|
||||
## [convert_to_gray]
|
||||
# Convert the image to grayscale
|
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
## [convert_to_gray]
|
||||
|
||||
## [sobel]
|
||||
# Gradient-X
|
||||
# grad_x = cv2.Scharr(gray,ddepth,1,0)
|
||||
grad_x = cv2.Sobel(gray, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
|
||||
|
||||
# Gradient-Y
|
||||
# grad_y = cv2.Scharr(gray,ddepth,0,1)
|
||||
grad_y = cv2.Sobel(gray, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
|
||||
## [sobel]
|
||||
|
||||
## [convert]
|
||||
# converting back to uint8
|
||||
abs_grad_x = cv2.convertScaleAbs(grad_x)
|
||||
abs_grad_y = cv2.convertScaleAbs(grad_y)
|
||||
## [convert]
|
||||
|
||||
## [blend]
|
||||
## Total Gradient (approximate)
|
||||
grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
|
||||
## [blend]
|
||||
|
||||
## [display]
|
||||
cv2.imshow(window_name, grad)
|
||||
cv2.waitKey(0)
|
||||
## [display]
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
Reference in New Issue
Block a user