我想知道是否有比我在下面所做的更快的方法来计算分水岭图像的区域邻接矩阵。
输入:具有从 1 到 N 标记的 N 个区域的分水岭图像。
输出:这 N 个区域的邻接矩阵。
1. 对于每个区域,计算对应的掩码,并将所有掩码放入一个向量中:
vector<Mat> masks;
for(int i = 0; i < N; i++ )
{
// Create the corresponding mask
Mat mask;
compare(wshed, i+1, mask, CMP_EQ);
// Dilate to overlap the watershed line (border)
dilate(mask, mask, Mat());
// Add to the list of masks
masks.push_back(mask);
}
2.定义一个函数来检查两个区域是否相邻:
bool areAdjacent(const Mat& mask1, const Mat& mask2)
{
// Get the overlapping area of the two masks
Mat m;
bitwise_and(mask1, mask2, m);
// Compute the size of the overlapping area
int size = countNonZero(m);
// If there are more than 10 (for example) overlapping pixels, then the two regions are adjacent
return (size > 10);
}
3.计算邻接矩阵M:如果第i个区域和第j个区域相邻,则M[i][j] = M[j][i] =1,否则等于0。
Mat M = Mat::zeros(N, N, CV_8U);
for(int i = 0; i < N-1; i++)
{
for(int j = i+1; j < N; j++)
{
if(areAdjacent(masks[i], masks[j]))
{
M.at<uchar>(i,j) = 1;
M.at<uchar>(j,i) = 1;
}
}
}
return M;