【问题标题】:Erode/Dilatation for binary and gray value images二值和灰度值图像的腐蚀/膨胀
【发布时间】:2012-12-30 14:43:48
【问题描述】:

我们必须首先实现 Erode 算法,该算法必须适用于二进制和灰度图像。

这是我们的代码:不完整

#include <iostream>
#include <limits>
#include "ErosionFilter.h"

/* Constructor. */
ErosionFilter::ErosionFilter()
{
 // Set structure element to NULL.
 m_StructureElement = NULL;
}

/* Destructor. */
ErosionFilter::~ErosionFilter()
{
 // Nothing to do here.
}

/* Set the structure element for the filter. */
void ErosionFilter::SetStructureElement(  StructureElement* structureElement )
{
  m_StructureElement = structureElement;
}

/* Execute the Erosion filter. */
bool ErosionFilter::Execute()
{
 // Check if structure element is set.
 if( m_StructureElement == NULL )
 {
  std::cout << "Error: No structure element set!" << std::endl;
  return false;
 }

// First, create a valid output image.
// This fails, if no valid input image is available.
if( !CreateOutputImage() )
{
 return false;
}



// We define few constants required for the filtering. It is more efficient to
// use constants instead of calling the member functions in each iteration.
const int kernelHalfSizeX = m_StructureElement->GetHalfSizeX();
const int kernelHalfSizeY = m_StructureElement->GetHalfSizeY();

// We cast the size to integer to avoid signed/unsigned warnings in the boundary checking.
const int imageSizeX = static_cast<int> ( m_InputImage->GetSizeX() );
const int imageSizeY = static_cast<int> ( m_InputImage->GetSizeY() );

// Iterate over all pixel coordinates.
for( int y = 0; y < imageSizeY; y++ )
{
 for( int x = 0; x < imageSizeX; x++ )
 {
  // Die Koordinaten des aktuellen Pixels sind jetzt gegeben als (x,y).

  // Iterate over all neighborhood pixel coordinates.
  for( int m = -kernelHalfSizeY; m <= kernelHalfSizeY; m++ )
  {
    // Compute the pixel y coordinate for this kernel row.
    int j = y + m;

    // Apply reflected boundary conditions if coordinate is outside the image.
    if( j < 0 )
    {
      j = -j - 1;
    }
    else if( j >= imageSizeY )
    {
      j = 2 * imageSizeY - j - 1;
    }

    for( int k = -kernelHalfSizeX; k <= kernelHalfSizeX; k++ )
    {
      // Compute the pixel x coordinate for this kernel column.
      int i = x + k;

      // Apply reflected boundary conditions if coordinate is outside the image.
      if( i < 0 )
      {
        i = -i - 1;
      }
      else if( i >= imageSizeX )
      {
        i = 2 * imageSizeX - i - 1;
      }

      // Die Koordinaten des Punktes in der Nachbarschaft des aktuellen Pixels (x,y)
      // sind jetzt gegeben als (i,j).
      // Die korrespondierende Position in der Maske ist (k,m).
      // Beachten Sie, dass auf die Koordinaten (i,j) schon die Randbedingungen
      // angewendet wurden.

    }
  }
  // You have to set the grayvalues for the output image.
  //for grayvalue images use min

  //for binary use logic AND for questioning if there are in the set

 }
}

return true;
}

我的问题是我是否可以对这两种类型使用 min max 运算符? 或者如果当前图像是二进制图像,我应该提出一个案例问题然后处理图像?

【问题讨论】:

  • 对于离散图像,您可以对两者都使用 min/max,但在二进制情况下,您必须将 A 定义为黑色,将 B 定义为白色,且 A B 的情况并不少见,因此您需要明确这一点。另外,您在这里只处理方形结构元素,我建议您重新考虑目前使用的设计。

标签: c++ mathematical-morphology


【解决方案1】:

min/max 是最通用的公式(因为对于 {0, 1},min 等效于 AND,max 等效于 OR)。所以它适用于二进制和灰度。但是,对于二进制图像,有一些专门的算法可以更快地执行。

如果您想编写更少的代码,请使用 min/max。如果您真正关注的是二值图像,请使用 if 分支并搜索快速的二值形态实现(并且可能还会放弃对灰度的支持——您最终真的会使用它吗?)

【讨论】:

  • 是的,这就是我们必须做的。所以我会尝试编写代码:)
  • min/max 不是最通用的公式 inf/sup 或等价物,但在这里无关紧要。
  • inf/sup 对于有限集 min/max。所以我认为你想要处理具有无限灰度强度范围的无限/连续图像。您总是可以概括手头问题的各个方面(并获得与提问者无关的“更一般”的表述)。
【解决方案2】:

所以我实现了其余的,但它没有做任何侵蚀。可能是我用错了min函数,这里是更新的部分:

     min = m_InputImage->GetPixel(i,j);
          //max = m_InputImage->GetPixel(i,j);
          //printf("Min: %d\nMax: %d\nm: %d\nk: %d\n", min, max, m, k);

        }
      }
      // Sie muessen die Grauwerte des Ausgabebildes setzen.
      //für grauwert bilder verwende min

      //für binär verwende logisches UND zur abfrage ob in menge enthalten
      //printf("min: %d\n", min);
      m_OutputImage->SetPixel(x,y,min);
    }
  }

  return true;
}

【讨论】:

  • min 在上面声明 Image::PixelType min = std::numeric_limits<:pixeltype>::min();
  • 也许您现在应该将问题移至codereview.stackexchange.com?您可能还想将 cmets 翻译成英文以获得更大的曝光率。
  • min 需要介于当前最小强度和您找到的新强度之间,您的代码永远不会这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多