02 – Object Modeling Overview Point Selection Bounding Box Line Equation Least Square Line...

26
02 – Object Modeling •Overview •Point Selection •Bounding Box Line Equation •Least Square Line Equation •Conclusions

Transcript of 02 – Object Modeling Overview Point Selection Bounding Box Line Equation Least Square Line...

02 – Object Modeling

•Overview•Point Selection•Bounding Box Line Equation•Least Square Line Equation•Conclusions

Overview

• Assume that we have an image of a table

• How can we create a mathematical model this geometric object?

Overview

• One option is to calculate the the 2D line equations for the visible edges

Overview

• Assume that we have calculated the (x,y) coordinates of the table top edge pixels

X Y12 1513 1715 1916 19. . . . . .

Point Selection

• How can we decide which points belong together on the same line?

• Trial and error point selection algorithm:– Take any two points at random (x1,y1), (x2,y2)– Calculate line equation Ax+By+C=0 where

A=(y1-y2), B=(x2-x1), C=x1 (y2-y1)+y1 (x1-x2)

– Find all points (xi,yi) that are close enough to the line by checking if Axi+Byi+C < threshold

– Remove these points from the list and repeat until there are no points left to consider

Point Selection

Select two points and calculate line equation Ax+By+C=0

Point Selection

Set of points that are close to the first line equation

Point Selection

Select two more points and calculate second line equation

Point Selection

Set of points that are close to the second line equation

Point Selection

Not all line equations will correspond to table top edges

We must ignore these line equations and select two new points

Bounding Box Line Equation

• How can calculate the line equation for the set of points on one line?

• Simple bounding box based algorithm:– Examine the set of points and calculate the

min_x, min_y, max_x, and max_y values– If (max_x-min_x) > (max_y-min_y) then calculate

the line equation using min_x and max_x points– Otherwise calculate the line equation using the

min_y and max_y points– This is robust for horizontal and vertical lines

Bounding Box Line Equation

Calculate bounding box by finding min and max (x,y) values

Bounding Box Line Equation

Define line equation using points with min_x and max_x

Bounding Box Line Equation

void bounding_box_line(float x[], float y[], int count, float &A, float &B)

{

// Calculate min and max (x,y) values

int min_x = 0, max_x = 0, min_y = 0, max_y = 0;

for (int i=1; i<count; i++)

{

if (x[min_x] > x[i]) min_x = i;

if (x[max_x] < x[i]) max_x = i;

if (y[min_y] > y[i]) min_y = i;

if (y[max_y] < y[i]) max_y = i;

}

Bounding Box Line Equation

// Calculate line equation

float size_x = x[max_x] - x[min_x];

float size_y = y[max_y] - y[min_y];

if ((size_x > size_y) && (x[max_x] != x[min_x]))

{ B = (y[max_x] - y[min_x]) / (x[max_x] - x[min_x]);

A = y[min_x] - B * x[min_x]; }

else if (x[max_y] != x[min_y])

{ B = (y[max_y] - y[min_y]) / (x[max_y] - x[min_y]);

A = y[min_y] - B * x[min_y]; }

}

Least Square Line Equation

• The goal of least square line fitting is to obtain a line equation that minimizes the square of the distances between the line and the points

Least Square Line Equation

• To simplify the analysis, we minimize the vertical offsets rather than the perpendicular offsets between the line and points

Least Square Line Equation

• The first step is to quantify vertical offsets using a basic line equation formula

Least Square Line Equation

• Then we take the partial derivatives with respect to the line parameters (a,b)

Least Square Line Equation

• Using S in place of summation, we can write the two equations in matrix form as follows

Least Square Line Equation

• To solve we calculate the 2x2 matrix inverse

Least Square Line Equation

• Multiplying we get expressions for (a,b)

Least Square Line Equation

void least_square_line(float x[], float y[], int count, float &A, float&B)

{

// Calculate sums of (x,y) values

float Sx = 0, Sy = 0, Sxx = 0, Sxy = 0;

for (int i=0; i<count; i++)

{

Sx += x[i];

Sy += y[i];

Sxx += x[i] * x[i];

Sxy += x[i] * y[i];

}

Least Square Line Equation

// Calculate line equation

float denominator = count * Sxx - Sx * Sx;

if (denominator != 0)

{

A = (Sxx * Sy - Sx * Sxy) / denominator;

B = (-Sx * Sy + count * Sxy) / denominator;

}

}

Least Square Line Equation

• For more information on least squares fitting see the Wolfram wiki page below:

http://mathworld.wolfram.com/LeastSquaresFitting.html

Conclusions

• There are many other ways to detect edges in images and calculate line equations– Laplacian zero crossings– Canny edges– Hough transforms

• We will study these computer vision algorithms in more detail later in the class