从小二进制图像中删除异常值像素

时间:2012-07-13 13:46:21

标签: algorithm computer-vision noise outliers

我目前正在实施一种算法,用于识别有色质量的最小惯性轴(由第二时刻提供)。为了做到这一点,我需要获得质量中心,正如最初时刻所给出的那样。

加权平均功能运行良好,但由于异常像素,我收到了不良结果。

这是平均功能:

(例如x的加权平均值)

for (i = 0, i < rows, i++) {
    for (j = 0, j < cols, j++) {
        if (colorAt(i,j).isForeground()) {
            tempSumX++;
            totalForeground++;
        }
    }
    x_ += i*tempSumX;
    tempSumX = 0;
}
x_ /= totalForeground; //where x_ represents the x coordinate of the weighted center of mass.

Incorrect Center of Mass

鉴于此类图像(仅由两种颜色(背景和前景)表示),如何删除外围像素?注意:外围像素指的是任何不属于大颜色质量的东西。白点是计算的质心,这是不正确的。

非常感谢。

3 个答案:

答案 0 :(得分:1)

有很多泛洪填充算法可以识别给定起点的所有连接像素。

另外,一种常见的方法是去除像这些来自噪音的小外观,这会侵蚀图像,然后扩大它以恢复到相同的大小 - 尽管如果你纯粹做CoG,你不一定需要扩张步骤< / p>

答案 1 :(得分:0)

用伪代码怎么样:

for( y = 0; y < rows; y++ )
{    
   for ( x = 0; x < cols; x++ )
   {
       if ( pixel( x, y ).isColor() )
       {
          int sum = 0;
          // forgetting about edge cases for clarity...
          if ( !pixel( x-1, y-1 ).isColor() ) sum++;
          if ( !pixel( x,   y-1 ).isColor() ) sum++;
          if ( !pixel( x+1, y-1 ).isColor() ) sum++;
          if ( !pixel( x-1, y   ).isColor() ) sum++;
          if ( !pixel( x+1, y   ).isColor() ) sum++;
          if ( !pixel( x-1, y+1 ).isColor() ) sum++;
          if ( !pixel( x,   y+1 ).isColor() ) sum++;
          if ( !pixel( x+1, y+1 ).isColor() ) sum++;
          if ( sum >= 7 )
          {
             pixel( x, y ).setBackground();
             x -= 1;
             y -= 1;
          }
       }
   }
}

即删除由7个背景像素包围的任何像素。如果您将像素的颜色更改为最早可能受影响的像素。

您对“异常值”的衡量可能会改变 - 例如您可以将对角线像素数量计算为1/2。例如。直接在上方,下方,左侧和右侧的像素计数为2.然后使用不同的数字作为总和。

您可以通过增加滤镜的大小来增加精度 - 比如5x5而不是3x3。在这种情况下,像素2距离应该算得更少。

答案 2 :(得分:0)

我相信你的算法不正确。 m10(抱歉,我无法弄清楚如何做下标)在二进制图像中是前景像素的x坐标的总和,所以你的代码应该是这样的:

for (i = 0, i < rows, i++) {
    for (j = 0, j < cols, j++) {
        if (colorAt(i,j).isForeground()) {
            tempSumX += i;
            totalForeground++;
        }
    }
}
x_ = tempSumX/totalForeground; //where x_ represents the x coordinate of the weighted center of mass.

假设这与您之前发布的帖子Algorithm for Finding Longest Stretch of a Value at any Angle in a 2D Matrix有关,您应该计算同一循环中的其他一阶和二阶矩:

m01 += j;
m20 += i*i;
m02 += j*j;
m11 += i*j;

(算法中的tempSumX只是m10,totalForeground是m00)

尝试一下,如果您仍然遇到异常值问题,可以使用连接组件标签http://en.wikipedia.org/wiki/Connected_component_labeling来查找最大质量。 (使用bwlabelbwconncomp在matlab中提供。)