二进制和灰度值图像的侵蚀/扩展

时间:2012-12-30 14:43:48

标签: c++ mathematical-morphology

我们必须先实现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;
}

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

2 个答案:

答案 0 :(得分:0)

min / max是最通用的公式(因为min等于AND,而最大值是{0,1}的OR)。因此它适用于二进制和灰度。但是,二进制图像的专用算法执行速度更快。

如果要编写更少的代码,请使用min / max。如果您真正专注于二进制图像,请使用if分支并搜索快速二进制形态实现(并且可能还会删除对灰度的支持 - 您最终是否会使用它?)

答案 1 :(得分:0)

所以我实施了其余部分,但它没有做任何侵蚀。也许我错误地使用了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;
}