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 ).
- 相关文章