import numpy as np
import cv2
import sys
import os
sys.path.insert(0, os.path.abspath('..'))

import salientregions as sr
import cProfile

%pylab inline

#Load the image
path_to_image = 'images/graffiti.jpg'
img = cv2.imread(path_to_image)
sr.show_image(img)

%%timeit
#Time: creation of the detector
det = sr.SalientDetector(SE_size_factor=0.20,
                                lam_factor=4)

det = sr.SalientDetector(SE_size_factor=0.20,
                                lam_factor=4)

%%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)

cProfile.run('det.detect(img, find_holes=True, find_islands=True, find_indentations=True, \
                    find_protrusions=True, visualize=False)')

%%timeit
#Only holes and islands
regions = det.detect(img,
                    find_holes=True,
                    find_islands=True,
                    find_indentations=False,
                    find_protrusions=False,
                    visualize=False)

"""
Explanation: Time the functions
This notebooks measures the runtime of each functionality.
End of explanation
"""


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)

%%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)

binarizer = sr.DatadrivenBinarizer(area_factor_large=area_factor_large, area_factor_verylarge=area_factor_verylarge, 
                                           lam=lam, weights=weights, connectivity=connectivity)

%%timeit
#The binarization
binarized = binarizer.binarize(grayscale, visualize=False)

cProfile.run('binarizer.binarize(grayscale, visualize=False)')

"""
Explanation: Binarization
End of explanation
"""


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

area_factor=0.05

%%timeit
detector = sr.BinaryDetector(se, lam, area_factor, connectivity)
regions = detector.detect(binarized, visualize=False)

detector = sr.BinaryDetector(se, lam, area_factor, connectivity)
cProfile.run('detector.detect(binarized, visualize=False)')

#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)')

"""
Explanation: Binary detection
End of explanation
"""


mser = cv2.MSER_create()

%%timeit
regions = mser.detectRegions(img, None)

cProfile.run('mser.detectRegions(img, None)')

"""
Explanation: MSER detection
End of explanation
"""


%timeit cv2.morphologyEx(binarized, cv2.MORPH_TOPHAT, se)

%timeit cv2.morphologyEx(binarized, cv2.MORPH_OPEN, se)

%timeit cv2.erode(binarized, se)
%timeit cv2.dilate(binarized, se)

"""
Explanation: Conclusion
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).
End of explanation
"""


