Angesichts eines Bildes mit einigen unregelmäßigen Objekten möchte ich ihren individuellen Durchmesser finden.Wie findet man den Durchmesser von Objekten mit der Bildverarbeitung in Python?
Thanks to this answer kann ich die Objekte identifizieren. Ist es jedoch möglich, den maximalen Durchmesser der im Bild gezeigten Objekte zu messen?
Ich habe in die scipy-ndimage
Dokumentation geschaut und habe keine dedizierte Funktion gefunden.
-Code für Objektidentifikation:
import numpy as np
from scipy import ndimage
from matplotlib import pyplot as plt
# generate some lowpass-filtered noise as a test image
gen = np.random.RandomState(0)
img = gen.poisson(2, size=(512, 512))
img = ndimage.gaussian_filter(img.astype(np.double), (30, 30))
img -= img.min()
img /= img.max()
# use a boolean condition to find where pixel values are > 0.75
blobs = img > 0.75
# label connected regions that satisfy this condition
labels, nlabels = ndimage.label(blobs)
# find their centres of mass. in this case I'm weighting by the pixel values in
# `img`, but you could also pass the boolean values in `blobs` to compute the
# unweighted centroids.
r, c = np.vstack(ndimage.center_of_mass(img, labels, np.arange(nlabels) + 1)).T
# find their distances from the top-left corner
d = np.sqrt(r*r + c*c)
# plot
fig, ax = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(10, 5))
ax[0].imshow(img)
ax[1].hold(True)
ax[1].imshow(np.ma.masked_array(labels, ~blobs), cmap=plt.cm.rainbow)
for ri, ci, di in zip(r, c, d):
ax[1].annotate('', xy=(0, 0), xytext=(ci, ri),
arrowprops={'arrowstyle':'<-', 'shrinkA':0})
ax[1].annotate('d=%.1f' % di, xy=(ci, ri), xytext=(0, -5),
textcoords='offset points', ha='center', va='top',
fontsize='x-large')
for aa in ax.flat:
aa.set_axis_off()
fig.tight_layout()
plt.show()
Im Allgemeinen ist es kein triviales Problem. OpenCV (mit Python-Bindings) hat 'minEnclosingCircle', aber ich weiß nichts Ähnliches in ndimage. Siehe auch beispielsweise https://en.wikipedia.org/wiki/Smallest-circle_problem – tom10