如何利用opencv实现彩色图像边缘检测算法
我目前要用VC++中的openvc实现图像边缘检测算法分析,最终的效果就是能够进行图像边缘提取,有哪个高手帮帮我,教我步骤,小弟在此谢了,就当交个朋友,很紧急!谢谢,加我...
我目前要用VC++中的openvc实现 图像边缘检测算法分析,最终的效果就是能够进行图像边缘提取,有哪个高手帮帮我,教我步骤,小弟在此谢了,就当交个朋友,很紧急!谢谢,加我QQ814656461
展开
推荐于2016-02-06
展开全部
在opencv中显示边缘检测很简单,只需调用一个cvCanny函数,其使用的是Canny算法来实现对图像的边缘检测.
函数原型为:
void cvCanny( const CvArr* image,CvArr* edges,double threshold1,double threshold2, int aperture_size=3 );
第一个参数为待检测的图像,注意一点,其必须是灰度图.
第二个参数为输出的边缘图,其也是一个灰度图.
后三个参数与Canny算法直接相关,threshold1和threshold2 当中的小阈值用来控制边缘连接,大的阈值用来控制强边缘的初始分割,aperture_size算子内核大小,可以去看看Canny算法.
从彩色图到灰度图需要使用到cvCvtColor函数,其接受三个参数,第一为输入,第二为输出,第三个为转换的标识,我们这边是RGB到GRAY,使用的是CV_RGB2GRAY.
参考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, 1, 110, 3);
cvNamedWindow("Src");
cvShowImage("Src", _pIplImageIn);
cvNamedWindow("Canny");
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;
}
函数原型为:
void cvCanny( const CvArr* image,CvArr* edges,double threshold1,double threshold2, int aperture_size=3 );
第一个参数为待检测的图像,注意一点,其必须是灰度图.
第二个参数为输出的边缘图,其也是一个灰度图.
后三个参数与Canny算法直接相关,threshold1和threshold2 当中的小阈值用来控制边缘连接,大的阈值用来控制强边缘的初始分割,aperture_size算子内核大小,可以去看看Canny算法.
从彩色图到灰度图需要使用到cvCvtColor函数,其接受三个参数,第一为输入,第二为输出,第三个为转换的标识,我们这边是RGB到GRAY,使用的是CV_RGB2GRAY.
参考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, 1, 110, 3);
cvNamedWindow("Src");
cvShowImage("Src", _pIplImageIn);
cvNamedWindow("Canny");
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;
}
展开全部
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <iostream.h>
#include <stdio.h>
int main(int argc, char** argv)
{
IplImage* img;
IplImage* temp=0;
if( argc == 2 && (img=cvLoadImage(argv[1],1))!= 0)
{
IplImage* gray = cvCreateImage( cvGetSize(img),8,1);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq * pcontour=0; //提取轮廓的序列指针
cvCvtColor( img, gray, CV_BGR2GRAY); //转化为二值图像
cvThreshold(gray, gray,110,255,CV_THRESH_BINARY);//阈值化
cvSmooth(gray,gray,CV_MEDIAN,3,0,0,0);
cvErode(gray,gray,0,2); //侵蚀
cvDilate(gray,gray,0,1); //放大
cvNamedWindow( "circles1", 1 );
cvShowImage( "circles1", gray );
cvFindContours(gray,storage,&pcontour,sizeof(CvContour),CV_RETR_LIST,CV_LINK_RUNS,cvPoint(0,0));//查找轮廓
int n1=0;
for (;pcontour!=0;pcontour=pcontour->h_next)//画轮廓
{
CvRect r = ((CvContour*)pcontour)->rect;
int area=r.height * r.width;
if(area > 800&&area<6500)
{
cvRectangle(img,cvPoint(r.x,r.y),cvPoint(r.x + r.width, r.y + r.height),CV_RGB(0,0,255),1,CV_AA,0);
n1++;
//设定颜色
CvScalar color = CV_RGB(255,255,255);
//基于给定的矩形设置感兴趣区域ROI
cvSetImageROI(gray,r);
//填充
cvSet(gray,color);
//取消感兴趣区域
cvResetImageROI(gray);
}
}
cvNamedWindow( "circles3", 1 );
cvShowImage( "circles3", gray );
printf("%d",n1);
cvNamedWindow( "circles", 1 );
cvShowImage( "circles", img );
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&gray);
}
return 0;
}
#include <highgui.h>
#include <math.h>
#include <iostream.h>
#include <stdio.h>
int main(int argc, char** argv)
{
IplImage* img;
IplImage* temp=0;
if( argc == 2 && (img=cvLoadImage(argv[1],1))!= 0)
{
IplImage* gray = cvCreateImage( cvGetSize(img),8,1);
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq * pcontour=0; //提取轮廓的序列指针
cvCvtColor( img, gray, CV_BGR2GRAY); //转化为二值图像
cvThreshold(gray, gray,110,255,CV_THRESH_BINARY);//阈值化
cvSmooth(gray,gray,CV_MEDIAN,3,0,0,0);
cvErode(gray,gray,0,2); //侵蚀
cvDilate(gray,gray,0,1); //放大
cvNamedWindow( "circles1", 1 );
cvShowImage( "circles1", gray );
cvFindContours(gray,storage,&pcontour,sizeof(CvContour),CV_RETR_LIST,CV_LINK_RUNS,cvPoint(0,0));//查找轮廓
int n1=0;
for (;pcontour!=0;pcontour=pcontour->h_next)//画轮廓
{
CvRect r = ((CvContour*)pcontour)->rect;
int area=r.height * r.width;
if(area > 800&&area<6500)
{
cvRectangle(img,cvPoint(r.x,r.y),cvPoint(r.x + r.width, r.y + r.height),CV_RGB(0,0,255),1,CV_AA,0);
n1++;
//设定颜色
CvScalar color = CV_RGB(255,255,255);
//基于给定的矩形设置感兴趣区域ROI
cvSetImageROI(gray,r);
//填充
cvSet(gray,color);
//取消感兴趣区域
cvResetImageROI(gray);
}
}
cvNamedWindow( "circles3", 1 );
cvShowImage( "circles3", gray );
printf("%d",n1);
cvNamedWindow( "circles", 1 );
cvShowImage( "circles", img );
cvWaitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&gray);
}
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询