In [1]:
import numpy as np
from skimage import io
In [2]:
%matplotlib inline
from matplotlib import pyplot as plt

import seaborn as sns
sns.set_style("dark")
In [3]:
I = io.imread('data/maize.jpg')
plt.imshow(I)
Out[3]:
<matplotlib.image.AxesImage at 0x7f759cc3ba90>
In [4]:
traits = {}

Segmentação da planta

In [5]:
import scipy as sp

from skimage import measure, color
from skimage import segmentation as seg
from skimage import morphology as morph
In [6]:
mask = sp.logical_and(I[:,:,1] > 1.1 * I[:,:,0], I[:,:,1] > 192)

foreground = np.zeros_like(mask, dtype=np.uint8)
foreground[mask] = 1

plt.imshow(foreground)
Out[6]:
<matplotlib.image.AxesImage at 0x7f75980e8cd0>
In [7]:
foreground = morph.binary_closing(foreground, morph.disk(1))

plt.imshow(foreground)
Out[7]:
<matplotlib.image.AxesImage at 0x7f759804aa10>
In [8]:
labels, num = measure.label(foreground, background=0, return_num=True)
print(num, ' objetos')
labels = seg.clear_border(labels)

plt.subplot(1,2,1)
plt.imshow(foreground)

plt.subplot(1,2,2)
plt.imshow(color.label2rgb(labels, bg_label=0))
(566, ' objetos')
Out[8]:
<matplotlib.image.AxesImage at 0x7f7591d99bd0>
In [9]:
areas = {region.label:region.area for region in measure.regionprops(labels) if region.area > 100**2}
areas
Out[9]:
{30: 37429}
In [10]:
plant = np.zeros_like(labels, dtype=np.uint8)
plant[labels == 30] = 1

plt.imshow(plant, cmap=plt.cm.viridis)
plt.imshow(I, alpha=0.4)
Out[10]:
<matplotlib.image.AxesImage at 0x7f7591ccf8d0>