#@ File    (Label="Template image") TemplateFile
#@ File[]  (label="Images/Image folder for which to look for the template", style="file") ListFile
#@ String  (Label="Keypoint detection/description method",choices={"SOBEL","HARRIS","GFTTD","SIFT","SURF","FAST","AGAST","ORB","BRISK","KAZE","AKAZE"}, value="SIFT") Method
#@ Boolean (Label="Search for rotation of the template", value = True) Rotation
#@ Boolean (Label="Edit default parameters for detector/descriptor", value = False) EditDefault
#@ String  (Label="Filter matches",choices={"Ratio test","Keep best matches up to the Nth percentile","Keep only the N best matches","None"}, value="Ratio test") MatchFilter
#@ int     (Label="N best matches", value=100) Nmatch
#@ int     (Label="Nth best percentile",min=0,max=100, value=0.5) Nperc
#@ Boolean (Label="Display overlay images with initial keypoints", value=True) ShowRaw
#@ Boolean (Label="Display matches before geometric evaluation", value=True) ShowMatch 
#@ Boolean (Label="Display image with found ROI", value=True) ShowFound
#@ Boolean (Label="Show result table", value=True) ShowTable

'''
FIJI macro - Keypoint matching without smart imaging
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

NB : not using the GUI method from the keypoint module since we need to add a field for the image files to process
This field is conveniently provided by the script parameters (while with the GenericDialog a bit more cumbersome)
'''
from ij 						  import IJ
from ROIdetection.SearchRoi 	  import getSearchRoi, getRoiDim
from ROIdetection.Keypoint_Module import SetupMethod, PreProcessTemplate, PreProcessImage, KpMatching, FindTransform, getResultRoi, BurnRoiToStackMatch, AddToTable

# Convert filter method string to the index
Dico_Method  = {"Ratio test":0,"Keep best matches up to the Nth percentile":1,"Keep only the N best matches":2,"None":3}
FilterIdx    =  Dico_Method[MatchFilter]

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

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

# Set up the kp detection/description method and matcher
DetectAndDescribeKp, Matcher = SetupMethod(Method, EditDefault, FilterIdx, Rotation) # Pop up a new Gui to setup the method if Edit Default is ticked

# Detect and Describe Kp in Template
KpTemp, KpTempCV, DesTemp, Corners = PreProcessTemplate(Template,DetectAndDescribeKp)

# Loop over the images in the list
for File in ListFile:
		
		# recover the path
		ImagePath = File.getPath()

		# open ImagePlus
		Image = IJ.openImage(ImagePath)

		# Detect and Describe keypoints in the image, automatically display the result if ShowRaw = True
		KpIm, KpImCV, DesIm = PreProcessImage(Image,DetectAndDescribeKp,RoiDim,ShowRaw)

		# Match Kp from template and image based on descriptors, automatically display the image with matches if ShowMatch == True
		ListMatch = KpMatching(Matcher,KpTempCV,DesTemp,KpImCV,DesIm,FilterIdx,Nperc,Nmatch,ShowMatch,Template,Image)

		# Try to find a rigid transformation
		RigidMatrix, List_ptImX, List_ptImY = FindTransform(KpTempCV, KpImCV, ListMatch, Rotation)

		# If could not find a rigid transform use Modal point value instead (most probable point from points in the image that got matched)
		FoundRoi, Xcenter, Ycenter = getResultRoi(RigidMatrix,Corners,List_ptImX, List_ptImY, Image.getTitle(), ShowFound)
		
		# Burn found roi on image in StackMatch
		if FoundRoi and ShowMatch:
			BurnRoiToStackMatch(FoundRoi,Template.width)
		
		if ShowTable:
			# Report results into table
			AddToTable(Image.getTitle(), Xcenter, Ycenter)