1

I have a 2D array (not jagged) of doubles (representing an image).

I want to crop part of it, to display, do statistics on etc.

This is what I have till now:

private double[,] subimage(double[,] image, int x1, int y1, int x2, int y2)
{
    int width = Math.Abs(x2 - x1);
    int height = Math.Abs(y2 - y1);
    double[,] newim = new double[width, height];
    int x0 = Math.Min(x1, x2);
    int y0 = Math.Min(y1, y2);
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            newim[i, j] = image[x0+i,y0+j];
        }
    }
    return newim;
}

Is there some way to avoid the double-for (for in general) loop?

e.g. Python has the convenient:

newim=image[x1:x2,y1:y2]
  • I have not really seen with 2d arrays but here is an example with 1d ->https://stackoverflow.com/questions/3130232/selecting-a-range-of-items-inside-an-array-in-c-sharp – panoskarajohn Dec 12 '19 at 21:10

0 Answers0