#@ ImagePlus Image
#@ int (Label="Delta (relative difference between threshold levels)",min=0) Delta
#@ int (Label="Minimum region area",min=0,value=500) MinArea
#@ int (Label="Maximum region area",min=0,value=2000) MaxArea
#@ float (Label="Maximum variation of stability for the region",min=0,value=1) MaxVar
#@ String (Label = "Identify bright/dark regions", choices={"Both (Default)", "Bright", "Dark"}, value = "Both (Default)") Method
'''
MSER : Maximally Stable Extremal Region Detector
This detector performs successive thresholding with different thresholds : from bright to dark AND dark to bright by default (but one can optionnally choose only one direction too)
The Bright to Dark option rather identify bright regions and vice-versa

For each threshold it performs a connected component analysis
Then it compare the evolution of a connected region over the level of thresholds :
the more variation in the size of the region over the threshold levels levels, the less stable is the region.

Finally the script retruns a set of stable regions that fits in the criterium of area and stability defined by the user

It retruns this region as a cloud of point, as well as a bounding box for the region. 
In this macro, the bounding boxes is returned as a ROI stored into the roi manager

A nice use can be to combine with the mask of the object using "AND" arithmetic so that we only retain the MSER region on the object !
'''
from org.bytedeco.javacpp.opencv_features2d import MSER
from org.bytedeco.javacpp.opencv_core       import RectVector, PointVectorVector
from ij 									import IJ, ImagePlus
from ij.gui 								import Roi
from ij.plugin.frame						import RoiManager
from ImageConverter							import ImProcToMat

# Initialise ROI manager
RM = RoiManager()
rm = RM.getRoiManager()
rm.runCommand("Show All")

# Define defaut paramters for detector - according to doc those are used only for color images so lets use the default only
min_diversity  = 1
max_evolution  = 10
area_threshold = 1.01
min_margin     = 0.003
edge_blur_size = 5 

# Initialise detector
detector = MSER.create(Delta,MinArea,MaxArea,MaxVar,min_diversity,max_evolution,area_threshold,min_margin,edge_blur_size)

# Set unilateral threshold if selected
if Method != "Both (Default)":
	print "Set to unidirectionnal thresholding"
	detector.setPass2Only(True)

# Get ImageProcessor from ImagePlus
ImProc = Image.getProcessor().duplicate()

# Detect stable regions
if Method == "Dark":
	# invert the image before doing the unidirectionnal detection
	print "Invert image before unidirectionnal thresholding"
	ImProc.invert()

# Convert image to 8-bit (scaling) opencv matrix
ImCV = ImProcToMat(ImProc, Bit=8)

# Initialise result vectors
Points = PointVectorVector()
BBox   = RectVector()

# detect MSER regions
detector.detectRegions(ImCV,Points, BBox)

# Report the number of detected BBoxes (rather report it in a result table ?)
N = BBox.size()
message = "Found {} stable regions".format(N)
print message
IJ.log(message)

# Add BBoxes as ROI on the image (eventually create an overlay when they will be the need to process several images) or burn them into the image
ListBox = [BBox.get(i) for i in range(N)]
for box in ListBox : 
	NewRoi = Roi(box.x(), box.y(), box.width(), box.height())
	rm.addRoi(NewRoi)