#@File    (Label="Template image") TemplateFile  
#@File[]  (label="Images/Image folder for which to look for the template", style="file") ListFile 
#@String  (Label="Matching method",choices={"Square difference","Normalised Square Difference","Cross-Correlation","Normalised cross-correlation","0-mean cross-correlation","0-mean normalised cross-correlation"}, value="0-mean normalised cross-correlation") Method
#@Boolean (Label="Flip template vertically") FlipV
#@Boolean (Label="Flip template horizontally") FlipH
#@String  (Label="Additional rotation angles separated by ," ,required=False) inAngles
#@Boolean (Label="Display image with found ROI") ShowIm
#@Boolean (Label="Display correlation map(s)") ShowMap
#@Boolean (Label="Show result table") ShowTable
#@Boolean (Label="Save ROI found") SaveFound
'''
FIJI macro  to do template matching without smart imaging
We dont use the GUI function from the module since
This script prompts a window to get the list of image to process 
Folder can also be given in this case all images of the foler are processed 
The only limitation currently is that all target images (where the search is done) must have the same size 
Then it calls the GUI of the matching method and performs the match on the list of images 
 
We dont use the config() method from the template matching module otherwise we mix #@Script parameters and GenericDialog input and the macro is no more properly recordable 
'''
from ij import IJ 
from ROIdetection.SearchRoi			   import getSearchRoi
from ROIdetection.MatchTemplate_Module import PreProcessTemplate,Routine
 
ListPath = [File.getPath() for File in ListFile]

# Convert method string to the index
Dico_Method  = {"Square difference":0,"Normalised Square Difference":1,"Cross-Correlation":2,"Normalised cross-correlation":3,"0-mean cross-correlation":4,"0-mean normalised cross-correlation":5}
Method       =  Dico_Method[Method]

# Open template
Template = IJ.openImage(TemplateFile.getPath())

# Check if searchROI
searchROI = getSearchRoi(Display=False) # Use the first Roi in RoiManager as the search area. if None nothing happen

# PreProcess template
ListTemplateWithFlipped, ListTemplateFlip90_Name, ListTemplateFlip90CV, otherAngle = PreProcessTemplate(Template,FlipV,FlipH,inAngles,searchROI)

# Loop over target images
Routine(ListPath, ListTemplateWithFlipped, ListTemplateFlip90_Name, ListTemplateFlip90CV, Method, otherAngle, searchROI, ShowIm, ShowMap, ShowTable, SaveFound)