关于贝塞尔曲线实现的问题? [英] Question about the implementation of Bezier Curves?

查看:116
本文介绍了关于贝塞尔曲线实现的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些bezier曲线的教程,比如这个 http:// www 。bproject.com / KB / recipes / BezirCurves.aspx

创建贝塞尔曲线的基本思想是使用一些控制点并决定有多少需要创建新点。然后插入这些新的点。



下面是问题:假设我有1000分,我会喜欢多插入2000点。我想要使​​用的控制点的数量是5.参数t在[0,1]范围内。



Givens点P0,P1,P2,P3 ,P4,P5,P6,... P1000。
我可以使用P0-P4来生成新的点,那么接下来是什么?
使用P5-P9来产生新的点数?我可以立即看到P4和P5之间有突然的变化。



我该如何解决这个问题?



谢谢



//////////////////////////////// /////////////////////
Hello Stargazer712,



我了解您的意见,直至达到

假设我们有以下几点:$ b​​
$ b

  A1→A2→A3→A4→A5→A6→A7→A8初始点

你说我们需要在每一对中点加上一个新点。



我的问题是新点是?



让我们使用这个注解(A1 + A3)/ 2 == A12



现在生成的新点是

  A13 A24 A35 A46 A57 A68(这就是您的意思是每隔一对?

我应该在哪里插入这些点到原始列表中?



我工作的轮廓o n是从二进制图像中提取的。生成的轮廓为锯齿形。应用这种平滑方法之后,它的形状不会有太大的改善。我认为主要原因是邻居彼此靠近并且使插值没有用。



谢谢

/////////////////////////////////////////////// /////

解决方案

我想你要做的是创建一个内插点的光滑曲线。要做到这一点,您需要了解以下有关贝塞尔曲线:



假设我们有两条曲线,分别为A1,A2,A3,A4和B1,B2,B3 ,和B4。如果两条曲线在同一点上结束,并且如果第一个控制点与下一个控制点的第一个控制点共线,则曲线会平滑。所以在我们的例子中,如果:


  • A4 == B1

  • A3,A4和B2是共线的(同样说A3,B1,B2是共线的)



然后曲线会变得平滑。
$ b

为了获得任意点并建立平滑曲线,我们需要强制这两个条件成立。

要做到这一点,可以说我们从一组要点开始:



为了强制上述条件,我们添加一些额外的点。我们会在所有其他对的中点处添加一个新点,如图所示: stack.imgur.com/9jsMi.pngalt =使用meta points>



现在我们可以绘制0-3,3-6,6点之间的贝塞尔曲线-9等,我们可以肯定它会形成一条平滑的曲线:



希望这有助于您!
$ b 编辑:这是一个简单的Python程序,实现上面显示的内容(我的意思是)。你需要安装python和PIL:

  from PIL import Image 
import math


#在图片上绘制单点

def drawPoint(img,loc,size = 5,color =(0,0,0)):
px (大小):
for y in range(size):
xloc = loc [0] + x - size / 2
yloc = loc [1] + y - size / 2
px [xloc,yloc] = color



#绘制一个简单的贝塞尔曲线,4点

def drawCurve(img,points):

steps = 20
在范围内(步数):

t = i / float(steps)

xloc = math.pow(1-t,3)* points [0] [0] \
+ 3 * t * math.pow(1- t,2)* points [1] [0] \
+ 3 *(1-t)* math.pow(t,2)* points [2] [0] \
+ math.pow(t,3)* points [3] [0]
yloc = math.pow(1-t,3)* points [0] [1] \
+ 3 * t * math.pow( 1-t,2)* points [1] [1] \
+ 3 *(1-t)* math.pow(t,2)* points [2] b + math.pow(t,3)* points [3] [1]

drawPoint(img,(xloc,yloc),size = 2)


$
#绘制一个具有任意数量点的贝塞尔曲线

def drawBezier(img,points):

在范围内(0,len (分),3):
if(i + 3 < len(points)):
drawCurve(img,points [i:i + 4])



#通过添加点来绘制平滑的贝塞尔曲线
#强制平滑

def drawSmoothBezier(img,points):
$ b $ newpoints = []

(points)):

#添加下一个点(并绘制它)
newpoints.append(points [i])
drawPoint(img,points [i],color =(255,0,0))

if(i%2 == 0且i> 0且i + 1
#计算中点
xloc =(points [i] [0] + points [i + 1] [0])/2.0
yloc =(points [i] [1] + points [i + 1] [1])/ 2.0

#添加新点(并绘制它)
newpoints.append((xloc,yloc))
drawPoint(img,(xloc,yloc ),color =(0,255,0))

drawBezier(img,newpoints)



创建图像
myImage = Image.new( RGB,(627271),(255,255,255))

#创建点
points = [(54,172),
(121,60),
(220,204),
(284,56) ,
(376,159),
(444,40),
(515,228),
(595,72)]

#绘制曲线
drawSmoothBezier(myImage,points)

#保存图片
myImage.save(myfile.png,PNG)

这条线将遵循点的模式。如果你的结果是锯齿状的,那是因为这就是这些线条的样子。


I have read some tutorials for bezier curve such as this one http://www.codeproject.com/KB/recipes/BezirCurves.aspx.

The basic idea to create bezier curve is to use some control points and make decision how many new points need to be created. And then interpolate those new points.

Here is the question:

Assume I have 1000 points and I would like to interpolate 2000 points more. The number of control points I want to use is 5. The parameter t is in the range of [0, 1].

Givens points P0, P1, P2, P3, P4, P5, P6, ...P1000. I can use P0-P4 to generate new points, then what's next? use P5-P9 to generate new points??? I can immediately see there is a sudden transform between P4 and P5.

How can I solve this issue?

Thank you

///////////////////////////////////////////////////// Hello Stargazer712,

I understand your comments until it reaches the implementation method.

Assume we have the following points:

A1->A2->A3->A4->A5->A6->A7->A8 initial points

You said that we need to add a new point at the midpoint of every other pair.

My question is what the order of the new point is?

Let use use this annotation (A1+A3)/2 == A12

Now generated new points are

A13 A24 A35 A46 A57 A68 (this is what you mean "every other pair"?

Where should I insert those points into the original list?

The contour I am working on is extracted from binary image. The generated contour is zig-zag shape. After I apply this smooth method, it shape doesn't improve too much. I think the major reason is that the neighbors are near each other and make the interpolation not that useful.

Thank you

////////////////////////////////////////////////////

解决方案

I think what you are trying to do is to create a smooth curve interpolating the points. To do this, you need to understand the following about Bezier curves:

Say we have two curves with points A1, A2, A3, A4, and B1, B2, B3, and B4.

If the two curves end on the same point, and if the last control point of the first is colinear with the first control point of the next, then the curves will be smooth. So in our example, if:

  • A4 == B1
  • A3, A4, and B2 are colinear (same as saying A3, B1, B2 are colinear)

Then the curves will be smooth.

In order to take an arbitrary list of points and make a smooth curve, we need to force these two conditions to be true.

To do this, lets say that we start with a set of points:

To force the above conditions, lets add some extra points. We'll place a new point at the midpoint of every other pair as shown:

We can now draw bezier curves between points 0-3, 3-6, 6-9, etc., and we can be sure that it will form a smooth curve:

Hope this helps!

EDIT: Here's a simple python program that implements exactly what was shown above (and I mean exactly). You need to have python and PIL installed:

from PIL import Image
import math

#
#   draws a single point on our image
#
def drawPoint( img, loc, size=5, color=(0,0,0) ):
    px = img.load()
    for x in range(size):
        for y in range(size):
            xloc = loc[0] + x - size/2
            yloc = loc[1] + y - size/2
            px[ xloc, yloc ] = color


#
#   draws a simple bezier curve with 4 points
#            
def drawCurve( img, points ):

    steps = 20
    for i in range(steps):

        t = i / float(steps)

        xloc = math.pow(1-t,3) * points[0][0] \
             + 3*t*math.pow(1-t,2) * points[1][0] \
             + 3*(1-t)*math.pow(t,2) * points[2][0] \
             + math.pow(t,3) * points[3][0]
        yloc = math.pow(1-t,3) * points[0][1] \
             + 3*t*math.pow(1-t,2) * points[1][1] \
             + 3*(1-t)*math.pow(t,2) * points[2][1] \
             + math.pow(t,3) * points[3][1]

        drawPoint( img, (xloc,yloc), size=2 )


#
#   draws a bezier curve with any number of points
#
def drawBezier( img, points ):

    for i in range(0,len(points),3):
        if( i+3 < len(points) ):
            drawCurve( img, points[i:i+4] )


#
#   draws a smooth bezier curve by adding points that
#   force smoothness
#
def drawSmoothBezier( img, points ):

    newpoints = []

    for i in range(len(points)):

        # add the next point (and draw it)
        newpoints.append(points[i])
        drawPoint( img, points[i], color=(255,0,0) )

        if( i%2 == 0 and i>0 and i+1<len(points) ):

            # calculate the midpoint
            xloc = (points[i][0] + points[i+1][0]) / 2.0
            yloc = (points[i][1] + points[i+1][1]) / 2.0

            # add the new point (and draw it)
            newpoints.append( (xloc, yloc) )
            drawPoint( img, (xloc, yloc), color=(0,255,0) )

    drawBezier( img, newpoints )



#   Create the image
myImage = Image.new("RGB",(627,271),(255,255,255))

#   Create the points
points = [  (54,172), 
            (121,60), 
            (220,204), 
            (284,56), 
            (376,159), 
            (444,40), 
            (515,228), 
            (595,72) ]

#   Draw the curve
drawSmoothBezier( myImage, points )

#   Save the image
myImage.save("myfile.png","PNG")

The line will follow the pattern of the points. If your result is zig-zagged, that's because that's what the lines looked like.

这篇关于关于贝塞尔曲线实现的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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