Skip to content Skip to sidebar Skip to footer

Ellipse Fitting For Images Using Fitellipse

Using OpenCV fitEllipse, I'd like to perform ellipse Fitting of 3900 × 3072 png images (Python 3.6.7). As the input image, I'm using the images inside the AMD 14 directory, which

Solution 1:

cv2.fitEllipse(...)
    fitEllipse(points) -> retval
    .   @brief Fits an ellipse around a set of 2D points.
    .
    .   The function calculates the ellipse that fits (in a least-squares sense) a set of 2D points best of
    .   all. It returns the rotated rectangle in which the ellipse is inscribed. The first algorithm described by@cite Fitzgibbon95
    .   is used. Developer should keep in mind that it is possible that the returned
    .   ellipse/rotatedRect data contains negative indices, due to the data points being close to the
    .   border of the containing Mat element.
    .
    .   @param points Input 2D point set, stored in std::vector\<\> or Mat

Here is a demo:

#!/usr/bin/python3# 2018/12/25

import cv2
img = cv2.imread('test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)

## findContours, then fitEllipse for each contour.## 使用 cv2.findContours(...)[-2:] 对 2.x|3.x|4.x 兼容
cnts, hiers = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]

for cnt in cnts:
    ellipse = cv2.fitEllipse(cnt)
    cv2.ellipse(img, ellipse, (255,0, 255), 1, cv2.LINE_AA)

cv2.imwrite("dst.png", img)

enter image description here

Post a Comment for "Ellipse Fitting For Images Using Fitellipse"