简体   繁体   English

如何从索引图像(Python)中找到椭圆坐标?

[英]How to find ellipse coordinates from indexed image (Python)?

I have this kind of image:我有这样的形象:

在此处输入图像描述

which represents concentric ellipses.代表同心椭圆。 This image is contained in a numpy matrix with ones in the index of the different ellipses perimeter and zeros elsewhere.该图像包含在 numpy 矩阵中,其中不同椭圆周长的索引中为 1,其他位置为 0。

Since I want to find the centers of each one of these ellipses, I found these two functions:因为我想找到每个椭圆的中心,所以我找到了这两个函数:

def fitEllipse(x,y):
   x = x[:,np.newaxis]
   y = y[:,np.newaxis]
   D =  np.hstack((x*x, x*y, y*y, x, y, np.ones_like(x)))
   S = np.dot(D.T,D)
   C = np.zeros([6,6])
   C[0,2] = C[2,0] = 2; C[1,1] = -1
   E, V =  eig(np.dot(inv(S), C))
   n = np.argmax(np.abs(E))
   a = V[:,n]
return a

def ellipse_center(a):
   b,c,d,f,g,a = a[1]/2, a[2], a[3]/2, a[4]/2, a[5], a[0]
   num = b*b-a*c
   x0=(c*d-b*f)/num
   y0=(a*f-b*d)/num
return np.array([x0,y0])

which can fit an ellipse curve and find the center of the given ellipse, respectively.它可以拟合椭圆曲线并分别找到给定椭圆的中心。 The problem is that If I can able to draw by myself different ellipses "objects" I can know what the coordinates of the different points are.问题是,如果我能够自己绘制不同的椭圆“对象”,我就可以知道不同点的坐标是什么。 But in my case I have only access to the index where the values are ones, without knowing what indexes belong to a certain ellipse and what indexes to another..但在我的情况下,我只能访问值为 1 的索引,而不知道哪些索引属于某个椭圆以及哪些索引属于另一个椭圆。

So is there a way I can separate those different ellipses, individually storing their index coordinates?那么有没有一种方法可以分离这些不同的椭圆,分别存储它们的索引坐标?

Finally, the idea would be to fit an ellipse to all of the these ellipse objects and find their centers.最后,想法是为所有这些椭圆对象拟合一个椭圆并找到它们的中心。 Thanks in advance:)提前致谢:)

So is there a way I can separate those different ellipses, individually storing their index coordinates?那么有没有一种方法可以分离这些不同的椭圆,分别存储它们的索引坐标?

You can use scikit-image label to label your ellipses like so:您可以像这样使用 scikit-image label到 label 椭圆:

import skimage.io
import skimage.measure
import numpy as np

im = (skimage.io.imread("so67279689.png", as_gray=True)>.3).astype(int)
labeled_im, n = skimage.measure.label(im, return_num=True)

and then access ellipse number 6 for instance as:然后访问 6 号椭圆,例如:

el6 = np.where(labeled_im==6, im, 0)

In total you'll get labels 1 ... n , label 0 is the background.总共你会得到标签1 ... n , label 0是背景。

(this is the image I copied from your question for testing: (这是我从您的问题中复制的用于测试的图像:

在此处输入图像描述 ) )

To visualize the labeled ellipses:要可视化标记的椭圆:

import matplotlib.pyplot as plt
fig,ax=plt.subplots(3,5)
for i in range(n):
    ax.flat[i].imshow(np.where(labeled_im==i+1, im, 0))
    ax.flat[i].set_title(f"{i+1}")
    ax.flat[i].axis("off")
ax.flat[13].axis("off")
ax.flat[14].axis("off")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM