vb.net 中二维数组的滑动窗口

发布于 2024-10-13 21:09:50 字数 1643 浏览 0 评论 0原文

我在 vb.net 中的 2D 数组上滑动可变大小的窗口时遇到问题。我的问题是,当我在 0,0 处取数组的第一个元素时,无论窗口的大小如何,它都需要更小,因为有问题的元素必须是滑动窗口的中心。例如: arrar size(40,43) 窗口大小 5x5 (窗口大小为 NxN N=3 获胜大小为 3x3),因此数组(0,0)获胜大小为 5,因此需要计算出 2 列和 2 行并创建一个新的窗口大小为 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 像素是二维数组

I'm having problems with sliding a variable sized window over a 2D array in vb.net. my porblem is when I take the first element of the array at 0,0 what ever the size of the window is it needs to be smaller because the element in question has to be the center of the sliding window. ex: arrar size(40,43) window size 5x5 ( window size is NxN N=3 wins size is 3x3) so array(0,0) with win size 5 so 2 col and 2 rows need to be cout out and a new window size of 3x3. any help would be great.`

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

the var pixels is the 2d array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2024-10-20 21:09:50

像这样的东西吗?

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;
    }
}

Something like this?

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文