■ 최대값 필터 회선 적용하기

----------------------------------------------------------------------------------------------------

#region 최대값 필터 회선 적용하기 - ConvolveMaximumValueFilter(sourceArray, windowWidth, windowHeight)

 

/// <summary>

/// 최대값 필터 회선 적용하기

/// </summary>

/// <param name="sourceArray">소스 배열</param>

/// <param name="windowWidth">윈도우 너비</param>

/// <param name="windowHeight">윈도우 높이</param>

/// <returns>최대값 필터 회선 적용 배열</returns>

public int[,] ConvolveMaximumValueFilter(int[,] sourceArray, int windowWidth, int windowHeight)

{

    int    sourceWidth  = sourceArray.GetUpperBound(1) + 1;

    int    sourceHeight = sourceArray.GetUpperBound(0) + 1;

    int[,] resultArray  = new int[sourceHeight, sourceWidth];

    int    xPadding     = windowWidth  / 2;

    int    yPadding     = windowHeight / 2;

    int    windowSize   = windowHeight * windowWidth;

    int[]  targetArray  = new int[windowSize];

    int    index;

 

    for(int y = 0; y < sourceHeight - 2 * yPadding; y++)

    {

        for(int x = 0; x < sourceWidth - 2 * xPadding; x++)

        {

            index = 0;

 

            for(int r = 0; r < windowHeight; r++)

            {

                for(int c = 0; c < windowWidth; c++)

                {

                    targetArray[index++] = sourceArray[y + r, x + c];

                }

            }

 

            resultArray[y + yPadding, x + xPadding] = GetMaximumValue(targetArray, windowSize);

        }

    }

 

    for(int y = 0; y < yPadding; y++)

    {

        for(int x = xPadding; x < sourceWidth - xPadding; x++)

        {

            resultArray[y, x] = resultArray[yPadding, x];

 

            resultArray[sourceHeight - 1 - y, x] = resultArray[sourceHeight - 1 - yPadding, x];

        }

    }

 

    for(int x = 0; x < xPadding; x++)

    {

        for(int y = 0; y < sourceHeight; y++)

        {

            resultArray[y, x] = resultArray[y, xPadding];

 

            resultArray[y, sourceWidth - 1 - x] = resultArray[y, sourceWidth - 1 - xPadding];

        }

    }

 

    return resultArray;

}

 

#endregion

 

#region 최대값 구하기 - GetMaximumValue(targetArray, targetLength)

 

/// <summary>

/// 최대값 구하기

/// </summary>

/// <param name="targetArray">타겟 배열</param>

/// <param name="targetLength">타겟 길이</param>

/// <returns>최대값</returns>

public int GetMaximumValue(int[] targetArray, int targetLength)

{

    int maximumValue = targetArray[0];

 

    for(int i = 1; i < targetLength; i++)

    {

        if(targetArray[i] > maximumValue)

        {

            maximumValue = targetArray[i];

        }

    }

 

    return maximumValue;

}

 

#endregion

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요