每个白色区域的包围盒可以用来判断简单的矩形"嵌套"关系.
使用connectedComponentsWithStats
,可以获得AABB(轴对齐边界框).
(以下代码是C++.)
//Judge the rectangular nested relation
bool Is_A_Including_B( const cv::Rect &A, const cv::Rect &B, int Margin )
{
int Left = A.x - Margin;
int Right = Left + A.width + 2*Margin - 1;
int Top = A.y - Margin;
int Bottom = Top + A.height + 2*Margin - 1;
auto B_br = B.br();
return ( Left<=B.x && B_br.x<=Right && Top<=B.y && B_br.y<=Bottom );
}
//main
int main()
{
//Load Image and Binalize
cv::Mat SrcImg = cv::imread( "Regs.png", cv::IMREAD_GRAYSCALE );
if( SrcImg.empty() )return 0;
cv::Mat BinImg;
cv::threshold( SrcImg, BinImg, 128, 255, cv::THRESH_BINARY );
//Find Bounding Boxes of white regions
std::vector< cv::Rect > BBs;
{
cv::Mat Labels, Stats, Centroids;
int N = cv::connectedComponentsWithStats( BinImg, Labels, Stats, Centroids );
BBs.reserve( N-1 );
for( int i=1; i<N; ++i )
{
BBs.emplace_back(
Stats.at<int>( i, cv::CC_STAT_LEFT ),
Stats.at<int>( i, cv::CC_STAT_TOP ),
Stats.at<int>( i, cv::CC_STAT_WIDTH ),
Stats.at<int>( i, cv::CC_STAT_HEIGHT )
);
}
}
//Find nested region
std::vector< cv::Rect > Found;
for( auto i=BBs.begin(); i!=BBs.end(); ++i )
{
for( auto j=BBs.begin(); j!=BBs.end(); ++j )
{
if( i == j )continue;
if( Is_A_Including_B( *i, *j, 5 ) )
{ Found.push_back( *j ); }
}
}
//Visualize
cv::Mat ShowImg;
cv::cvtColor( BinImg, ShowImg, cv::COLOR_GRAY2BGR );
ShowImg *= 0.5;
for( const auto &Rect : Found )
{
cv::rectangle( ShowImg, Rect, cv::Scalar(0,0,255), 4 );
}
cv::imshow( "Result", ShowImg );
cv::waitKey();
return 0;
}
Result: