PxRT

by Patrick on August 03, 2013 at 00:00

This was a simple project to play with an interesting bit of image manipulation.

It sorts the pixels of an image, either in each row or in each column. There's also an option for controlling the brightness threshold. Once a pixel is found crossing the threshold the sorting stops for that row.

public static void SortVerticals(PixelWrapper Image)
{
    List Unsorted = Image.GetPixels();
    Pixel[] Sorted = new Pixel[Unsorted.Count];
    for (int x = 0; x < Image.Width; x++)
    {
        List Col = new List();
        // Split out vertical column
        for (int y = 0; y < Image.Height; y++)
        {
            Col.Add(Unsorted[(y * Image.Width) + x]);
        }
        Col.Sort();
        // Reconstruct
        for (int y = 0; y < Col.Count; y++)
        {
            Sorted[(y * Image.Width) + x] = Col[y];
        }
    }
    Image.SetPixels(Sorted.ToList());
}

This takes the list of Pixels from the image, moves down the y-axis adding each pixel into a new list, sorts it, and adds them into the Sorted array. Once the column is filled it moves forward along the x-axis to the next column.

The Pixel list works by unravelling the rows of an image. Each horizontal row of pixels follows the previous in one long line.

This excerpt doesn't take the threshold into account.