Opencv 利用分割的图像掩膜来完善检测前景
来源:互联网 发布:网络自制剧牺牲图解 编辑:程序博客网 时间:2024/05/16 09:54
在目标检测中,经常会出现目标断开、不连续或空洞的情况,针对该钟情况,我们总是要想各种方法去填补空洞,使断开的部分连接起来。下面介绍一个函数来实现该功能:
void cvRefineForegroundMaskBySegm( CvSeq* segments, IplImage* pFrImg ){ IplImage* tmp_image = cvCreateImage(cvSize(pFrImg->width,pFrImg->height), IPL_DEPTH_8U, 1); for( ; segments; segments = ((CvSeq*)segments)->h_next ) { CvSeq seq = *segments; seq.v_next = seq.h_next = NULL; cvZero(tmp_image); cvDrawContours( tmp_image, &seq, CV_RGB(0, 0, 255), CV_RGB(0, 0, 255), 10, -1); int num1 = cvCountNonZero(tmp_image); cvAnd(tmp_image, pFrImg, tmp_image); int num2 = cvCountNonZero(tmp_image); if( num2 > num1*0.5 ) cvDrawContours( pFrImg, &seq, CV_RGB(0, 0, 255), CV_RGB(0, 0, 255), 10, -1); else cvDrawContours( pFrImg, &seq, CV_RGB(0, 0, 0), CV_RGB(0, 0, 0), 10, -1); } cvReleaseImage(&tmp_image); }
其中,segments是利用图像分割算法得到的轮廓序列,而pFrImg则是目标检测算法检测到的前景目标,这样就可以分割图像的结果来对目标检测的区域进行填充。另外,对于如何得到分割图像,也可以采用以下函数来实现:
CvSeq*cvSegmentFGMask( CvArr* _mask, int poly1Hull0, float perimScale, CvMemStorage* storage, CvPoint offset ){ CvMat mstub, *mask = cvGetMat( _mask, &mstub ); CvMemStorage* tempStorage = storage ? storage : cvCreateMemStorage(); CvSeq *contours, *c; int nContours = 0; CvContourScanner scanner; // clean up raw mask cvMorphologyEx( mask, mask, 0, 0, CV_MOP_OPEN, 1 ); cvMorphologyEx( mask, mask, 0, 0, CV_MOP_CLOSE, 1 ); // find contours around only bigger regions scanner = cvStartFindContours( mask, tempStorage, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, offset ); while( (c = cvFindNextContour( scanner )) != 0 ) { double len = cvContourPerimeter( c ); double q = (mask->rows + mask->cols)/perimScale; // calculate perimeter len threshold if( len < q ) //Get rid of blob if it's perimeter is too small cvSubstituteContour( scanner, 0 ); else //Smooth it's edges if it's large enough { CvSeq* newC; if( poly1Hull0 ) //Polygonal approximation of the segmentation newC = cvApproxPoly( c, sizeof(CvContour), tempStorage, CV_POLY_APPROX_DP, 2, 0 ); else //Convex Hull of the segmentation newC = cvConvexHull2( c, tempStorage, CV_CLOCKWISE, 1 ); cvSubstituteContour( scanner, newC ); nContours++; } } contours = cvEndFindContours( &scanner ); // paint the found regions back into the image cvZero( mask ); for( c=contours; c != 0; c = c->h_next ) cvDrawContours( mask, c, cvScalarAll(255), cvScalarAll(0), -1, CV_FILLED, 8, cvPoint(-offset.x,-offset.y)); if( tempStorage != storage ) { cvReleaseMemStorage( &tempStorage ); contours = 0; } return contours;}
_mask是一个二值图像,可以是Canny边缘检测或cvThreshold等函数得到的二值图像。
- Opencv 利用分割的图像掩膜来完善检测前景
- 利用OpenCV的Grabcut()函数实现图像的前景与背景的分割-并对Grabcut()作详细介绍
- 利用OpenCV实现图像的阈值分割
- opencv实现图像分割,分离前景和背景(1)
- opencv实现图像分割,分离前景和背景(2)
- OpenCv自带的前景检测方法
- 利用OpenCV的SimpleBlobDetector检测图像的奇异区域(斑点)
- Android平台上利用opencv进行图像的边沿检测
- Android平台上利用opencv进行图像的边沿检测
- opencv-视频图像的前景动态追踪
- 直接可以用的Python和OpenCV检测及分割图像的目标区域例子
- [opencv]利用每帧图像减去第一帧无人的图像来检测视频人数
- 利用均值漂移实现图像分割的原理和OpenCV代码
- opencv-视频处理-实时的前景检测-Vibe算法
- opencv-视频处理-实时的前景检测-Vibe算法
- opencv学习笔记(十七)利用cvPyrSegmentation()做图像分割
- opencv例程之彩色图像的分割
- opencv 的图像局部与部分分割
- oracle入库的datetime
- CentOS设置系统时间和时区
- Opencv2.X以上Mat类型与IplImage*的转换
- U盘WINPE下安装xp系统的关键
- RTOS(3)Wind 任务调度
- Opencv 利用分割的图像掩膜来完善检测前景
- 网络监视--netstat命令详解
- hdu 2199 Can you solve this equation?
- mouseover,mouseout,mouseenter,mouseleave,hover
- 再次写给我们这些浮躁的程序员
- node.js express框架文件上传路径
- springmvc学习(一) 图解SpringMVC整体流程
- jQueryDom——Select、Option
- HTTP Header 详解