canny边缘检测 demo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了canny边缘检测 demo相关的知识,希望对你有一定的参考价值。

#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;
}

 

以上是关于canny边缘检测 demo的主要内容,如果未能解决你的问题,请参考以下文章

应用 Canny 边缘检测后如何计算边缘数?

openCV Canny 边缘检测改进

Canny边缘检测算法的步骤和理解

Canny边缘检测算法(python 实现)

Canny边缘检测

Matlab边缘检测问题