ido*_*w09 2 matlab image-processing morphological-analysis
我有兴趣为我的整个二进制图像找到坐标(X,Y),而不是每个组件的CoM分别找到.我怎样才能有效地做到这一点?我想使用regionprops,但找不到正确的方法.
您可以将所有区域定义为单个区域 regionprops
props = regionprops( double( BW ), 'Centroid' );
Run Code Online (Sandbox Code Playgroud)
根据数据类型BW
regionprops
决定是否应将每个连接的组件标记为不同的区域,或者将所有非零视为具有多个组件的单个区域.
或者,您可以自己计算质心
[y x] = find( BW );
cent = [mean(x) mean(y)];
Run Code Online (Sandbox Code Playgroud)
只需迭代所有像素,计算其X和Y坐标的平均值
void centerOfMass (int[][] image, int imageWidth, int imageHeight)
{
int SumX = 0;
int SumY = 0;
int num = 0;
for (int i=0; i<imageWidth; i++)
{
for (int j=0; j<imageHeight; j++)
{
if (image[i][j] == WHITE)
{
SumX = SumX + i;
SumY = SumY + j;
num = num+1;
}
}
}
SumX = SumX / num;
SumY = SumY / num;
// The coordinate (SumX,SumY) is the center of the image mass
}
Run Code Online (Sandbox Code Playgroud)
将此方法扩展到范围为[0..255]的灰度图像:而不是
if (image[i][j] == WHITE)
{
SumX = SumX + i;
SumY = SumY + j;
num = num+1;
}
Run Code Online (Sandbox Code Playgroud)
使用以下计算
SumX = SumX + i*image[i][j];
SumY = SumY + j*image[i][j];
num = num+image[i][j];
Run Code Online (Sandbox Code Playgroud)
在这种情况下,值为100的像素具有比值为1的暗像素高100倍的权重,因此暗像素对质心计算贡献相当小的分数.请注意,在这种情况下,如果您的图像很大,您可能会遇到32位整数溢出,因此在这种情况下使用long int
sumX,sumY变量而不是int
.