opencv-邊緣檢測

來源:互聯網
上載者:User

標籤:opencv   邊緣檢測   

// 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);}

效果:

1、

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

該函數接受了以下參數:

  • src_gray: 在本例中為輸入映像,元素類型 CV_8U
  • grad_x/grad_y: 輸出映像.
  • ddepth: 輸出映像的深度,設定為 CV_16S 避免外溢。
  • x_orderx 方向求導的階數。
  • y_ordery 方向求導的階數。
  • scaledelta 和 BORDER_DEFAULT: 使用預設值

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

函數接受了以下參數:

  • src_gray: 輸入映像。
  • dst: 輸出映像
  • ddepth: 輸出映像的深度。 因為輸入映像的深度是 CV_8U ,這裡我們必須定義 ddepth = CV_16S 以避免外溢。
  • kernel_size: 內部調用的 Sobel運算元的核心大小,此例中設定為3。
  • scaledelta 和 BORDER_DEFAULT: 使用預設值。

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

輸入參數:

  • detected_edges: 原灰階映像
  • detected_edges: 輸出映像 (支援原地計算,可為輸入映像)
  • lowThreshold: 使用者通過 trackbar設定的值。
  • highThreshold: 設定為低閾值的3倍 (根據Canny演算法的推薦)
  • kernel_size: 設定為 3 (Sobel核心大小,內部使用)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.