How to crop tilt image in c#
Asked Answered
A

1

10

I have tilt image capture by mobile. I want to cut section/portion of image in between two rectangles of both sides to find out circles in between them. I have all 4 co-ordinates of middle section like (x0,y0),(x1,y1),(x2,y2),(x3,y3).

My Image is looks like, enter image description here

But crop function I have something like

public static Bitmap CropImage(int x, int y, int width, int height, Bitmap bitmap)
{

    Bitmap croppedImage;
    var originalImage = bitmap;
    {
        Rectangle crop = new Rectangle(x, y, width, height);
        croppedImage = originalImage.Clone(crop, originalImage.PixelFormat);

    } // Here we release the original resource - bitmap in memory and file on disk.

    return croppedImage;
}  

But above function cuts portion as rectangle as show in 1st and 2nd red color box.
I am looking for code to cut portion shown in 3rd red rectangle. I had search for code and find out below code

List<IntPoint> corners = new List<IntPoint>();

corners.Add(new IntPoint(x0, y0));
corners.Add(new IntPoint(x3, y3));
corners.Add(new IntPoint(x1 + 30, y1 + 20));
corners.Add(new IntPoint(x2 + 30, y2 + 0));
AForge.Imaging.Filters.QuadrilateralTransformation filter = new AForge.Imaging.Filters.QuadrilateralTransformation(corners, WidthOfCut, HeightOfCut);
Bitmap newImage = filter.Apply(mainOuterWindow);

of AForge.Imaging. library but it will cut potion like below

enter image description here

which disturb shape of circle and make it ellipse which causes issue for other calculation.
please let me know how to crop image using 4 points.
Or is there is any way to correct the tilt of image by passing correction angle?

Amice answered 7/10, 2018 at 18:23 Comment(1)
Is it okay to just the extract circles only, or you really want the pixel distortion fix?Melessa
H
10

If the quadrilateral includes angled sides, the resulting transformation actually looks pretty good.

Using this image:

enter image description here

Transformed by this code:

        var i = Image.FromFile("pic.png");

        List<IntPoint> corners = new List<IntPoint>();
        corners.Add(new IntPoint(63, 183));
        corners.Add(new IntPoint(863, 151));
        corners.Add(new IntPoint(869, 182));
        corners.Add(new IntPoint(65, 211));
        QuadrilateralTransformation filter = new QuadrilateralTransformation(corners, 869 - 63, 211 - 183);
        var i2 = filter.Apply(i);

        i2.Save("pic2.png");

Results in this image:

enter image description here

I think that's exactly what you are looking for.

The trick was making sure the quadrilateral transform uses angled sides to avoid the skew. My points look roughly like this:

enter image description here

Hillary answered 10/10, 2018 at 14:17 Comment(2)
Better if you use a distance formula and average for width and height, it will be more precise.Gegenschein
Hi TheSoftwareJedi, thank you so much, it works for me.Amice

© 2022 - 2024 — McMap. All rights reserved.