Value from DoublePointer openCV/Jython

jython
scripting
opencv
Tags: #<Tag:0x00007fa3072c4050> #<Tag:0x00007fa3072bfd48> #<Tag:0x00007fa3072bfa50>

#1

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
Template
Head-A005

Target


#2

Hey @LThomas,

cool to see that openCV runs within Fiji!

I’m not sure if I understood the problem correctly: You try to access the value of the maximum pixel using a DoublePointer object? If you call

print maxVal.get();

you get a NullPointerException, because your DoublePointer points to an empty (length=0) memory block. If you hand over a number to the Constructor DoublePointer, that many Doubles will be reserved in memory. To make a long story short:

# ...

# Find location of the max
minVal = DoublePointer(1)
maxVal = DoublePointer(1)
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...
print maxVal.get()

Results in a value of 0.24… in the output. Does that help?

Cheers,
Robert


#3

Perfect, thanks Robert !


#4

Thank you for giving me a script that runs OpenCV within Fiji with no pain at all. My toolbox just increased significantly in size!


#5

You are welcome :smiley:
Just watch out if you use Maven to deal with the dependencies, if you use the pom from the github repository you get a different version than the one from the update site as pointed by @imagejan in the previous post . Some functions have been renamed or move to other folders…