This series of learning notes is referenced from the OpenCV2.4.10Opencv\sources\samples\cpp\tutorial_code and http://www.opencv.org.cn/opencvdoc/2.3.2/html/genindex.html
in image processing, it is often necessary to extract an effective edge to the image. This post will cover Laplace edge detection and Sobel edge detection, as well as image remapping.
1.laplace_demo.cpp (Laplace edge detection )Demo source and comments are as follows:
#include "stdafx.h" //Precompiled Header File /**laplace Transform detect edge */#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/ Highgui/highgui.hpp "#include <stdlib.h> #include <stdio.h>using namespace cv;/** * Main function */int main (int, char* * argv) { 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"; Load image src = imread ("D:\\opencv\\lena.png"); if (!src.data) {return-1;} The Gaussian filter is then blurred to remove the noise gaussianblur (src, src, Size (3,3), 0, 0, border_default); Convert the image to grayscale cvtcolor (src, Src_gray, color_rgb2gray); Create Window Namedwindow (window_name, window_autosize); Mat ABS_DST; Laplace transform Laplacian (Src_gray, DST, ddepth, kernel_size, scale, Delta, Border_default); Convertscaleabs (DST, ABS_DST); Display imshow (window_name, ABS_DST); Waitkey (0); return 0;}
Run:
FunctionLaplacian used to calculate the imageLaplace edges, function prototypes are:C + +: voidLaplacian(Inputarraysrc, OutputarrayDST, intddepth, intksize=1, double Scale=1, doubleDelta=0, intBordertype=border_defaultThe parameter src is a grayscale, DST is the same as thesrc size, the same type of Laplace edge detection image,ddepth used to describe the image bit depth,ksize Used to calculate the aperture size of a second derivative filter with an odd value. Scale is an optional scaling factor for calculating the Laplace value. The delta is an optional constant value superimposed on DST.
2.sobel_demo.cpp(Sobel edge detection )
Demo source and comments are as follows:
#include "stdafx.h"//Precompiled Header File/**sobel Edge detection demo */#include "opencv2/imgproc/imgproc.hpp" #include "Opencv2/highgui/high Gui.hpp "#include <stdlib.h> #include <stdio.h>using namespace cv;/** * Main function */int main (int, char** argv) {Mat SRC, Src_gray; Mat grad; Const char* window_name = "Sobel demo-simple Edge Detector"; int scale = 1; int delta = 0; int ddepth = cv_16s; Load image src = imread ("D:\\opencv\\lena.png"); if (!src.data) {return-1;} Gaussianblur (SRC, src, Size (3,3), 0, 0, Border_default); Convert to Grayscale figure Cvtcolor (SRC, Src_gray, color_rgb2gray); Create Window Namedwindow (Window_name, window_autosize); Mat grad_x, grad_y; Mat abs_grad_x, abs_grad_y; Gradient Edge X-direction Sobel (Src_gray, grad_x, ddepth, 1, 0, 3, scale, Delta, Border_default); Convertscaleabs (grad_x, abs_grad_x); Gradient edge in Y-direction Sobel (Src_gray, grad_y, ddepth, 0, 1, 3, scale, Delta, Border_default); Convertscaleabs (grad_y, abs_grad_y); Image Overall Edge addweighted (abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad); Imshow (Window_name, grad); Waitkey (0); return 0;}
Run:
The function of the Sobel function is to compute the edge of an image by Sobel operator whose function is declared as:C + +: voidSobel(Inputarraysrc, OutputarrayDST, intddepth, intXorder, intYorder, intksize=3, double Scale=1, doubleDelta=0, intBordertype=border_default)the first parameter, SRC, is the input image, DST is the output image, Ddepth is the output image bit depth, xorder and Yorder are the order of the derivative of the x direction and the y direction respectively, Ksize is the size of the Sobel operator, which must be an odd number.
3.remap_demo.cpp (image remapping)
Demo source and comments are as follows:
#include "stdafx.h"//Precompiled Header File/** image remapping Demo */#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc. HPP "#include <iostream> #include <stdio.h>using namespace cv;///global variable mat src, DST; Mat map_x, map_y;const char* remap_window = "Remap demo"; int ind = 0;///function declares void update_map (void);/** * Main function */int Main ( int, char** argv) {///load picture src = imread ("D:\\opencv\\lena.png", 1); Created in SRC image size child dst,map_x,map_y dst.create (Src.size (), Src.type ()); Map_x.create (Src.size (), CV_32FC1); Map_y.create (Src.size (), CV_32FC1); Create Window Namedwindow (Remap_window, window_autosize); loop for (;;) {int c = waitkey (1000); if ((char) c = =) {break;} Remap Update_map (); Remap (src, DST, map_x, map_y, Cv_inter_linear, Border_constant, Scalar (0, 0, 0)); Display Results imshow (Remap_window, DST); } return 0;} /** * Four types of remap */void update_map (void) {ind = ind%4; for (int j = 0; J < Src.rows; J + +) {for (int i = 0; i < src.cols i++) {switch (IND) {case 0://height is reduced by half and displayed in the middle if (I > src.cols*0.25 && ; I < src.cols*0.75 && J > src.rows*0.25 && J < src.rows*0.75) {map _x.at<float> (j,i) = (i-src.cols*0.25f) + 0.5f; Map_y.at<float> (j,i) = (j-src.rows*0.25f) + 0.5f; } else {map_x.at<float> (j,i) = 0; Map_y.at<float> (J,i) = 0; } break; Case 1://Image Upside down map_x.at<float> (j,i) = (float) i; Map_y.at<float> (j,i) = (float) (SRC.ROWS-J); Break Case 2://image left and right map_x.at<float> (j,i) = (float) (src.cols-i); Map_y.at<float> (j,i) = (float) j; Break Case 3://image upside-down and left-right upside-down map_x.at<float> (j,i) = (float) (src.cols-i); Map_Y.at<float> (j,i) = (float) (SRC.ROWS-J); Break }}} ind++;}
Run as follows:
the function of remap is to implement image remapping. The mapping relationship is:
The function prototypes are:C + +: voidRemap(Inputarraysrc, OutputarrayDST, InputarrayMap1, InputarrayMAP2, intinterpolation, intBordermode=border_constant, const scalar&Bordervalue=scalar ())where SRC is the source image, DST is the destination image. MAP1 is the mapping relationship in the x direction, and MAP2 is the mapping relationship in the Y direction. interpolation is an interpolation method.
OpenCV2.4.10 Samples_cpp_tutorial-code_learn-----Imgtrans (Laplace edge detection and Sobel edge detection, image remapping)