#include <iostream> #include <string> #include <sstream> #include <opencv/cv.h> #include <opencv/highgui.h> using namespace std; int String2int(const string& str_) { int _nre = 0; stringstream _ss; _ss << str_; _ss >> _nre; return _nre; } void DoCanny(const string& strFileName_) { //原彩色图片 IplImage* _pIplImageIn = cvLoadImage(strFileName_.data()); if (_pIplImageIn == NULL) { return; } //彩色图片转换成灰度图放置的图片 IplImage* _pIplImageCanny = cvCreateImage(cvGetSize(_pIplImageIn), _pIplImageIn->depth, 1); cvCvtColor(_pIplImageIn, _pIplImageCanny, CV_RGB2GRAY);//CV_RGB2GRAY将rgb图转成灰度图 //只有边缘路径的图片 IplImage* _pIplImageOut = cvCreateImage(cvGetSize(_pIplImageIn), IPL_DEPTH_8U, 1); //边缘检测只能作用于灰度图 if (_pIplImageCanny->nChannels != 1) { return; } //边缘检测操作 cvCanny(_pIplImageCanny, _pIplImageOut, 50, 200, 3); cvNamedWindow("Src",0); cvShowImage("Src", _pIplImageIn); cvNamedWindow("Canny",0); cvShowImage("Canny", _pIplImageOut); cvWaitKey(0); cvReleaseImage(&_pIplImageIn); cvReleaseImage(&_pIplImageCanny); cvReleaseImage(&_pIplImageOut); cvDestroyWindow("Src"); cvDestroyWindow("Canny"); } int main(int argc, char* argv[]) { if (argc < 2) { cout << "You should give the filename of picture!" << endl; return -1; } DoCanny(argv[1]); return 0; }
时间: 2024-10-10 10:00:06