封闭像素轮廓的周长筛选算法
Contour为保存像素轮廓的结构体,函数只适用于封闭的像素轮廓,对于不封闭的像素轮廓,会得到不正确的结果。contourLengthMark函数中,_rContour为输入的像素轮廓,_MinLength为周长下限,_MaxLength为周长上限,返回值为筛选过后的轮廓空间点。
typedef struct Contours
{
int height;
int width;
int stateFlag;
} contour;
contour* contourLengthMark( contour* _rContours, const int _MinLength, const int _MaxLength )
{
int i = 0, j = 0;//轮廓点遍历计数
int point_Count_Now = 0;//当前轮廓点总数
int length_Count = 0;//轮廓周长计数
int length_Temp = 0;//临时长度变量
contour* contour_New_Ptr = NULL;//返回的新轮廓空间点首地址
//对于轮廓长度不符合条件的,轮廓空间点标志位置“3”
point_Count_Now = _rContours[0].stateFlag;//通过第一个点的状态位记录轮廓空间点数
_rContours[0].stateFlag = 1;//将第一个点的状态位还原为“1”
while ( _rContours[i].height > 0 )
{
length_Count = 0;
if ( 0 != _rContours[ i + length_Count ].stateFlag )
{
length_Count ++;
while ( _rContours[ i + length_Count ].stateFlag == 0 )
{
length_Count ++;
}
length_Temp = length_Count + 1;
if ( length_Temp < _MinLength || length_Temp > _MaxLength )
{
while ( length_Count )
{
_rContours[ i + length_Count ].stateFlag = 3;
length_Count --;
}
_rContours[i].stateFlag = 3;
point_Count_Now -= length_Temp;
}
}
i = i + length_Temp;
}
contour_New_Ptr = ( contour *)malloc( point_Count_Now * sizeof(contour) );//新轮廓空间
i = 0; j = 0;
while ( _rContours[i].height > 0 )
{
if ( 3 != _rContours[i].stateFlag )
{
contour_New_Ptr[j].height = _rContours[i].height;
contour_New_Ptr[j].width = _rContours[i].width;
contour_New_Ptr[j].stateFlag = _rContours[i].stateFlag;
j ++;
}
i ++;
}
contour_New_Ptr[0].stateFlag = point_Count_Now;//将现轮廓点数量保存在第一个轮廓空间点的状态位
free(_rContours);
return contour_New_Ptr;
}