复制图像之间的非矩形区域 [英] Copying non-rectangular region between images

查看:61
本文介绍了复制图像之间的非矩形区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在图像之间复制非矩形(在我的情况下为饼形)部分?我在Bitmap类中查看了Clone方法,但它只接受Rectangles。不幸的是,每像素分析并没有令人满意的表现。

Is there a possibility to copy a non-rectangular (pie slice-shaped in my case) part between Images? I've looked on Clone method in Bitmap class, but it only accepts Rectangles. Per-pixel analysis doesn't give satisfactory performance unfortunately.

推荐答案

SetPixel / GetPixel 是一个严重的瓶颈。在你的情况下, LockBits 位图要好得多,以便你可以直接访问图像缓冲区(你会在CP的其他地方找到关于这个主题的帖子)。如果另外你可以生成一个运行长度编码的饼图表示(这不是一个简单的任务),你可以 memcpy 一次整个运行。这对于大面积来说非常有效。



关于对形状进行游程编码的额外提示:为了填充任意多边形,扫描线方法是众所周知的(http ://www.cecs.csulb.edu/~pnguyen/cecs449/lectures/fillalgorithm.pdf)。它相当于将多边形分解为梯形(其中一些简并为三角形)。您可以将其调整为饼图,只要考虑圆的曲率并在需要时将弧分割在顶部和底部,以确保水平线只穿过一次。
SetPixel/GetPixel are a severe bottleneck. In your case it is much better to "LockBits" the bitmap so that you can access the image buffer directly (you will find posts on this topic elsewhere in CP). If in addition you can generate a run-length-coded representation of the pie (not an easy task), you can memcpy whole runs at a time. This can be quite efficient for large areas.

Extra hint about run-length-coding the shape: to fill an arbitrary polygon, the sweepline approach is well known (http://www.cecs.csulb.edu/~pnguyen/cecs449/lectures/fillalgorithm.pdf). It amounts to decomposing the polygon into trapezia (some of which degenerate into triangles). You can adapt it to the case of a Pie, provided you take the curvature of the circle into account and split the arc at the top and bottom if needed to ensure that an horizontal line crosses once only.


首先 - 考虑故意选择矩形,因为可以快速分析所涉及的像素。考虑圆绘图例程通过包围矩形定义其内容。一般情况是你已经探索过的,逐个像素的,并且会特别引起很多痛苦解决每个问题。



但是,有一个供你寻求帮助的地方:



Gimp 2 - 一个开源的照片处理应用程序 - 有两个功能(至少)你可能会觉得有用:

自由选择工具:选择由定义封闭边界的任意数量的点定义的区域

剪刀选择工具:像自由选择工具一样使用但它在连接相邻点时定位边缘。



由于这是开源,你可以查看用于这两者的源代码,看看它是否有帮助。





Gimp 2主页^





First - consider that rectangles are chosen deliberately because the involved pixels can be "analyzed" quickly. Consider that circle-drawing routines define their content by the enclosing rectangle. The general case would be one you've already explored, pixel-by-pixel, and would cause a lot of pain solving each problem specifically.

BUT, there is a place for you to look for help:

Gimp 2 - an open source photo manipulation application - has two features (at least) you may find useful to consider:
Free Select Tool : selects a region defined by an arbitrary number of points defining a closed boundary
Scissor Select Tool: Used like Free Select Tool but it locates edges when connection adjacent points.

Since this is "Open Source", you can check out the source code used for both of these and see if it helps.


Gimp 2 Home Page^



再次感谢您输入,我有设法使用LockBits的建议方法解决问题。至于馅饼切片,我没有明确处理它。相反,我使用对数坐标和笛卡尔坐标之间的转换进行了映射,通过在对数极坐标域中进行的计算将源图像的像素相应地映射到目标图像上。



即使是预优化,性能也令人满意(在当前状态下,查找表中没有预先计算三角函数值。)
Thank you again for input, I have managed to solve the problem using suggested approach of LockBits. As for pie slice, I've not dealt with it explicitly. Instead I've made a mapping using transformations between log-polar and Cartesian coordinates, which mapped pixels of source image onto destination image accordingly by calculations done in log-polar domain.

Performance is satisfactory even pre-optimization (in current state none of the trigonometric values are precached in lookup table).


这篇关于复制图像之间的非矩形区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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