canny邊緣檢測

canny邊緣檢測


author@jason_ql
http://blog.csdn.net/lql0716


1、canny邊緣檢測代碼

  • python代碼(對圖片進行邊緣檢測)
import cv2
import numpy as np

img = cv2.imread('D:/testSource/myImg/031.jpg',0)
imgs = cv2.Canny(img,100,200)
cv2.imshow('canny', imgs)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • C++代碼(對圖片進行邊緣檢測)
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

/// 全局變量
Mat src, src_gray;
Mat dst, detected_edges;
int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";

void CannyThreshold(int, void*)
{
    /// Reduce noise with a kernel 3x3
    blur( src_gray, detected_edges, Size(3,3) );
    /// Canny detector
    Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
    dst = Scalar::all(0);
    src.copyTo( dst, detected_edges);
    imshow( window_name, dst );
}

int main( )
{
  src = imread( "/home/jason/jason2/photo/2.jpg" );
  if( !src.data )
    { return -1; }
  dst.create( src.size(), src.type() );
  cvtColor( src, src_gray, CV_BGR2GRAY );
  namedWindow( window_name, CV_WINDOW_AUTOSIZE );
  createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
  CannyThreshold(0, 0);
  waitKey(0);
  return 0;
}
  • C++代碼(讀取攝像頭進行邊緣檢測)
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

/// 全局變量
Mat src, src_gray;
Mat dst, detected_edges;
int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
char* window_name = "Edge Map";

void CannyThreshold(int, void*)
{
    /// Reduce noise with a kernel 3x3
    blur( src_gray, detected_edges, Size(3,3) );
    /// Canny detector
    Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
    dst = Scalar::all(0);
    src.copyTo( dst, detected_edges);
    imshow( window_name, dst );
}

int main( )
{
    cv::VideoCapture cap(0);
    if(!cap.isOpened()){
        return -1;
    }

    cv::namedWindow(window_name,cv::WINDOW_AUTOSIZE);

    while(1){

        char key = cv::waitKey(1);
        if(key == 'q'){
            cv::destroyWindow("Video");
            break;
        }
        cap>>src;
//      src = imread( "/home/jason/jason2/photo/2.jpg" );
      if( !src.data ){ return -1; }
      dst.create( src.size(), src.type() );
      cvtColor( src, src_gray, CV_BGR2GRAY );
      createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold );
      CannyThreshold(0, 0);
    //  cout<< "sqrt:" << sqrt(25) <<endl;  //it means 5
    //  cout<< "pow:" << pow(5,2) <<endl;  //it means 5*5
      waitKey(0);
    }
  cap.release();
  return 0;
}

2、canny的C++數據結構

C++

void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false )

3、canny的python函數

python

cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]]) → edges

4、canny調用的相關參數

  • Parameters:
    • image – single-channel 8-bit input image.圖像
    • edges – output edge map; it has the same size and type as image .原圖像canny之後的二值化圖像
    • threshold1 – first threshold for the hysteresis procedure.
    • threshold2 – second threshold for the hysteresis procedure.
    • apertureSize – aperture size for the Sobel() operator.
    • L2gradient – a flag, indicating whether a more accurate L2 norm =(dI/dx)2+(dI/dy)2 should be used to calculate the image gradient magnitude ( L2gradient=true ), or whether the default L1 norm =|dI/dx|+|dI/dy| is enough ( L2gradient=false ).


  • 相關文章

發佈了69 篇原創文章 · 獲贊 296 · 訪問量 74萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章