在vb.net中的2d数组的滑动窗口

时间:2011-01-25 06:40:58

标签: vb.net arrays 2d multidimensional-array

我遇到了在vb.net中将可变大小窗口滑过2D数组的问题。我的问题是,当我将数组的第一个元素设置为0,0时,窗口的大小需要更小,因为所讨论的元素必须是滑动窗口的中心。例如:arrar大小(40,43)窗口大小5x5(窗口大小是NxN N = 3胜大小是3x3)所以数组(0,0)的胜利大小为5所以2 col和2行需要cout out和一个新的窗口大小为3x3。任何帮助都会很棒。

Public Function getPIXELSinWINDOW(ByVal Wsize As Integer, ByVal x As Integer, ByVal y As Integer)

        Dim tempARRAY As New ArrayList()
        Dim Xwidth As Integer = Wsize
        Dim Yheight As Integer = Wsize
        Dim Xvalue As Integer = x - Wsize / 2
        Dim Yvalue As Integer = y - Wsize / 2
        Dim imgHEIGHT As Integer = Me.mysize.Height
        Dim imgWIDTH As Integer = Me.mysize.Width
        Dim i, j As Integer


        While Xvalue < 0
            Xvalue += 1
            Xwidth -= 1
        End While
        While Xvalue > imgWIDTH
            Xvalue -= 1
            Xwidth -= 1
        End While
        While Xwidth >= imgWIDTH
            Xwidth -= 1
        End While
        While Xvalue + Xwidth > imgWIDTH
            Xwidth -= 1
        End While

        While Yvalue < 0
            Yvalue += 1
            Yheight -= 1
        End While
        While Yvalue > imgHEIGHT
            Yvalue -= 1
            Yheight -= 1
        End While
        While Yheight >= imgHEIGHT
            Yheight -= 1
        End While
        While Yvalue + Yheight > imgHEIGHT
            Yheight -= 1
        End While

        For i = Xvalue To Xvalue + Xwidth
            For j = Yvalue To Yvalue + Yheight
                tempARRAY.Add(pixels(j, i))
            Next
        Next

        Return tempARRAY
    End Function

var像素是2d数组

1 个答案:

答案 0 :(得分:0)

这样的东西?

public class MainClass
{
    public static void Main (string[] args)
    {
        MainClass mc = new MainClass();
        List<int> pix = mc.GetPixelsInWindow(5, 40, 43);            
    }

    private int[,] data = new int[40, 43];

    public List<int> GetPixelsInWindow(int windowSize, int xPoint, int yPoint)
    {
        // As we are dealing with integers, this will result in a truncated value,
        // so a windowSize of 5 will yield a windowDelta of 2.
        int windowDelta = windowSize / 2;
        List<int> pixels = new List<int>();

        int xMin = Math.Max(0, xPoint - windowDelta);
        int xMax = Math.Min(data.GetLength(0) - 1, xPoint + windowDelta);

        int yMin = Math.Max(0, yPoint - windowDelta);
        int yMax = Math.Min(data.GetLength(1) - 1, yPoint + windowDelta);

        for (int x = xMin; x <= xMax; x++)
        {
            for (int y = yMin; y <= yMax; y++)
            {
                Console.WriteLine("Getting pixel (" + x.ToString() + ", " + y.ToString() + ")...");
                pixels.Add(data[x, y]);
            }
        }   

        return pixels;
    }
}