I have successfully used openCV within jython to do some template matching following this previous post, and in my case using the openCV version provided by the update site.
I am using the
minMaxLoc method to get the location and value of the maximal pixel for an openCV matrix object containing the correaltion map.
I could indeed recover the location but for the value,I have a
DoublePointer object and even looking at its attribute I could not really guess how I can recover this maxima.
I though of indexing using the index provided by the location but Matrix object cannot be indexed simply by M[i,j] indexing apparently. I though of reconverting back to an ImagePlus object but I did not manage to perform the conversion, maybe because the Matrix object containing the correlation map is of type float…
Here is the code:
# @ ImagePlus (label="Template") Template # @ ImagePlus (label="Image") Image from org.bytedeco.javacpp.opencv_imgproc import matchTemplate from org.bytedeco.javacpp.opencv_core import Mat, Point, minMaxLoc from org.bytedeco.javacpp import DoublePointer from ijopencv import ImageConverter # convert ImagePlus object to Opencv matrices # Initialise converter Converter = ImageConverter() # Convert to opencv image for match template TemplateCV = Converter.convertTo(Template) # convert to OpenCV matrice object ImageCV = Converter.convertTo(Image) # Create a correlation map object and do the matching CorrMapCV = Mat() matchTemplate(ImageCV, TemplateCV, CorrMapCV, 5) # result directly stored in CorrMap # Convert correlation map back to an image plus (fails for some reasons) #CorrMap = Converter.convertFrom(CorrMapCV) #print CorrMap # Find location of the max minVal = DoublePointer() maxVal = DoublePointer() minLoc = Point() maxLoc = Point() minMaxLoc(CorrMapCV, minVal, maxVal, minLoc, maxLoc, Mat()) CornerXY = maxLoc.x(),maxLoc.y() # ImageJ has the same XY than openCV print CornerXY print maxVal # well not very informative...
and the images