根据4点坐标裁剪图像 [英] Cropping an image based on 4 point Co-ordinates

查看:145
本文介绍了根据4点坐标裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从图像中裁剪矩形的方法,给定信息是C#或vb中的4个角点坐标(x,y).
我不想遵循使用左上角坐标以及宽度和高度的常规方法的原因,因为它给了我与轴平行的矩形(在我的场景中,Rectangle可能会稍微倾斜).如何在C#/vb .net中编写具有以下语法的函数

图片img =裁剪(bmp,x1,y1,x2,y2,x3,y3,x4,y4);

其中bmp是原始图像.

I am looking for a way to crop a rectangle from an image given information is the 4 corner point co-ordinates (x,y) in C# or vb.
The reason i don''t want to follow usual approach that uses top left point co-ordinates and width and height because it gives me rectangle that is parallel to the axes (Rectangle could be a little tilted in my scenario). How do i write a function in C#/vb .net that has following syntax

Image img = Crop(bmp,x1,y1,x2,y2,x3,y3,x4,y4);

Where bmp is original image.

推荐答案

请不要在您的问题中加上紧急"字样,我们不太在乎您能做到这一点"做好您的工作,已经等不及看了.

如您所说,位图始终是一个矩形.因此,即使无法将其设为黑色或其他形状,也无法使用随机坐标来做您想做的事情,并且没有多余的信息.如果四个点定义一个矩形但已旋转,则需要旋转图像以为其创建位图.因此,您可能想做的是确定旋转图像所需的距离. GDI +为旋转图像提供了很多支持,它计算旋转角度以及矩形的点将是一个挑战.
Please don''t put ''urgent'' in your question, we don''t really care that you can''t do your job and waited too late to look in to it.

As you say, a bitmap is always a rectangle. As such, if it''s not possible to use random co-ordinates to do what you want, and not have extra info, even if you make it black or something. If the four points define a rectangle, but rotated, then you need to rotate the image to create a bitmap of it. As such, what you probably want to do, is work out how far you need to rotate the image. GDI+ has plenty of support for rotating the image, it''s calculating how far to rotate and what the points of your rectangle will be, that will be the challenge.


假设这四个点定义任意多边形,您可能需要应用蒙版.
您可以这样进行:
1)创建一个与原始图像大小相同的遮罩图像(可能)
2)在该蒙版图层"上绘制填充为多边形的多边形
3)使用遮罩和原始图像(如此处)应用遮罩算法
4)定义多边形的边界框并裁剪结果

另外,如果合适,您可以移至RGBA空间,并在遮罩外部应用完全透明的效果.
本文 [ AForge [^ ]可能也有帮助.
Assuming that these four point define an arbitrary polygon, you will probably need to apply a mask.
You could proceed like this:
1) Create a mask image with the same size as your original (probably)
2) Draw your polygon as filled one onto this mask "layer"
3) Apply a masking algorithm using the mask and the original image (like here)
4) Define the bounding box of the polygon and crop the result

Alternatively, if suitable, you could move to RGBA space, and apply full transparency outside the mask.
This article[^], based on AForge[^] might also help you.


这篇关于根据4点坐标裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆