我们必须首先实现 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 运算符?或者我应该提出一个案例问题,如果当前图像是二进制然后处理图像?