OpenCV里提取目标轮廓的函数是findContours,它的输入图像是一幅二值图像,输出的是每一个连通区域的轮廓点的集合:vector<vector>。
外层vector的size代表了图像中轮廓的个数,里面vector的size代表了轮廓上点的个数。
hiararchy参数和轮廓个数相同,每个轮廓contours[ i ]对应4个hierarchy元素hierarchy[ i ][ 0 ] ~hierarchy[ i ][ 3 ],分别表示后一个轮廓、前一个轮廓、父轮廓、内嵌轮廓的索引编号,如果没有对应项,该值设置为负数。
第六个参数传入CV_CHAIN_CODE时,要设置成sizeof(CvChain),其它情况统一设置成sizeof(CvContour)
CV_CHAIN_CODE:以Freeman链码的方式输出轮廓,所有其他方法输出多边形(顶点的序列)。
CV_CHAIN_APPROX_SIMPLE:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分。例如一个矩形轮廓只需4个点来保存轮廓信息
CV_RETR_EXTERNAL:只检索最外面的轮廓;
CV_RETR_LIST:检测的轮廓不建立等级关系,检索所有的轮廓,并将其保存到一条链表当中;
CV_RETR_CCOMP:建立两个等级的轮廓,上面的一层为外边界,里面的一层为内孔的边界信息。检索所有的轮廓,并将他们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界;
CV_RETR_TREE:检索所有的轮廓,并重构嵌套轮廓的整个层次,可以参见下图。
加滚动条确定阈值化的合适阈值!:http:
****************************************************************************************
var kernal = Cv.CreateStructuringElementEx(5, 2, 1, 1, ElementShape.Rect);
Cv.Erode(gray, gray, kernal, 2);
Cv.Threshold(gray, gray, 0, 255, ThresholdType.BinaryInv | ThresholdType.Otsu);
var storage = Cv.CreateMemStorage();
CvSeq contour = null;
Cv.FindContours(gray, storage, out contour, CvContour.SizeOf, ContourRetrieval.CComp, ContourChain.ApproxSimple);
var color = new CvScalar(0, 0, 255);
while (contour != null)
{
var rect = Cv.BoundingRect(contour);
if(rect.Height > 10 && (rect.Width * 1.0 / rect.Height) > 0.2)
Cv.DrawRect(src, rect, color);
contour = contour.HNext;
}
***************************************************************
***********************************************************************************************************
void getSizeContours(vector<vector> &contours)
{
int cmin = 100;
int cmax = 1000;
vector<vector>::const_iterator itc = contours.begin();
while(itc != contours.end())
{
if((itc->size()) < cmin || (itc->size()) > cmax)
{
itc = contours.erase(itc);
}
else ++ itc;
}
}
****************************************************************************
while(contour) {
area = fabs(cvContourArea( contour, CV_WHOLE_SEQ ));
printf("area == %lf\n", area);
CvRect r = ((CvContour*)contour)->rect;
if (r.height * r.width > size)
{
cvRectangle(pimg, cvPoint(r.x, r.y), cvPoint(r.x + r.width, r.y + r.height),CV_RGB(255, 0, 0), 1, CV_AA, 0);
}
contour = contour->h_next;
}
*********************************************************************************************************
std::vector<std::vector> contours;
cv::findContours(gray,
contours,
CV_RETR_EXTERNAL ,
CV_CHAIN_APPROX_NONE);
std::cout << "Contours: " << contours.size() << std::endl;
std::vector<std::vector>::const_iterator itContours= contours.begin();
for ( ; itContours!=contours.end(); ++itContours)
{
std::cout << "Size: " << itContours->size() << std::endl;
}
cv::Mat result(image.size(),CV_8U,cv::Scalar(255));
cv::drawContours(result,contours,
-1,
cv::Scalar(0),
2);
************************************************************************
double maxarea = 0;
double minarea = 100;
int m = 0;
for( ; contour != 0; contour = contour->h_next )
{
double tmparea = fabs(cvContourArea(contour));
if(tmparea < minarea)
{
cvSeqRemove(contour, 0);
continue;
}
CvRect aRect = cvBoundingRect( contour, 0 );
if ((aRect.width/aRect.height)<1)
{
cvSeqRemove(contour, 0);
continue;
}
if(tmparea > maxarea)
{
maxarea = tmparea;
}
m++;
CvScalar color = CV_RGB( 0, 255, 255 );
cvDrawContours(dst, contour, color, color, -1, 1, 8);
}
contour = _contour;
int count = 0;
for(; contour != 0; contour = contour->h_next)
{
count++;
double tmparea = fabs(cvContourArea(contour));
if (tmparea == maxarea)
{
CvScalar color = CV_RGB( 255, 0, 0);
cvDrawContours(dst, contour, color, color, -1, 1, 8);
}
}
*************************************************************************************
在提取之前还可以调用一个函数:
contour = cvApproxPoly( contour, sizeof(CvContour), storage, CV_POLY_APPROX_DP, 3, 1 );
可能是拟合,有这一句找出的轮廓线更直。 contour里面包含了很多个轮廓,每个轮廓是单独存放的.
输出轮廓位置!
printf(" %d elements:\n", c->total );
for( int i=0; itotal; ++i ) {
CvPoint* p = CV_GET_SEQ_ELEM( CvPoint, c, i );
printf(" (%d,%d)\n", p->x, p->y );
}
输出轮廓面积!
for( ; contour; contour = contour->h_next)
{
area = fabs(cvContourArea(contour, CV_WHOLE_SEQ));
printf("area == %lf\n", area);
if(area > maxArea)
{
contmax = contour;
maxArea = area;
}
}
***********************************************************************************************
内轮廓填充
void FillInternalContours(IplImage *pBinary, double dAreaThre)
{
double dConArea;
CvSeq *pContour = NULL;
CvSeq *pConInner = NULL;
CvMemStorage *pStorage = NULL;
if (pBinary)
{
pStorage = cvCreateMemStorage(0);
cvFindContours(pBinary, pStorage, &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)
{
for (pConInner = pContour->v_next; pConInner != NULL; pConInner = pConInner->h_next)
{
dConArea = fabs(cvContourArea(pConInner, CV_WHOLE_SEQ));
if (dConArea <= dAreaThre)
{
cvDrawContours(pBinary, pConInner, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 0, CV_FILLED, 8, cvPoint(0, 0));
}
}
}
cvReleaseMemStorage(&pStorage);
pStorage = NULL;
}
}
******************************************************************* *********************
std::vector<std::vector> contours;
cv::findContours(image,
contours,
CV_RETR_EXTERNAL,
CV_CHAIN_APPROX_NONE);
std::cout << "Contours: " << contours.size() << std::endl;
std::vector<std::vector>::const_iterator itContours= contours.begin();
for ( ; itContours!=contours.end(); ++itContours)
{
std::cout << "Size: " << itContours->size() << std::endl;
}
***************************************************************************************
int cmin= 100;
int cmax= 1000;
std::vector<std::vector>::const_iterator itc= contours.begin();
while (itc!=contours.end()) {
if (itc->size() < cmin || itc->size() > cmax)
itc= contours.erase(itc);
else
++itc;
}
输出所有轮廓的旋转角度
CvBox2D End_Rage2D;
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq* contour = NULL;
cvFindContours( pSrcImage, storage, &contour, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
for(; contour; contour = contour->h_next)
{
End_Rage2D = cvMinAreaRect2(contour);
std::cout <<" angle:\n"<<(float)End_Rage2D.angle << std::endl;
}
void DrawRec(IplImage* pImgFrame,IplImage* pImgProcessed,int MaxArea)
{
stor = cvCreateMemStorage(0);
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
cvFindContours( pImgProcessed, stor, &cont, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
for(;cont;cont = cont->h_next)
{
CvRect r = ((CvContour*)cont)->rect;
if(r.height * r.width > MaxArea)
{
cvRectangle( pImgFrame, cvPoint(r.x,r.y),
cvPoint(r.x + r.width, r.y + r.height),
CV_RGB(255,0,0), 1, CV_AA,0);
}
}
cvShowImage("video", pImgFrame);
}
***********************************************************************************************
绍opencv 的基于面积区域过滤方法,这个对图像处理时去除小区域杂点是很有帮助的。基于区域宽度,高度等其他方式的过滤也可以根据这个方法类推。
# 图片中找到我们需要的目标 一般是最大连通区域
#获取当前轮廓面积
area = abs(cv.cvContourArea( contour ))
# 获取最大区域矩形块
aRect = cv.cvBoundingRect( contmax, 0 )
#原始区域的不加边框
#rcenter = cv.cvPoint2D32f(aRect.x + aRect.width/2.0, aRect.y + aRect.height/2.0)
*****************************************************************************************
int cmin = 100;
int cmax = 1000;
vector<vector>::const_iterator itc = contours.begin();
while (itc!=contours.end())
{
if (itc->size() < cmin || itc->size() > cmax)
itc = contours.erase(itc);
else
++itc;
}
Mat result_erase(binaryImage.size(), CV_8U, Scalar(255));
drawContours(result_erase, contours,
-1,
Scalar(0),
2);
Rect r0 = boundingRect(Mat(contours[0]));
rectangle(result_erase, r0, Scalar(128), 2);
Rect r1 = boundingRect(Mat(contours[1]));
rectangle(result_erase, r1, Scalar(128), 2);
*****************************************************************************************************************
69 medianBlur(mask, mask, 5);
70
71
72
73 morphologyEx(mask, mask, MORPH_CLOSE, getStructuringElement(MORPH_RECT, Size(5, 5)));
74 morphologyEx(mask, mask, MORPH_OPEN, getStructuringElement(MORPH_RECT, Size(5, 5)));
93 Rect rct;
94
95
96 sort(contours.begin(), contours.end(), descSort);
97
98 for (int i = 0; i < contours.size(); i++)
99 {
100
101 if (contourArea(contours[i]) < contourArea(contours[0]) / 5)
102 break;
103
104 rct = boundingRect(contours[i]);
105 rectangle(result, rct, Scalar(0, 255, 0), 2);
106
107 }
**************************************************************************************************************
Mat element(5,5,CV_8U,Scalar(1));
cvMorphologyEx(green, green, NULL, element, CV_MOP_OPEN);
cvThreshold(green, green, 0.0, 255.0, CV_THRESH_BINARY | CV_THRESH_OTSU);
一般用:findContours(image,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
int cmin=100;
int cmax=1000;
vector<std::vector >::iterator itc = contours.begin();
while(itc!=contours.end())
{
if(itc->size()size()>cmax)
itc =contours.erase(itc);
else
itc++;
}
Mat image2=imread("E:\\group.jpg");
drawContours(image2,contours,-1,Scalar(255,255,255),2);
imshow("image",image2);