Linear regression in OpenCV

Before trying out linear regression on a real-life dataset, let's understand how we can use the cv2.fitLine function to fit a line to a 2D or 3D point set:

  1. Let's start by generating some points. We will generate them by adding noise to the points lying on the line :
In [1]: import cv2...     import numpy as np...     import matplotlib.pyplot as plt...     from sklearn import linear_model...     from sklearn.model_selection import train_test_split...     plt.style.use('ggplot')...     %matplotlib inlineIn [2]: x = np.linspace(0,10,100)...     y_hat = x*5+5...     np.random.seed(42)...     y = x*5 + 20*(np.random.rand(x.size) - 0.5)+5
  1. We can also ...

Get Machine Learning for OpenCV 4 - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.