Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to crop tilt image in c#

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?

like image 557
Aamir Avatar asked Oct 07 '18 18:10

Aamir


1 Answers

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

like image 70
TheSoftwareJedi Avatar answered Nov 08 '22 21:11

TheSoftwareJedi