v9.1.1
Menu

Count Inliers

easyFREERANSACRobust EstimationCV: Model Fitting and Optimization

Implement a function to count the number of inlier points for a given line model, which is crucial in RANSAC for robust model fitting. The task involves determining how many points in a dataset are well-represented by a line model.

The concept of inliers is essential in model fitting as it distinguishes between points that are close to the model and those that are not, known as outliers. A point is considered an inlier if its perpendicular distance to the line, defined by y=mx+by = mx + b, is less than a specified threshold. This distance can be calculated using the formula d=∣mxi−yi+b∣m2+1d = \frac{|mx_i - y_i + b|}{\sqrt{m^2 + 1}}.

To count inliers, follow these steps:

  1. Calculate the perpendicular distance from each point to the line.
  2. Compare this distance with the given threshold. The main equation for the distance is
d=∣mxi−yi+b∣m2+1d = \frac{|mx_i - y_i + b|}{\sqrt{m^2 + 1}}

This technique is widely used in computer vision for line detection and model fitting.

Example

Input
count_inliers([(0,0), (1,1), (1,3)], 1, 0, 0.5)
Output
2
Reasoning
  • Inputs: points is a list of (x,y)(x, y) coordinates, the line is y=mx+by = mx + b with slope m =1= 1 and intercept b =0= 0, and threshold =0.5= 0.5 is the largest perpendicular distance that still counts as "on" the line (strictly less than).
  • With m=1m = 1 the denominator is 12+1=2≈1.414\sqrt{1^2 + 1} = \sqrt{2} \approx 1.414, the same for every point.
  • (0,0)(0,0): d=∣1⋅0−0+0∣2=0<0.5d = \frac{|1 \cdot 0 - 0 + 0|}{\sqrt{2}} = 0 < 0.5, an inlier.
  • (1,1)(1,1): d=∣1⋅1−1+0∣2=0<0.5d = \frac{|1 \cdot 1 - 1 + 0|}{\sqrt{2}} = 0 < 0.5, an inlier; it lies exactly on y=xy = x.
  • (1,3)(1,3): d=∣1−3∣2=22≈1.414>0.5d = \frac{|1 - 3|}{\sqrt{2}} = \frac{2}{\sqrt{2}} \approx 1.414 > 0.5, an outlier.
  • Output: two points pass, so the answer is 2, a plain integer count. Notice that (1,3)(1,3) is 2 units away vertically but only ≈1.414\approx 1.414 away perpendicularly: dividing by m2+1\sqrt{m^2+1} is what turns vertical gap into true distance, and it matters more the steeper the line.

Constraints

  • Return count of inliers

Learn this in

More from CV: Model Fitting and Optimization

solution.py

Test results

Sign in to run your code against the test cases.