使用 OpenCV fitEllipse,我想执行 3900 × 3072 png 图像(Python 3.6.7)的椭圆拟合。
作为输入图像,我使用 AMD 14 目录中的图像,该目录分布在以下站点。
最终,我想创建一个蒙版,通过将椭圆形状拟合到非零像素值的最大连接区域来从感兴趣的中心区域 (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'
守着星空守着你
相关分类