# This file deals with tests of imagej's ijopencv solution to use opencv in imagej
# https://imagej.net/Jython_Scripting#Using_openCV_in_Jython and https://github.com/joheras/IJ-OpenCV/wiki provides a necessary but (too) basic documentation,
# "OpenCV (Open Source Computer Vision) is a library of several hundred algorithms for computer vision and video analysis. OpenCV can be us on JVM using two approaches. First are Java wrappers provided by OpenCV. Second are are wrappers based on JavaCPP (C++ wrapper engine for JVM) called OpenCV JavaCPP Presets. There are also JavaCPP presets for other computer vision related libraries like: FFmpeg, libdc1394, PGR FlyCapture, OpenKinect, videoInput, ARToolKitPlus, flandmark, and others. JavaCV combines libraries in JavaCPP Presets and add some additional functionality that makes them easier use on JVM."
# Here's what I've worked out by testing ijopencv :
# 1. ijopencv is a solution based on javacpp (http://bytedeco.org/)
# 2. javacpp provides java wrappers for a lot of c++ libraries such as opencv. A javacpp version is tied with a specific opencv version :
# 3. javacpp is not responsible for the python binding
# 4. jython exposes java classes and functions as python classes and functions. As a result, the java api of javacpp is exposed by jython as the python api org.bytedeco.javacpp.* (eg org.bytedeco.javacpp.opencv_core)
# 5. as aresult of 4, javacpp's python api for opencv is not as practical as opencv's python api: for example, it doesn't seem possible to access pixel data using image[x, y]. As a result, a python code written using opencv's javacpp api is not compatible with a python code using opencv vi opencv's python wrappers.
# 6. as a result of 4, the documentation for org.bytedeco.javacpp's functions and arguments can be found in http://bytedeco.org/javacpp/apidocs/ in the form of java functions. As this url only shows the latest version (javacpp 1.5.1), here's a way to find the functions that are available with your javacpp :
# 7 javacpp's python api doesn't seem to support keyword arguments
# 8 as org.bytedeco.javacpp.opencv* knows nothing about imagej, the package ijopencv provides conversion functions that convert images from imagej's ImageProcessor to org.bytedeco.javacpp.opencv_core.Mat and vice versa
# there are 2 calcHist functions in /Applications/Fiji.app/jars/opencv-3.4.2-1.4.2.jar, and they take 6 or 7 arguments :
# jar -xvf /Applications/Fiji.app/jars/opencv-3.4.2-1.4.2.jar
# TypeError: calcHist(): expected 6-8 or 10 args; got 1
# ah: ./opencv/src/main/java/org/bytedeco/javacpp/opencv_imgproc.java seems to contain the list of 57 allowed calcHist sets of arguments, as the number of arguments match
# [OK]graffy@pr079234:~/toto/javacpp-presets[19:45:41]>grep -A 5 calcHist ./opencv/src/main/java/org/bytedeco/javacpp/opencv_imgproc.java | sed -E 's/^[ ]*//g' | tr '\n' '`' | sed 's/,`/, /g' | tr '`' '\n' | grep '^@Namespace' | wc -l
# 57
# [OK]graffy@pr079234:~/toto/javacpp-presets[19:45:56]>grep -A 5 calcHist ./opencv/src/main/java/org/bytedeco/javacpp/opencv_imgproc.java | sed -E 's/^[ ]*//g' | tr '\n' '`' | sed 's/,`/, /g' | tr '`' '\n' | grep '^@Namespace' | head -2
# @Namespace("cv") public static native void calcHist( @Const Mat images, int nimages, @Const IntPointer channels, @ByVal Mat mask, @ByVal Mat hist, int dims, @Const IntPointer histSize, @Cast("const float**") PointerPointer ranges, @Cast("bool") boolean uniform/*=true*/, @Cast("bool") boolean accumulate/*=false*/ );
# @Namespace("cv") public static native void calcHist( @Const Mat images, int nimages, @Const IntPointer channels, @ByVal Mat mask, @ByVal Mat hist, int dims, @Const IntPointer histSize, @Const @ByPtrPtr FloatPointer ranges );
# java.lang.RuntimeException: java.lang.RuntimeException: OpenCV(3.4.2) /Users/travis/build/bytedeco/javacpp-presets/opencv/cppbuild/macosx-x86_64/opencv-3.4.2/modules/imgproc/src/histogram.cpp:918: error: (-210:Unsupported format or combination of formats) in function 'calcHist'
# create an empty mask. From https://docs.opencv.org/3.4.1/d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d :
# Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as images[i] . The non-zero mask elements mark the array elements counted in the histogram.
empty_mask=opencv_core.Mat()
print('empty_mask',empty_mask)
# @Namespace("cv") public static native void calcHist( @Const Mat images, int nimages, @Const int[] channels, @ByVal Mat mask, @ByVal Mat hist, int dims, @Const int[] histSize, @Const @ByPtrPtr float[] ranges );