【OpenCV学习之Canny算法边缘检测(可调阈值)】threhold1和threshold2中的小阈值用来调控边缘连接,大阈值用来控制强边缘的初始分割。
Canny算法使用双阈值算法检测和连接边缘。
采用Canny算法进行边缘检测
#include "cv.h"
#include "highgui.h"
char wndname[] = "Edge";
char tbarname[] = "Threshold";
int edge_thresh = 1;
IplImage* image = 0, *cedge = 0, *gray = 0, *edge = 0;
//定义跟踪条的回调函数
void on_trackbar(int h) {
cvSmooth(gray, edge, CV_BLUR, 3, 3, 0);
cvNot(gray, edge);
//边缘检测
cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh * 3, 3);
//显示边缘
cvShowImage(wndname, edge);
}int main(int argc, char** argv) {
image = cvLoadImage("Lena.tiff", 1);
cedge = cvCreateImage(cvSize(image->width, image->height), 8, 3);
//将彩色图像转化为灰度图
gray = cvCreateImage(cvSize(image->width, image->height), 8, 1);
edge = cvCreateImage(cvSize(image->width, image->height), 8, 1);
cvCvtColor(image, gray, CV_BGR2GRAY);
cvNamedWindow(wndname, 1);
cvCreateTrackbar(tbarname, wndname, &edge_thresh, 100, on_trackbar);
//显示图像
on_trackbar(0);
cvWaitKey(0);
cvReleaseImage(&image);
cvReleaseImage(&gray);
cvReleaseImage(&edge);
cvDestroyWindow(wndname);
return 0;
}
推荐阅读
- #|OpenCV学习笔记-MeanShift
- vs2013中将OpenCV程序由x64转化为x86
- OpenCV学习笔记|学习opencv之(六)-图像切割,使用ROI
- OpenCV学习笔记|OpenCV学习笔记09--通过cvPtr2D或指针算法绘制图形
- 【OpenCV学习笔记】十九、图像边缘检测
- 【OpenCV学习笔记】二十一、直方图计算及绘制(一)
- Opencv2系列学习笔记7(图像形态学运算)
- opencv学习笔记|opencv4学习笔记(2)-canny边缘检测
- opencv学习笔记|opencv学习笔记(1)-阈值分割3种方法,以及相关参数调试心得