This notebooks measures the runtime of each functionality.
In [1]:
import numpy as np
import cv2
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
import salientregions as sr
import cProfile
In [2]:
%pylab inline
In [3]:
#Load the image
path_to_image = 'images/graffiti.jpg'
img = cv2.imread(path_to_image)
sr.show_image(img)
In [4]:
%%timeit
#Time: creation of the detector
det = sr.SalientDetector(SE_size_factor=0.20,
lam_factor=4)
In [5]:
det = sr.SalientDetector(SE_size_factor=0.20,
lam_factor=4)
In [6]:
%%timeit
#Time: detect all regions in color image
regions = det.detect(img,
find_holes=True,
find_islands=True,
find_indentations=True,
find_protrusions=True,
visualize=False)
In [7]:
cProfile.run('det.detect(img, find_holes=True, find_islands=True, find_indentations=True, \
find_protrusions=True, visualize=False)')
In [8]:
%%timeit
#Only holes and islands
regions = det.detect(img,
find_holes=True,
find_islands=True,
find_indentations=False,
find_protrusions=False,
visualize=False)
In [9]:
lam_factor = 3
area_factor_large = 0.001
area_factor_verylarge = 0.1
lam = 50
connectivity = 4
weights=(0.33,0.33,0.33)
grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
In [10]:
%%timeit
#Creation of the binarizer
binarizer = sr.DatadrivenBinarizer(area_factor_large=area_factor_large, area_factor_verylarge=area_factor_verylarge,
lam=lam, weights=weights, connectivity=connectivity)
In [11]:
binarizer = sr.DatadrivenBinarizer(area_factor_large=area_factor_large, area_factor_verylarge=area_factor_verylarge,
lam=lam, weights=weights, connectivity=connectivity)
In [12]:
%%timeit
#The binarization
binarized = binarizer.binarize(grayscale, visualize=False)
In [13]:
cProfile.run('binarizer.binarize(grayscale, visualize=False)')
In [14]:
binarized = binarizer.binarize(grayscale, visualize=False)
regions = det.detect(img,
find_holes=True,
find_islands=True,
find_indentations=True,
find_protrusions=True,
visualize=False)
se = det.SE
In [15]:
area_factor=0.05
In [16]:
%%timeit
detector = sr.BinaryDetector(se, lam, area_factor, connectivity)
regions = detector.detect(binarized, visualize=False)
In [17]:
detector = sr.BinaryDetector(se, lam, area_factor, connectivity)
cProfile.run('detector.detect(binarized, visualize=False)')
In [18]:
#Only holes and islands
detector = sr.BinaryDetector(se, lam, area_factor, connectivity)
cProfile.run('detector.detect(binarized, find_indentations=False, \
find_protrusions=False, visualize=False)')
In [19]:
mser = cv2.MSER_create()
In [20]:
%%timeit
regions = mser.detectRegions(img, None)
In [21]:
cProfile.run('mser.detectRegions(img, None)')
The tophat operation (for protrusions and indentations) is the bottleneck, takes 4.9 (of in total 5.7) seconds.
The binarization is also quite slow because the function connectedComponentsWithStats
is called for every threshold level.
The MSER detection is somewhat faster for a color image (about 2-3 times as fast).
In [22]:
%timeit cv2.morphologyEx(binarized, cv2.MORPH_TOPHAT, se)
In [23]:
%timeit cv2.morphologyEx(binarized, cv2.MORPH_OPEN, se)
In [24]:
%timeit cv2.erode(binarized, se)
%timeit cv2.dilate(binarized, se)
In [ ]: