CSharp · Console
#nullable enable
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using SkiaSharp;
// WARNING: This downloads a sample images from a repo in GitHub - if it is slow to download
// then running this may result in an execution-time-limit-exceeded failure. If that is the
// case then trying running it again, it usually works then!
// Note that .NET Fiddle runs as Linux for .NET 6 builds, so SkiaSharp is being used instead of
// System.Drawing.Common, which is Windows-only as of .NET 6. Most of the code is same as it
// would be if System.Drawing.Common was the Bitmap type were used, the differences boil down
// largley to type names (eg. SKBitmap and SKColor) and slight differences in how they are
// instantiated - it doesn't affect the logic of code.
// The NuGet package SkiaSharp.NativeAssets.Linux.NoDependencies is also required to avoid
// runtime failures when run in this Linux environment.
class Program
{
static async Task Main()
{
// Most common highlighted area from frames in presentation, which it is presumed represents
// the area in the frames where the slides are being projected onto the wall
// (See https://dotnetfiddle.net/X8IPgQ)
var (topLeft, topRight, bottomRight, bottomLeft) =
(new Point(1224, 197), new Point(1915, 72), new Point(1915, 662), new Point(1229, 638));
// A sample frame image to use to demonstrate
const string frameImageUrl =
"https://raw.githubusercontent.com/ProductiveRage/NaivePerspectiveCorrection/main/" +
"Samples/Frames/frame_2837.jpg";
// Download the sample image..
using var httpClient = new HttpClient();
using var downloadStream = await httpClient.GetStreamAsync(frameImageUrl);
using var image = SKBitmap.Decode(downloadStream);
// .. then extract the highlighted area from the image and then try to contort it back into
// a rectangle to try to reverse the perspective distortion
using var extractedContent = SimplePerspectiveCorrection.ExtractAndPerspectiveCorrect(
image, topLeft, topRight, bottomRight, bottomLeft
);
const int resizeLargestSideTo = 150;
Console.WriteLine();
Console.Write("Below is a data url that is a shrunken version of the slide as it was shown");
Console.Write(" projected onto a wall in the original video, contorted back into a rectangle");
Console.Write(" (undoing the distortion of perspective) so that it will be easier to compare");
Console.WriteLine(" it to the original slides and try to work out which one is being shown:");
Console.WriteLine();
Console.WriteLine(GenerateDataUrlForResizedVersion(extractedContent, resizeLargestSideTo));
Console.WriteLine();
Console.WriteLine("The original (full size) version of the frame is available here:");
Console.WriteLine();
Console.WriteLine(frameImageUrl);
}
static string GenerateDataUrlForResizedVersion(SKBitmap image, int resizeLargestSideTo)
{
using var shrunken = image.CopyAndResize(resizeLargestSideTo);
using var shrunkenData = shrunken.Encode(SKEncodedImageFormat.Jpeg, 80);
using var memoryStream = new MemoryStream();
shrunkenData.SaveTo(memoryStream);
var shrunkenBytes = memoryStream.ToArray();
return "data:image/jpg;base64," + Convert.ToBase64String(shrunkenBytes);
}
}
static class BitmapExtensions
{
public static SKBitmap CopyAndResize(this SKBitmap image, int resizeLargestSideTo)
{
var (width, height) = (image.Width > image.Height)
? (resizeLargestSideTo, (int)((double)image.Height / image.Width * resizeLargestSideTo))
: ((int)((double)image.Width / image.Height * resizeLargestSideTo), resizeLargestSideTo);
var resized = new SKBitmap(width, height);
image.ScalePixels(resized, SKFilterQuality.High);
return resized;
}
public static DataRectangle<SKColor> GetAllPixels(this SKBitmap image)
{
var values = new SKColor[image.Width, image.Height];
var pixels = image.Pixels;
for (var y = 0; y < image.Height; y++)
{
for (var x = 0; x < image.Width; x++)
{
values[x, y] = pixels[(y * image.Width) + x];
}
}
return DataRectangle.For(values);
}
}
static class DataRectangle
{
public static DataRectangle<T> For<T>(T[,] values) => new DataRectangle<T>(values);
}
sealed class DataRectangle<T>
{
readonly T[,] _values;
public DataRectangle(T[,] values)
{
if ((values.GetLowerBound(0) != 0) || (values.GetLowerBound(1) != 0))
throw new ArgumentException("Both dimensions must have lower bound zero");
var arrayWidth = values.GetUpperBound(0) + 1;
var arrayHeight = values.GetUpperBound(1) + 1;
if ((arrayWidth == 0) || (arrayHeight == 0))
throw new ArgumentException("zero element arrays are not supported");
Width = arrayWidth;
Height = arrayHeight;
_values = new T[Width, Height];
Array.Copy(values, _values, Width * Height);
}
/// <summary>
/// This will always be greater than zero
/// </summary>
public int Width { get; }
/// <summary>
/// This will always be greater than zero
/// </summary>
public int Height { get; }
public T this[int x, int y]
{
get
{
if ((x < 0) || (x >= Width))
throw new ArgumentOutOfRangeException(nameof(x));
if ((y < 0) || (y >= Height))
throw new ArgumentOutOfRangeException(nameof(y));
return _values[x, y];
}
}
}
/// <summary>
/// This uses a simple algorithm to try to undo the distortion of a rectangle in an image due to
/// perspective - it takes the content that would originally have been rectangular and stretches
/// it back into a rectangle. This is only a simple approximation and does not guarantee accuracy
// (in fact, it will result in an image that is slightly vertically stretched such that its aspect
/// ratio will not match the original content and a more thorough approach is necessary if this is
/// too great an approximation)
/// </summary>
static class SimplePerspectiveCorrection
{
public static SKBitmap ExtractAndPerspectiveCorrect(
SKBitmap image,
Point topLeft,
Point topRight,
Point bottomRight,
Point bottomLeft)
{
var (projectionSize, sourceSliceLocations, sliceRenderer) =
GetProjectionDetails(topLeft, topRight, bottomRight, bottomLeft);
// It's quicker to read all of the pixels once and then get their values from a DataRectangle
// than it is to call GetPixel repeatedly on a bitmap
var pixels = image.GetAllPixels();
var projection = new SKBitmap(projectionSize.Width, projectionSize.Height);
foreach (var (lineToTrace, index) in sourceSliceLocations)
{
var lengthOfLineToTrace = LengthOfLine(lineToTrace);
var pixelsOnLine = Enumerable
.Range(0, lengthOfLineToTrace)
.Select(j =>
{
var fractionOfProgressAlongLineToTrace = (float)j / lengthOfLineToTrace;
var point = GetPointAlongLine(lineToTrace, fractionOfProgressAlongLineToTrace);
return GetAverageColour(pixels, point);
});
sliceRenderer(projection, pixelsOnLine, index);
}
return projection;
static SKColor GetAverageColour(DataRectangle<SKColor> pixels, PointF point)
{
var (integralX, fractionalX) = GetIntegralAndFractional(point.X);
var x0 = integralX;
var x1 = Math.Min(integralX + 1, pixels.Width);
var (integralY, fractionalY) = GetIntegralAndFractional(point.Y);
var y0 = integralY;
var y1 = Math.Min(integralY + 1, pixels.Height);
var (topColour0, topColour1) = GetColours(new Point(x0, y0), new Point(x1, y0));
var (bottomColour0, bottomColour1) = GetColours(new Point(x0, y1), new Point(x1, y1));
return CombineColours(
CombineColours(topColour0, topColour1, fractionalX),
CombineColours(bottomColour0, bottomColour1, fractionalX),
fractionalY
);
(SKColor c0, SKColor c1) GetColours(Point p0, Point p1)
{
var c0 = pixels[p0.X, p0.Y];
var c1 = (p0 == p1) ? c0 : pixels[p1.X, p1.Y];
return (c0, c1);
}
static (int Integral, float Fractional) GetIntegralAndFractional(float value)
{
var integral = (int)Math.Truncate(value);
var fractional = value - integral;
return (integral, fractional);
}
static SKColor CombineColours(SKColor x, SKColor y, float proportionOfSecondColour)
{
if ((proportionOfSecondColour == 0) || (x == y))
return x;
if (proportionOfSecondColour == 1)
return y;
return new SKColor(
red: CombineComponent(x.Red, y.Red),
green: CombineComponent(x.Green, y.Green),
blue: CombineComponent(x.Blue, y.Blue),
alpha: CombineComponent(x.Alpha, y.Alpha)
);
byte CombineComponent(byte x, byte y) =>
Math.Min(
(byte)Math.Round((x * (1 - proportionOfSecondColour)) + (y * proportionOfSecondColour)),
(byte)255
);
}
}
}
delegate void SliceRenderer(SKBitmap image, IEnumerable<SKColor> pixelsOnLine, int index);
sealed record ProjectionDetails(
Size ProjectionSize,
IEnumerable<((PointF From, PointF To) Line, int Index)> SourceSliceLocations,
SliceRenderer SliceRenderer
);
/// <summary>
/// Depending upon the dimensions of the source area, better results may be achieved by taking
/// slices across the image horizontally or vertically - currently this method always takes
/// vertical slices but it's feasible that different images might get better results if
/// horizontal slices were taken.
/// </summary>
static ProjectionDetails GetProjectionDetails(
Point topLeft,
Point topRight,
Point bottomRight,
Point bottomLeft)
{
var edgeToStartFrom = (From: topLeft, To: topRight);
var edgeToConnectTo = (From: bottomLeft, To: bottomRight);
var lengthOfEdgeToStartFrom = LengthOfLine(edgeToStartFrom);
var dimensions = new Size(
lengthOfEdgeToStartFrom,
GetProjectionHeight(topLeft, topRight, bottomRight, bottomLeft)
);
return new ProjectionDetails(dimensions, GetSourceSliceLocationEnumerator(), RenderSlice);
IEnumerable<((PointF From, PointF To) Line, int Index)> GetSourceSliceLocationEnumerator() =>
Enumerable
.Range(0, lengthOfEdgeToStartFrom)
.Select(i =>
{
var fractionOfProgressAlongPrimaryEdge = (float)i / lengthOfEdgeToStartFrom;
return (
(
GetPointAlongLine(edgeToStartFrom, fractionOfProgressAlongPrimaryEdge),
GetPointAlongLine(edgeToConnectTo, fractionOfProgressAlongPrimaryEdge)
),
i
);
});
static void RenderSlice(SKBitmap bitmap, IEnumerable<SKColor> pixelsOnLine, int index)
{
var pixelsOnLineArray = pixelsOnLine.ToArray();
using var slice = new SKBitmap(1, pixelsOnLineArray.Length);
for (var j = 0; j < pixelsOnLineArray.Length; j++)
slice.SetPixel(0, j, pixelsOnLineArray[j]);
using var canvas = new SKCanvas(bitmap);
canvas.DrawBitmap(slice, dest: new SKRect(index, 0, index + 1, bitmap.Height));
}
}
static PointF GetPointAlongLine((PointF From, PointF To) line, float fraction)
{
var deltaX = line.To.X - line.From.X;
var deltaY = line.To.Y - line.From.Y;
return new PointF(
(deltaX * fraction) + line.From.X,
(deltaY * fraction) + line.From.Y
);
}
static int LengthOfLine((PointF From, PointF To) line)
{
var deltaX = line.To.X - line.From.X;
var deltaY = line.To.Y - line.From.Y;
return (int)Math.Round(Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY)));
}
static int GetProjectionHeight(Point topLeft, Point topRight, Point bottomRight, Point bottomLeft)
{
var leftVerticalEdgeLength = LengthOfLine((topLeft, bottomLeft));
var rightVerticalEdgeLength = LengthOfLine((topRight, bottomRight));
var shorterVerticalEdgeLength = Math.Min(leftVerticalEdgeLength, rightVerticalEdgeLength);
var largerVerticalEdgeLength = Math.Max(leftVerticalEdgeLength, rightVerticalEdgeLength);
return (shorterVerticalEdgeLength + largerVerticalEdgeLength) / 2;
}
}