SMIL  0.11.3-dev
Mesures on blobs

Blobs More...

Detailed Description

Blobs

Blobs are just maps (or dictionaries in Python) to represent disjoint regions in image.

blobs are probably the best way to handle measures on regions of images, as calling a function to evaluate a measure on a blobs map is equivalent to call the equivalent function for each blob. E.g. calling blobsArea() is equivalent to call area() for each blob in the map.

To handle blobs, you need to have a labeled image, generated by some method of segmentation (e.g. watershed()), and create them thanks to computeBlobs().

Example

import smilPython as sp
im = sp.Image("balls.png")
iml = sp.Image(im)
# this is just a binary image - no need to segment
sp.label(im, iml)
iml.showLabel()
# create blobs structure
blobs = sp.computeBlobs(iml)
# evaluate some measures :
areas = sp.blobsArea(blobs)
barys = sp.blobsBarycenter(im, blobs)
# print areas and barycenters of each region
print("{:3s} - {:>6s} - {:>13s}".format("ID", "Area", "Barycenter"))
for k in blobs.keys():
bary = barys[k]
print("{:3d} - {:6.0f} - {:6.0f} {:6.0f}".format(k, areas[k], bary[0], bary[1]))
+ Collaboration diagram for Mesures on blobs:

Classes

struct  PixelSequence
 Start offset and length of pixels in an image. More...
 
struct  Blob
 List of offset and size of line contiguous pixels. More...
 

Functions

template<class T >
map< T, BlobcomputeBlobs (const Image< T > &imIn, bool onlyNonZero=true)
 Create a map of blobs from a labeled image. More...
 
template<class labelT , class T >
RES_T drawBlobs (map< labelT, Blob > &blobs, Image< T > &imOut, T blobsValue=ImDtTypes< T >::max(), bool fillFirst=true, T defaultValue=T(0))
 Represent Blobs in an image. More...
 
template<class labelT , class T >
RES_T drawBlobs (map< labelT, Blob > &blobs, map< labelT, T > &lut, Image< T > &imOut, bool fillFirst=true, T defaultValue=T(0))
 Represent Blobs in an image with a lookup map. More...
 
template<class T >
map< T, double > blobsArea (const Image< T > &imLbl, const bool onlyNonZero=true)
 blobsArea() - Calculate the area of each region in a labeled image More...
 
template<class labelT >
map< labelT, double > blobsArea (map< labelT, Blob > &blobs)
 blobsArea() - Measure areas from a pre-generated Blob map (faster). More...
 
template<class T , class labelT >
map< labelT, double > blobsVolume (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsVolume() - Measure the sum of values of each blob in imIn. More...
 
template<class T , class labelT >
map< labelT, T > blobsMinVal (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsMinVal() - Measure the minimum value of each blob in imIn. More...
 
template<class T , class labelT >
map< labelT, T > blobsMaxVal (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsMaxVal() - Measure the maximum value of each blob in imIn. More...
 
template<class T , class labelT >
map< labelT, vector< T > > blobsRangeVal (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsRangeVal() - Measure the min and max values of each blob in imIn. More...
 
template<class T , class labelT >
map< labelT, Vector_doubleblobsMeanVal (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsMeanVal() - Measure the mean value and the std dev. More...
 
template<class T , class labelT >
map< labelT, vector< T > > blobsValueList (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsValueList() - Measure the list of values of each blob in imIn. More...
 
template<class T , class labelT >
map< labelT, T > blobsModeVal (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsModeVal() - Measure the mode value of imIn in each blob. More...
 
template<class T , class labelT >
map< labelT, T > blobsMedianVal (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsMedianVal() - Measure the median value of imIn in each blob. More...
 
template<class T >
map< T, Vector_doubleblobsBarycenter (const Image< T > &imLbl, const bool onlyNonZero=true)
 blobsBarycenter() - Measure barycenter of a labeled image. More...
 
template<class T , class labelT >
map< labelT, Vector_doubleblobsBarycenter (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsBarycenter() - Measure the barycenter of each blob in imIn. More...
 
template<class T >
map< T, vector< size_t > > blobsBoundBox (const Image< T > &imLbl, const bool onlyNonZero=true)
 blobsBoundBox() - Measure bounding boxes of labeled image. More...
 
template<class T , class labelT >
map< labelT, vector< size_t > > blobsBoundBox (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsBoundBox() - Measure bounding boxes of each blob (faster with blobs pre-generated). More...
 
template<class T , class labelT >
map< labelT, Vector_doubleblobsMoments (const Image< T > &imIn, map< labelT, Blob > &blobs, bool central=false)
 blobsMoments() - Measure blobs image moments (faster with blobs pre-generated). More...
 
template<class T >
map< T, Vector_doubleblobsMoments (const Image< T > &imLbl, const bool onlyNonZero=true, bool central=false)
 blobsMoments() - Measure image moments of each label. More...
 
template<class T , class labelT >
map< labelT, double > blobsEntropy (const Image< T > &imIn, map< labelT, Blob > &blobs)
 blobsEntropy() - Measure blobs entropy. More...
 

Function Documentation

◆ computeBlobs()

map<T, Blob> smil::computeBlobs ( const Image< T > &  imIn,
bool  onlyNonZero = true 
)

Create a map of blobs from a labeled image.

Parameters
[in]imIn: input labeled image
[in]onlyNonZero: ignore regions whose label is 0
Returns
a map of pairs <label, blob>

◆ drawBlobs() [1/2]

RES_T smil::drawBlobs ( map< labelT, Blob > &  blobs,
Image< T > &  imOut,
blobsValue = ImDtTypes<T>::max(),
bool  fillFirst = true,
defaultValue = T(0) 
)

Represent Blobs in an image.

Parameters
[in]blobs: input blobs map
[in]imOut: output image
[in]blobsValue: value to assign to each blob
[in]fillFirst: fill the background before beginning
[in]defaultValue: default value for the background (default : 0)
Note
If blobsValue == 0, each blobs is represented with its label value.

◆ drawBlobs() [2/2]

RES_T smil::drawBlobs ( map< labelT, Blob > &  blobs,
map< labelT, T > &  lut,
Image< T > &  imOut,
bool  fillFirst = true,
defaultValue = T(0) 
)

Represent Blobs in an image with a lookup map.

Parameters
[in]blobs: input blobs map
[in]lut: lookup table with value to assign to each blob
[in]imOut: output image
[in]fillFirst: fill the background before beginning
[in]defaultValue: default value for the background (default : 0)

◆ blobsArea() [1/2]

map<T, double> smil::blobsArea ( const Image< T > &  imLbl,
const bool  onlyNonZero = true 
)

blobsArea() - Calculate the area of each region in a labeled image

Parameters
[in]imLbl: input labeled image
[in]onlyNonZero: skip a blob having a null label
Returns
a map of pairs <label, double> with the area of each blob.

Example

from smilPython import *
# Load an image
imIn = Image("https://smil.cmm.minesparis.psl.eu/images/DNA_small.png")
imIn.show()
imThr = Image(imIn)
threshold(imIn, imThr)
imLbl = Image(imIn, "UINT16")
label(imThr, imLbl)
imLbl.showLabel()
# Bounding boxes
bboxes = blobsBoundBox(imLbl)
imRec = Image(imIn)
drawRectangles(imRec, bboxes)
imIn.getViewer().drawOverlay(imRec)
# Blobs measures
blobs = computeBlobs(imLbl)
# areas
areas = blobsArea(imLbl, blobs) # equivalent but faster than measAreas(imLbl)
# barycenters
barys = blobsBarycenter(imLbl, blobs)
# volume of blobs in imIn
vols = blobsVolume(imIn, blobs)
print("Label\tarea\tvolume\tbarycenter (x,y)")
for lbl in blobs.keys():
print(str(lbl) + "\t" + str(areas[lbl]) + "\t" + str(vols[lbl]) + "\t" + str(barys[lbl]))

◆ blobsArea() [2/2]

map<labelT, double> smil::blobsArea ( map< labelT, Blob > &  blobs)

blobsArea() - Measure areas from a pre-generated Blob map (faster).

Parameters
[in]blobs: input Blob map
Returns
a map of pairs <label, double> with the area of each blob.

◆ blobsVolume()

map<labelT, double> smil::blobsVolume ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsVolume() - Measure the sum of values of each blob in imIn.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, double> with the volume (sum of pixel values) in each blob.

◆ blobsMinVal()

map<labelT, T> smil::blobsMinVal ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsMinVal() - Measure the minimum value of each blob in imIn.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, T> with the min image value in each blob.

◆ blobsMaxVal()

map<labelT, T> smil::blobsMaxVal ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsMaxVal() - Measure the maximum value of each blob in imIn.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, T> with the max image value in each blob.

◆ blobsRangeVal()

map<labelT, vector<T> > smil::blobsRangeVal ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsRangeVal() - Measure the min and max values of each blob in imIn.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, <T, T>> with a vector with the min and max pixel values in each blob.

◆ blobsMeanVal()

map<labelT, Vector_double> smil::blobsMeanVal ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsMeanVal() - Measure the mean value and the std dev.

of each blob in imIn.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, <double, double>> with a vector with the mean and standard deviation pixel values in each blob.

◆ blobsValueList()

map<labelT, vector<T> > smil::blobsValueList ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsValueList() - Measure the list of values of each blob in imIn.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, vector<T>> with the list of pixel values in each blob.

◆ blobsModeVal()

map<labelT, T> smil::blobsModeVal ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsModeVal() - Measure the mode value of imIn in each blob.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, T> with the mode value in each blob.

◆ blobsMedianVal()

map<labelT, T> smil::blobsMedianVal ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsMedianVal() - Measure the median value of imIn in each blob.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, T> with the median value in each blob.

◆ blobsBarycenter() [1/2]

map<T, Vector_double> smil::blobsBarycenter ( const Image< T > &  imLbl,
const bool  onlyNonZero = true 
)

blobsBarycenter() - Measure barycenter of a labeled image.

Parameters
[in]imLbl: input labeled image
[in]onlyNonZero: skip a blob having a null label
Returns
a map of pairs <label, Vector<double>> with the coordinates of the barycenter of each blob.

Example

from smilPython import *
# Load an image
imIn = Image("https://smil.cmm.minesparis.psl.eu/images/DNA_small.png")
imIn.show()
imThr = Image(imIn)
threshold(imIn, imThr)
imLbl = Image(imIn, "UINT16")
label(imThr, imLbl)
imLbl.showLabel()
# Bounding boxes
bboxes = blobsBoundBox(imLbl)
imRec = Image(imIn)
drawRectangles(imRec, bboxes)
imIn.getViewer().drawOverlay(imRec)
# Blobs measures
blobs = computeBlobs(imLbl)
# areas
areas = blobsArea(imLbl, blobs) # equivalent but faster than measAreas(imLbl)
# barycenters
barys = blobsBarycenter(imLbl, blobs)
# volume of blobs in imIn
vols = blobsVolume(imIn, blobs)
print("Label\tarea\tvolume\tbarycenter (x,y)")
for lbl in blobs.keys():
print(str(lbl) + "\t" + str(areas[lbl]) + "\t" + str(vols[lbl]) + "\t" + str(barys[lbl]))

◆ blobsBarycenter() [2/2]

map<labelT, Vector_double> smil::blobsBarycenter ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsBarycenter() - Measure the barycenter of each blob in imIn.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, Vector<double>> with the coordinates of the barycenter of each blob.

◆ blobsBoundBox() [1/2]

map<T, vector<size_t> > smil::blobsBoundBox ( const Image< T > &  imLbl,
const bool  onlyNonZero = true 
)

blobsBoundBox() - Measure bounding boxes of labeled image.

Parameters
[in]imLbl: input labeled image
[in]onlyNonZero: skip a blob having a null label
Returns
a map of pairs <label, Vector<size_t>> with the coordinates of the bounding box of each label.

◆ blobsBoundBox() [2/2]

map<labelT, vector<size_t> > smil::blobsBoundBox ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsBoundBox() - Measure bounding boxes of each blob (faster with blobs pre-generated).

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, Vector<size_t>> with the coordinates of the bounding box of each label.

◆ blobsMoments() [1/2]

map<labelT, Vector_double> smil::blobsMoments ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs,
bool  central = false 
)

blobsMoments() - Measure blobs image moments (faster with blobs pre-generated).

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
[in]central: moments are evaluated with respect to the blob barycenter
Returns
a map of pairs <label, Vector<double>> with the first and second order image moments of each label.
See also
measMoments()

Example : Calculating moments

import smilPython as sp
# How to print moments
def printBlobMoments(m, title=""):
print(title.center(90))
hdr = ["m00", "m10", "m01", "m11", "m20", "m02"]
s = " " * 5
for h in hdr:
s += h.rjust(16)
print(s)
for label in m.keys():
s = '{:3} :'.format(label)
for v in m[label]:
s += ' {:14.2f}'.format(v)
print(s)
# Serious work begins here
#
binaryImage = False
if binaryImage:
imageName = "https://smil.cmm.minesparis.psl.eu/images/balls.png"
imo = sp.Image(imageName)
iml = sp.Image(imo, 'UINT16')
# normalize binary image values
imo /= 255
sp.label(imo, iml)
else:
imageName = "https://smil.cmm.minesparis.psl.eu/images/tools.png"
imo = sp.Image(imageName)
imThr = sp.Image(imo)
sp.topHat(imo, imThr, sp.hSE(20))
sp.threshold(imThr, imThr)
iml = sp.Image(imo, "UINT16")
sp.label(imThr, iml)
# create blobs
blobs = sp.computeBlobs(iml)
print("=== > Image : ", imageName, "\n")
# Calculate not centered moments
moments = sp.blobsMoments(imo, blobs, False)
printBlobMoments(moments, "Not centered moments")
print("=" * 102)
# Calculate centered moments
moments = sp.blobsMoments(imo, blobs, True)
printBlobMoments(moments, "Centered moments")

Example : Getting blob orientation in space

from smilPython import *
from math import *
# Load an image
imIn = Image("https://smil.cmm.minesparis.psl.eu/images/tools.png")
imIn.show()
imThr = Image(imIn)
topHat(imIn, imThr, hSE(20))
threshold(imThr, imThr)
imLbl = Image(imIn, "UINT16")
label(imThr, imLbl)
imLbl.showLabel()
def fitRectangle(mat):
m00, m10, m01, m11, m20, m02 = mat
if m00 == 0:
return 0, 0, 0, 0, 0
# COM
xc = int (m10 / m00)
yc = int (m01 / m00)
# centered matrix (central moments)
u00 = m00
u20 = m20 - m10**2 / m00
u02 = m02 - m01**2 / m00
u11 = m11 - m10 * m01 / m00
# eigen values
delta = 4 * u11**2 + (u20 - u02)**2
I1 = (u20 + u02 + sqrt(delta)) / 2
I2 = (u20 + u02 - sqrt(delta)) / 2
theta = 0.5 * atan2(-2 * u11, (u20 - u02))
# Equivalent rectangle
# I1 = a**2 * S / 12, I2 = b**2 * S / 12
a = int (sqrt(12 * I1 / u00))
b = int (sqrt(12 * I2 / u00))
return xc, yc, a, b, theta
# Compute Blobs
blobs = computeBlobs(imLbl)
# Compute Inertia Matrices
mats = blobsMoments(imIn, blobs)
bboxes = blobsBoundBox(imLbl)
imDraw = Image(imIn)
print("Label\tA\tB\tTheta")
for b in blobs.keys():
mat = xc, yc, A, B, theta = fitRectangle(mats[b])
print(str(b) + "\t" + str(A) + "\t" + str(B) + "\t" + str(theta))
dx = A / 2 * cos(pi - theta)
dy = A / 2 * sin(pi - theta)
drawLine(imDraw, int(xc - dx), int(yc - dy), int(xc + dx), int(yc + dy), b)
dx = B / 2 * sin(theta)
dy = B / 2 * cos(theta)
drawLine(imDraw, int(xc - dx), int(yc - dy), int(xc + dx), int(yc + dy), b)
imIn.getViewer().drawOverlay(imDraw)

◆ blobsMoments() [2/2]

map<T, Vector_double> smil::blobsMoments ( const Image< T > &  imLbl,
const bool  onlyNonZero = true,
bool  central = false 
)

blobsMoments() - Measure image moments of each label.

Parameters
[in]imLbl: input labeled image
[in]onlyNonZero: skip a blob having a null label
[in]central: moments are evaluated with respect to the blob barycenter
Returns
a map of pairs <label, Vector<double>> with the image moments of each label.
Note
Whenever possible it's better to privilegiate the other version of this function, as you have more control on what you get.
See also
measMoments()

◆ blobsEntropy()

map<labelT, double> smil::blobsEntropy ( const Image< T > &  imIn,
map< labelT, Blob > &  blobs 
)

blobsEntropy() - Measure blobs entropy.

Parameters
[in]imIn: input labeled image
[in]blobs: input Blob map
Returns
a map of pairs <label, double> with the entropy of the image in each blob.
See also
measEntropy()
smil::blobsBarycenter
map< T, Vector_double > blobsBarycenter(const Image< T > &imLbl, const bool onlyNonZero=true)
blobsBarycenter() - Measure barycenter of a labeled image.
Definition: DBlobMeasures.hpp:209
smil::blobsVolume
map< labelT, double > blobsVolume(const Image< T > &imIn, map< labelT, Blob > &blobs)
blobsVolume() - Measure the sum of values of each blob in imIn.
Definition: DBlobMeasures.hpp:91
smil::blobsMoments
map< labelT, Vector_double > blobsMoments(const Image< T > &imIn, map< labelT, Blob > &blobs, bool central=false)
blobsMoments() - Measure blobs image moments (faster with blobs pre-generated).
Definition: DBlobMeasures.hpp:280
smil::drawOverlay
RES_T drawOverlay(const Image< T > &imToDraw, Image< T > &imOut)
Draw overlay.
Definition: DImage.hpp:514
smil::computeBlobs
map< T, Blob > computeBlobs(const Image< T > &imIn, bool onlyNonZero=true)
Create a map of blobs from a labeled image.
Definition: DBlob.hpp:98
smil::drawRectangles
RES_T drawRectangles(Image< T > &imOut, const map< MapT, vector< size_t >> &coordsVectMap, bool fill=false)
Draw a list of rectangles.
Definition: DImageDraw.hpp:197
smil::topHat
RES_T topHat(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
topHat() - Top-Hat
Definition: DMorphoResidues.hpp:115
smil::blobsArea
map< T, double > blobsArea(const Image< T > &imLbl, const bool onlyNonZero=true)
blobsArea() - Calculate the area of each region in a labeled image
Definition: DBlobMeasures.hpp:60
smil::blobsBoundBox
map< T, vector< size_t > > blobsBoundBox(const Image< T > &imLbl, const bool onlyNonZero=true)
blobsBoundBox() - Measure bounding boxes of labeled image.
Definition: DBlobMeasures.hpp:239
smil::label
size_t label(const Image< T1 > &imIn, Image< T2 > &imOut, const StrElt &se=DEFAULT_SE)
label() - Image labelization
Definition: DMorphoLabel.hpp:554
smil::drawLine
RES_T drawLine(Image< T > &im, int x0, int y0, int x1, int y1, T value=ImDtTypes< T >::max())
Draws a line between two points P0(x0,y0) and P1(x1,y1).
Definition: DImageDraw.hpp:58
smil::threshold
RES_T threshold(const Image< T > &imIn, T minVal, T maxVal, T_out trueVal, T_out falseVal, Image< T_out > &imOut)
threshold() - Image threshold
Definition: DImageHistogram.hpp:269