Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
Check the corresponding tutorial for more details
#include <iostream>
int main(
int argc,
char** 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";
cout << "\nPress 'ESC' to exit program.\nPress 'R' to reset values ( ksize will be -1 equal to Scharr function )";
Mat image,src, src_gray;
Mat grad;
const String window_name =
"Sobel Demo - Simple Edge Detector";
int ksize = parser.
get<
int>(
"ksize");
int delta = parser.
get<
int>(
"delta");
if( image.empty() )
{
printf("Error opening image: %s\n", imageName.c_str());
return EXIT_FAILURE;
}
for (;;)
{
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
if(key == 27)
{
return EXIT_SUCCESS;
}
if (key == 'k' || key == 'K')
{
ksize = ksize < 30 ? ksize+2 : -1;
}
if (key == 's' || key == 'S')
{
}
if (key == 'd' || key == 'D')
{
delta++;
}
if (key == 'r' || key == 'R')
{
ksize = -1;
delta = 0;
}
}
return EXIT_SUCCESS;
}
Designed for command line parsing.
Definition: utility.hpp:890
T get(const String &name, bool space_delete=true) const
Access arguments by name.
Definition: utility.hpp:956
void printMessage() const
Print help message.
void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)
Scales, calculates absolute values, and converts the result to 8-bit.
void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)
Calculates the weighted sum of two arrays.
@ BORDER_DEFAULT
same as BORDER_REFLECT_101
Definition: core/include/opencv2/core/base.hpp:277
std::string String
Definition: cvstd.hpp:149
Size2i Size
Definition: modules/core/include/opencv2/core/types.hpp:370
#define CV_16S
Definition: core/include/opencv2/core/hal/interface.h:76
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
@ IMREAD_COLOR
Same as IMREAD_COLOR_BGR.
Definition: imgcodecs.hpp:72
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc/include/opencv2/imgproc.hpp:555
void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Blurs an image using a Gaussian filter.
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
Definition: core/include/opencv2/core.hpp:107