Opencv-Edge Detection

Source: Internet
Author: User

// ConsoleApplication3_6_23.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<opencv2/opencv.hpp>#include<iostream>using namespace std;using namespace cv;Mat src,dst,gray;int pro_type = 0;char* windowName = "demo";char* windowName1 = "demo_pro";void Image_pro(int,void*);int _tmain(int argc, _TCHAR* argv[]){src = imread("test.png");if(!src.data)return -1;namedWindow(windowName,CV_WINDOW_AUTOSIZE);imshow(windowName,src);GaussianBlur(src,src,Size(3,3),0,0,BORDER_DEFAULT);cvtColor(src,gray,CV_RGB2GRAY);namedWindow(windowName1,CV_WINDOW_AUTOSIZE);createTrackbar("Type : 0-sobel 1-laplace 2-canny /n",windowName1,&pro_type,2,Image_pro);Image_pro(0,0);waitKey(0);return 0;}void Image_pro(int,void*){Mat grd_x,grd_y;Mat abs_grd_x,abs_grd_y;Mat la_dst;switch (pro_type){case 0 :Sobel(gray,grd_x,CV_16S,1,0,3,1,0,BORDER_DEFAULT);convertScaleAbs(grd_x,abs_grd_x);Sobel(gray,grd_y,CV_16S,0,1,3,1,0,BORDER_DEFAULT);convertScaleAbs(grd_y,abs_grd_y);addWeighted(abs_grd_x,0.5,abs_grd_y,0.5,0,dst);break;case 1:Laplacian(gray,la_dst,CV_16S,3,1,0,BORDER_DEFAULT);convertScaleAbs(la_dst,dst);break;case 2:Canny(gray,dst,20,50 * 3,3);break;default:break;}imshow(windowName1,dst);}

Effect:

1,

Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );

This function accepts the following parameters:

  • Src_gray: Input image in this example, element typeCv_8u
  • Grad_x/Grad_y: Output image.
  • Ddepth: Depth of the output image, setCv_16sAvoid overflow.
  • X_order:XThe order of the direction.
  • Y_order:YThe order of the direction.
  • Scale,DeltaAndBorder_default: Use the default value

2,
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );

The function accepts the following parameters:

  • Src_gray: Input image.
  • DST: Output image
  • Ddepth: Depth of the output image. Because the input image depth isCv_8u, Which must be defined hereDdepth=Cv_16sTo avoid overflow.
  • Kernel_size: The kernel size of the Internally called Sobel operator, which is set to 3 in this example.
  • Scale,DeltaAndBorder_default: Use the default value.

Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );

Input parameters:

  • Detected_edges: Original grayscale image
  • Detected_edges: Output image (supports in-situ computation and can be input images)
  • Lowthreshold: The value set by the user through trackbar.
  • Highthreshold: Set to 3 times the low threshold value (recommended based on the Canny algorithm)
  • Kernel_size: Set to 3 (Sobel kernel size, used internally)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.