Python 基于fitEllipse的图像椭圆拟合

Python 基于fitEllipse的图像椭圆拟合,python,python-3.x,opencv,Python,Python 3.x,Opencv,使用OpenCV fitEllipse,我想对3900×3072 png图像(Python 3.6.7)执行椭圆拟合 作为输入图像,我使用的是AMD14目录中的图像,该目录分布在 最后,我想创建一个遮罩,通过将椭圆形状拟合到非零像素值的最大连接区域,从感兴趣的中心区域(ROI)裁剪图像 import numpy as np import cv2 def create_ellipse(thresh,cnt): ellipse = cv2.fitEllipse(cnt) #ここでエラーが出

使用OpenCV fitEllipse,我想对3900×3072 png图像(Python 3.6.7)执行椭圆拟合

作为输入图像,我使用的是AMD14目录中的图像,该目录分布在

最后,我想创建一个遮罩,通过将椭圆形状拟合到非零像素值的最大连接区域,从感兴趣的中心区域(ROI)裁剪图像

import numpy as np
import cv2

def create_ellipse(thresh,cnt):
    ellipse = cv2.fitEllipse(cnt) #ここでエラーが出る
    thresh = cv2.ellipse(thresh,ellipse,(0,255,255),2) 
    return thresh

def rgb_to_gray(src):
     b, g, r = src[:,:,0], src[:,:,1], src[:,:,2]
     gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
     return gray   


im = cv2.imread('AMD1.png')

gray = rgb_to_gray(im)
gray = cv2.convertScaleAbs(gray)

cv2.imwrite("gray.png", gray)

height = im.shape[0]
width = im.shape[1]
cnt = (width/2, height/2)

im = np.float32(im)
thresh = create_ellipse(im,cnt)
虽然我执行了上面的代码,但是我得到了一个类似下面的错误

error                                     Traceback (most recent call last)
<ipython-input-46-9f83929ab8df> in <module>()
     37 im = np.float32(im)
---> 38 thresh = create_ellipse(im,cnt)

<ipython-input-46-9f83929ab8df> in create_ellipse(thresh, cnt)
      3 
      4 def create_ellipse(thresh,cnt):
----> 5     ellipse = cv2.fitEllipse(cnt) 
      6     
      7     #ex : cv2.ellipse(img, (width/2-200, height/2-300), (100, 50), 0, 0, 360, (0, 0, 255), 10)

error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/shapedescr.cpp:305: error: (-215:Assertion failed) n >= 0 && (depth == CV_32F || depth == CV_32S) in function 'fitEllipse'
错误回溯(最近一次调用上次)
在()
37 im=np.32(im)
--->38阈值=创建椭圆(im,cnt)
在创建椭圆中(阈值,cnt)
3.
4 def创建_椭圆(阈值,cnt):
---->5椭圆=cv2。fitEllipse(cnt)
6.
例如:cv2.椭圆(img,(宽度/2-200,高度/2-300),(100,50),0,0,360,(0,0,255),10)
错误:函数“fitEllipse”中的OpenCV(3.4.3)/io/OpenCV/modules/imgproc/src/shapedscr.cpp:305:错误:(-215:断言失败)n>=0&(深度==CV_32F | |深度==CV_32S)

您必须先提取轮廓
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
#!/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)