简体   繁体   English

如何在c#中裁剪倾斜图像

[英]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). 我有中间部分的所有4个坐标,如(x0,y0),(x1,y1),(x2,y2),(x3,y3)。

My Image is looks like, 我的形象看起来像, 在此输入图像描述

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. 但上面的功能切割部分为矩形,如第1和第2红色框所示。
I am looking for code to cut portion shown in 3rd red rectangle. 我正在寻找切割第3个红色矩形所示部分的代码。 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. AForge.Imaging。 library but it will cut potion like below 图书馆,但它会切割下面的药水

在此输入图像描述

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. 请让我知道如何使用4点裁剪图像。
Or is there is any way to correct the tilt of image by passing correction angle? 或者有没有办法通过校正角度来校正图像的倾斜?

If the quadrilateral includes angled sides, the resulting transformation actually looks pretty good. 如果四边形包括有角度的边,那么最终的转换实际上看起来非常好。

Using this image: 使用此图片:

在此输入图像描述

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: 结果在这张图片中:

在此输入图像描述

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: 我的观点大致如下:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM