Click here to load reader
Upload
vonguyet
View
217
Download
4
Embed Size (px)
Citation preview
Image Analysis and Synthesis - A.Y. 2006/2007
Simone GaspariniSimone [email protected]
Image Analysis and Synthesis - A.Y. 2006/2007
Outline
Graphics Pipeline
Clipping
Rasterization
Line rasterization
• Analytical algorithm
• DDA algorithm
• Bresenham’s algorithm
Image Analysis and Synthesis - A.Y. 2006/2007
Graphics pipeline
Steps required for rendering a 3D scene
Given a 3D (geometric) model of the scene and a viewpoint, generate the 2D image of the scene
Usually implemented as a mixture of software (CPU based) and hardware (GPU based) operations
Pipeline: a set of data processing elements connected in series, so that the output of one element is the input of the next one.
Graphics Pipeline: transform the 3D representation into a image
Image Analysis and Synthesis - A.Y. 2006/2007
Graphics pipeline
Geometric model of the sceneRepresentation by means of points/vertex
M odeling
Geom etry
Rasterization
Display
Image Analysis and Synthesis - A.Y. 2006/2007
Graphics pipeline
M odeling
Geom etry
Rasterization
Display
Clipping (field of view)Projection (3D 2D)Hidden surface removal (only visiblesurfaces)Shading & Shadowing (according tolight source(s) )
Image Analysis and Synthesis - A.Y. 2006/2007
Graphics pipeline
M odeling
Geom etry
Rasterization
Display
From projected points to pixel (discretization )
Image Analysis and Synthesis - A.Y. 2006/2007
Graphics pipeline
M odeling
Geom etry
Rasterization
Display
Image visualization (or printing)
Image Analysis and Synthesis - A.Y. 2006/2007
Clipping
Consider only scene objects that are in the field of view of the camera
Two main approaches
BEFORE rasterization• For each objects evaluates intersections with the field of view• Possible intersections are new vertices of the object• It speeds up rasterization• It can be used for simple geometric primitives (segments)
AFTER rasterization (Scissoring)• Perform rasterization on all geometric primitives• Keep only the primitives inside the image borders
Image Analysis and Synthesis - A.Y. 2006/2007
Rasterization
Last step in the graphics pipeline
Discretization:
• Projected points in float coordinates
• Pixel in int coordinates
Geometric primitives (segments) must be drawn into a pixel matrix (raster grid)
• A point is represented by a pixel
• A line segments as a chain of adjacent pixels
• Polygon as a set of connected pixels
If matrix is dense enough any kind of geometric primitive can bedrawn
Also known as Scan Conversion
Image Analysis and Synthesis - A.Y. 2006/2007
Raster Grid
A grid of square pixelThe pixel centre has integer coordinatesA projected point is represented by the nearest pixel centreSegment line as a sequence of pixels such that:• Pixels lies as close to the ideal
line as possible• Sequence is straight as
possible
Pixel
Image Analysis and Synthesis - A.Y. 2006/2007
Requirements
Clipping and Rasterization functions are called each time an image is created or modified
Hence… TIME MATTERS!
If Real-Time is required (e.g. animation), functions must be optimized
Optimization means:
• Use incremental algorithms− Exploits previous calculations− Minimize the use of time-consuming operation (multiplies and divides)
• Employ integer arithmetic− Integer arithmetic is faster than the floating-point one
Image Analysis and Synthesis - A.Y. 2006/2007
Segments Rasterization
Line equation• y = mx + B• m = (x1-x2)/(y1-y2) = Δx/Δy
|m|<1 |m|>1|m| = 1 easy casem = 0 easy casem inf easy case
We deal with |m| < 1|m| < 1 is straightforward
|m|<1 a pixel foreach column
|m|>1 a pixel foreach rowFrom now on:
Pixel is “ON” Pixel is “OFF”
Image Analysis and Synthesis - A.Y. 2006/2007
Analytical method – dummy method
Suppose:• P1(x1,y1) and • P2(x2,y2) segment extrema in integer coordinates
P1
P2
Starting from extremum having lowest x-coordinate:• xi= xi-1+1 • yi = mxi+ B• Round(yi)
Always select the closest pixel
Highly inefficient method!• Floating-point multiply (mx)• Floating-point addition (mx+B)• Round
Image Analysis and Synthesis - A.Y. 2006/2007
DDA Algorithm
Floating-point multiply can be avoid using incremental technique
• The new point is calculated exploiting previous point
Digital Differential Analyzer algorithm (DDA)
• Mechanical device used to solve differential equation by numerical methods
• It trace out successive (x, y) values by simultaneously incrementing x and y by small steps proportional to the first derivative of x and y
• Line is the solution of
dy mdx
=
Image Analysis and Synthesis - A.Y. 2006/2007
DDA Algorithm
Suppose |m|<1Note that:
But since
( )1 1
1
1
i i
i i
i i
y mx By m x x By y m x
+ +
+
+
= +
= + Δ +
= + Δ
1i iy y m+ = +
1xΔ =
1. Floating point addition
2. Round
Image Analysis and Synthesis - A.Y. 2006/2007
DDA Algorithm
Pseudo-code
Line( int x0, int y0, int x1, int y1, int value)
{int x;float dy, dx, y, m;
dy = y1-y0;dx = x1-x0;m = dy/dx;y = y0;
for (x = x0; x <= x1; x++) {
WritePixel(x, floor(0.5 + y), value);y = y + m;
}}
// assumes |m|<1, x0 < x1
// initialization
// step y by slope m
// value to place in pixels
// set pixel to value
Image Analysis and Synthesis - A.Y. 2006/2007
DDA Algorithm
|m|<1 and |m|>1 must be managed separately
Image Analysis and Synthesis - A.Y. 2006/2007
Can we do better than DDA?
DDA uses
• Floating point addition
• Round function
Floating-point arithmetic should be avoided
• Approximation error
• Time consuming
We need an incremental method that uses only integer arithmetic
Bresenham’s algorithm (1965)
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s Algorithm
Incremental methodEmploys only integer arithmeticHighly efficientCan be also applied to the rasterization of circles
At step n (assumes |m|<1):• P (xp,yp) pixel selected during previous step• For xp+1 we can select either E or NE pixel
NE
E
P (xp,yp)
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s Algorithm
Let Q intersection point between ideal line and x=xp+1 line
Let M the mid-point of the segment E-NE
Select the pixel minimizing the distance from the ideal line
Select the pixel on the same side of Q
NE
EP (xp,yp)
M
Q
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s Algorithm
To compute Q we need floating point arithmetic
Since the algorithms reduces to a choice between two pixel we can
exploit a decision variable.
E.g. we can choose the pixel according to the sign of the variable
NE
EP (xp,yp)
M
Q
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s Algorithm
Implicit equation of the line
The main idea is:
( , ) 0F x y ax by c= + + =
( , ) 0F x y >
( , ) 0F x y <
( , ) 0F x y =Let M=(xp+1,yp+1/2)
I can evaluate the sign of F(M) using the decision variable d=F(M)
Hence:
0 choose NE( ) 0 choose E
0 choose Ed F M
<⎧⎪= =⎨⎪ >⎩
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm
Starting from
If E is chosen, for the new pixel it yields that
but
( ) ( )( , ) 0
( ) ( 1, 1/ 2) 1 1/ 2p p p p
F x y ax by c
d F M F x y a x b y c
= + + =
= = + + = + + + +
( ) ( )( 2, 1/ 2) 2 1/ 2new p p p pd F x y a x b y c= + + = + + + +
( ) ( )( 1, 1/ 2) 1 1/ 2old p p p pd F x y a x b y c= + + = + + + +
new oldd d a= +
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm
Hence if we chose E we can evaluate the new value of d simply adding a to the previous value
If we had chosen NE:
Enew oldd d a= +
( ) ( )( 2, 3 / 2) 2 3/ 2new p p p pd F x y a x b y c= + + = + + + +
NEnew oldd d a b= + +
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm
It sounds good… but a and b are not integer in general…If we consider the explicit equation of the line in this form:
Now a and b are integer!
1 2
1 2
dx x xdxy x B wheredy y ydy
= −⎧= + ⎨ = −⎩
( ), 0F x y dy x dx y B dx
a dyb dxc B dx
= ⋅ − ⋅ + ⋅ =
=⎧⎪ = −⎨⎪ = ⋅⎩
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm
Calculate the initial value in the starting extremum
To avoid the fraction
Hence to evaluate dnew for any step a simple addition is needed
( ) ( )
( )
1 1 1 1
1 1
1 1
( 1, 1/ 2) 1 1/ 2/ 2
, / 2/ 2
F x y a x b y cax by c a bF x y a ba b
+ + = + + + +
= + + + +
= + +
= +
( )( , ) 2F x y ax by c= + +
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm
MidpointLine(int x0, int y0, int x1, int y1, int value){
int dx, dy, incrE, incrNE, d, x, y;
dy = y1-y0;dx = x1-x0;d = 2*dy-dx;incrE = 2*dy;incrNE = 2*(dy-dx);x = x0;y = y0;
while (x < x1 ) {if ( d <= 0 ) {
d = d+incrE;x++;
} else {d = d+incrNE;x++;y++;
}WritePixel(x, y, value);
}}
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm – Additional issues
Endpoint order
• Ensure that a line from P1 to P0 contains the same set of the line from P0 to P1
• The appearance of the line has to be independent of endpoints order
• Pixel choice is dependent on the order only when d=0 (the line passes exactly through the midpoint)
• Going left to right we chose E
• Hence going right to left we chose SW (by symmetry we would have chosen W)
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm – Additional issues
Vertical Clipping
• What happens if the line has been analitycally clipped?• Suppose line clipped at x=xmin
E
NE
M(xmin,Round(mxmin+B))
(xmin, (mxmin+B))
Start from (xmin,Round(mxmin+B)) as the line was unclipped
Do not clip the line at x=xmin and the scan convert the line from (xmin,Round(mxmin+B)) to (x1,y1) !!
y = ymin
x = xmin
Image Analysis and Synthesis - A.Y. 2006/2007
Bresenham’s algorithm – Additional issues
Horizontal Clipping
• What happens if the line intersects the horizontal border?• Suppose line clipped at y=ymin
• There are multiple pixel lying on the scan line y=ymin
• If scan conversion is applied at intersection point with y=ymin the line starts from A discarding leftmost pixels
• Hence find intersection with y=ymin -1/2 and round up the x value obtaining pixel B (Round(xymin-1/2), ymin)
y = ymin
x = xmin
y = ymin-1/2
B A
y = ymin-1
Image Analysis and Synthesis - A.Y. 2006/2007
References
Principles of Computer Graphics (2nd Edition)J.D. Foley, A. Van-Dam, S.K. Feiner and J.F. Hughes - Addison-Wesley, 1990.
Principles of Digital Image SynthesisAndrew S. Glassner - Morgan Kaufmann,1995
Image Analysis and Synthesis - A.Y. 2006/2007
Image Analysis and Synthesis - A.Y. 2006/2007