Это может быть перебор, но очень хорошим методом поиска модели из данных является метод RANSAC. Rougly, это работает так:
1. Let M* be the best model so far. // At the begining there is no such model.)
2. Let D be the set of data points. // In your case it is a set of x-y pairs.)
3. Until a stopping condition is not met:
Randomly pick a number of elements from D. // The number of elements is such number that allows a model be constructed from them In your case it will be (my idea) 2 elements.
Construct a model M from picked elements. // In your case the 2 picked elements will be the corners of the rectangle.
Count a cost of M c(M). // Cost is some value that represents how good (or bad) the model is. In your case it could be the number of points that are no further than some threshold from the lines of the rectangle.
If c(M) > c(M*) then M* := M // If the c() value represents how bad the model is, the relation would be opposite, i.e. c(M) < c(M*).
Этот метод отлично подходит, когда данные шумная может работать даже в ситуациях (некоторые из них), где большинство точек данных не относится к модели, которую мы ищем для.
Состояние остановки - сложная часть. Это может быть что угодно, от фиксированного числа итераций, до бега, пока у нас не будет времени.
Here - это короткая презентация в формате PDF из моего университета (но не мной), которая более подробно описывает бит и дает некоторые формулы, относящиеся к RANSAC.
Спасибо большое @Jan Žegklitz! – user3605837