代码之家  ›  专栏  ›  技术社区  ›  Aamir

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

  •  10
  • Aamir  · 技术社区  · 6 年前

    我用手机拍摄了倾斜图像。我想在两边的两个矩形之间切割图像的部分,找出它们之间的圆。我有中间部分的所有4个坐标,比如(x0,y0),(x1,y1),(x2,y2),(x3,y3)。

    enter image description here

    但是作物功能我有点像

    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;
    }  
    

    但上面的函数将部分剪切为矩形,如第一个和第二个红色框所示。
    我正在寻找代码,以削减部分显示在第三个红色矩形。我搜索了代码,找到了下面的代码

    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);
    

    enter image description here

    这就扰乱了圆的形状,使其成为椭圆,这就给其他计算带来了问题。
    请让我知道如何裁剪图像使用4点。

    1 回复  |  直到 6 年前
        1
  •  10
  •   TheSoftwareJedi jac    6 年前

    使用此图像:

    enter image description here

    由以下代码转换:

            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");
    

    此图像中的结果:

    enter image description here

    enter image description here

    推荐文章