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学习笔记)