边缘检测(Edge Detection)的源代码(需要OPENCV库的支持)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

下面是采用 CANNY 算子进行图像边缘检测的 C/C++ 源代码,在OPENCV BETA 4.0, VC6.0 环境下编译通过。关于OPENCV库的使用方法以及相关问题,请查阅下面的相关文章:

http://forum.assuredigit.com/display_topic_threads.asp?ForumID=11&TopicID=3471

=========

程序开始

=========

#ifdef _CH_

#pragma package

#endif

#ifndef _EiC

#include "cv.h"

#include "highgui.h"

#endif

char wndname[] = "Edge";

char tbarname[] = "Threshold";

int edge_thresh = 1;

IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;

// 定义跟踪条的 callback 函数

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

cvZero( cedge );

// copy edge points

cvCopy( image, cedge, edge );

// 显示图像

cvShowImage(wndname, cedge);

}

int main( int argc, char** argv )

{

char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";

if( (image = cvLoadImage( filename, 1)) == 0 )

return -1;

// Create the output image

cedge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 3);

// 将彩色图像转换为灰度图像

gray = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);

edge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);

cvCvtColor(image, gray, CV_BGR2GRAY);

// Create a window

cvNamedWindow(wndname, 1);

// create a toolbar

cvCreateTrackbar(tbarname, wndname, &edge_thresh, 100, on_trackbar);

// Show the image

on_trackbar(0);

// Wait for a key stroke; the same function arranges events processing

cvWaitKey(0);

cvReleaseImage(&image);

cvReleaseImage(&gray);

cvReleaseImage(&edge);

cvDestroyWindow(wndname);

return 0;

}

#ifdef _EiC

main(1,"edge.c");

#endif

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航