C/C++|图像轮廓凹陷修补

图像轮廓凹陷修补
轮廓凸外形,可以认为是一种不带限制条件的简单凹陷修补。考虑更为普遍的情况,目标对象总体上呈凸外形,而局部存在轻微凹陷。对于这类目标对象,如果存在 非常严重的凹陷,通常是由于某种干扰因素造成的部分轮廓未被检测出来所致。对于此类情况的轮廓修复,其实就是一种带条件的凹陷修补。

【C/C++|图像轮廓凹陷修补】轮廓的凹陷程度可以用凹陷深度与凹陷跨度之比来描述。当凹陷程度不小于某个给定阈值时,对轮廓进行凹陷修补。




#include "cxcore.h" #include "cv.h" #pragma comment(lib, "cxcore.lib") #pragma comment(lib, "cv.lib") // 凹陷修补 // 参数: // 1. pBinary: 输入二值图像,单通道,位深IPL_DEPTH_8U。 // 2. fConThre: 凹陷阈值,当凹陷程度小于等于fConThre时,进行修补。 void ConcavityRepair(IplImage *pBinary, float fConThre) { int i, dx, dy; float fSpan, fThreshold; CvSeq *pContour = NULL; CvSeq *pSeqHull = NULL; CvSeq *pSeqDefect = NULL; CvMemStorage *pStorage1 = NULL; CvMemStorage *pStorage2 = NULL; CvConvexityDefect *pDefect = NULL; // 执行条件 if (pBinary) { // 查找所有轮廓 pStorage1 = cvCreateMemStorage(0); pStorage2 = cvCreateMemStorage(0); cvFindContours(pBinary, pStorage1, &pContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // 填充所有轮廓 cvDrawContours(pBinary, pContour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 2, CV_FILLED, 8, cvPoint(0, 0)); // 外轮廓循环 for (; pContour != NULL; pContour = pContour->h_next) { // 轮廓的凸外形 pSeqHull = cvConvexHull2(pContour, NULL, CV_CLOCKWISE, 0); // 轮廓凸性缺陷 pSeqDefect = cvConvexityDefects(pContour, pSeqHull, pStorage2); // 凸性缺陷循环 for (i = 0; i < pSeqDefect->total; i++) { pDefect = (CvConvexityDefect*) cvGetSeqElem(pSeqDefect, i); // 凹陷跨度 dx = pDefect->start->x - pDefect->end->x; dy = pDefect->start->y - pDefect->end->y; fSpan = (float) (dx * dx + dy * dy); // 修补阈值 fThreshold = pDefect->depth * fConThre; fThreshold *= fThreshold; // 凹陷修补 if (fSpan <= fThreshold) { cvLine(pBinary, *pDefect->start, *pDefect->end, CV_RGB(255, 255, 255)); } } } cvReleaseMemStorage(&pStorage1); cvReleaseMemStorage(&pStorage2); pStorage1 = NULL; pStorage2 = NULL; } }

    推荐阅读