How to convert opencv code into javacv code - java

How to convert this C++ opencv code . (this code is from camshift tracking demo in opencv https://github.com/Itseez/opencv/blob/master/samples/cpp/camshiftdemo.cpp )
Mat roi(hue,selection), maskroi(mask,selection);
into javacv code?

The neweast javacpp presets javacpp-presets-0.9 already hava whole opencv javacv version implementation. Check if the functions you need is available.
https://github.com/bytedeco/javacpp-presets/tree/master/opencv
If not, I think you need to see the definition(implemetaion) of the two c++ function roi() and maskroi() to convert very line code into javacv counterpart by yourself.
And, google group of javacpp is also a best place to ask if you hava javacpp related questions. http://groups.google.com/group/javacpp-project
Note:
for c++ output parameter type(call by pointer or call by reference), you need to understand java function parameter has no output type, so you need to use array instead as workaround, for example:
C++ code:
void detectBothEars(Mat input, Rect* left, Rect* right);
The javacv counterpat should be:
void detectBothEarsRect(Mat input, Rect[] left, Rect[] right);
And the client code is:
Rect[] leftRect = new Rect[1];
Rect[] rightRect = new Rect[1];
detectBothEars(face, leftRect , rightRect);

The same constructor is available from Java: public Mat(Mat m, Rect roi)
So, we can basically do the same thing:
Mat roi = new Mat(hue, selection), maskroi = new Mat(mask, selection);

Related

How to transform projection EPSG:3857 to EPSG:4326 in java (Geospatial)

Is there a way to transform a EPSG:3857 projection to EPSG:4326 in java? I'm using the esri java sdk. I went through the esri skd docs, but couldn't find a way to transform EPSG:3857 to EPSG:4326. Is there a way of doing it?
I have a webMercator like this: Point property = new Point(1.7040237624799997e7,-3099509.4953500014, SpatialReferences.getWebMercator());
And have a WSG84 like this Point point1 = new Point(153.089361, -26.802295, SpatialReferences.getWgs84());
I need to merge them and as those points have different Spatial References I can't display a map property.
I'm assuming you are using the ArcObjects SDK for Java? Then the following code should work because your Point class is implementing the IGeometry interface according to esri java doc
https://desktop.arcgis.com/en/arcobjects/latest/java/api/arcobjects/com/esri/arcgis/geometry/IGeometry.html
Point property = new Point(1.7040237624799997e7,-3099509.4953500014,
spatialReferences.getWebMercator());
Point reprojected = property.project(SpatialReferences.getWgs84());
Because your Point constructor looks like you're using one of the newer Esri SDKs like ArcGIS Pro SDK or Runtime SDK I'm adding a solution for them too:
Point originalPoint = new Point(1.7040237624799997e7,-3099509.4953500014,
spatialReferences.getWebMercator());
Point projectedPoint = (Point) GeometryEngine.project(originalPoint,
SpatialReference.create(4326));
according to
https://developers.arcgis.com/java/latest/sample-code/project.htm

JAVA CIEXYZ ColorSpace Conversion

Hello i am trying to write my own HSL Colorspace in Java but i ran into an problem with the CIEXYZ conversion...
I have already writen a from/toRGB Method but java requires to write a from/toCIEXYZ method too. I thought i could use the pre-implemented CIEXYZ Colorspace for that...
#Override
public float[] fromCIEXYZ(float[] colorvalue) {
return fromRGB(CIEXYZ.toRGB(colorvalue));
}
but this doesn't work and after some time i figured out that the CIEXYZ colorspace java provides doesnt works like i expected it...
ColorSpace cieXYZ = ColorSpace.getInstance(ColorSpace.CS_CIEXYZ);
System.out.println(Arrays.toString(cieXYZ.toRGB(cieXYZ.fromRGB(new float[]{1,0.5f,0}))));
the result of this small code is [0.9820706, 0.49709317, 0.122087434] and not [1 , 0.5, 0]...
can anybody explain me why?
and how can i fix this?

OpenCV Constants.CaptureProperty

Hi I use OpenCV Java and have some problem.
I open video file and try get property like FPS.
And others:
CV_CAP_PROP_POS_MSEC
CV_CAP_PROP_FRAME_COUNT
So first I opened video like this:
VideoCapture vC = new VideoCapture(url2);
and next i have a problem with function
vC.get(int i)
in OpenCV C++ its look like
vC.get(CV_CAP_PROP_FPS);
In Java where I find this constants?In HighGui I didnt find them. Only what I find is another libary to OpenCV where are this constants http://siggiorn.com/wp-content/uploads/libraries/opencv-java/docs/sj/opencv/Constants.CaptureProperty.html. But where I find them in OpenCV Java. Anyway how I have to use vC.get() function? Maybe some working example?
There is a bug report about this issue.
Until it is fixed, I suggest that you find these constants in the C++ source code, and define them yourself.
Edit:
I was just curious myself. You find them in the file modules/highgui/include/opencv2/highgui.hpp They are:
CAP_PROP_POS_MSEC =0,
CAP_PROP_POS_FRAMES =1,
CAP_PROP_POS_AVI_RATIO =2,
CAP_PROP_FRAME_WIDTH =3,
CAP_PROP_FRAME_HEIGHT =4,
CAP_PROP_FPS =5,
CAP_PROP_FOURCC =6,
CAP_PROP_FRAME_COUNT =7,
CAP_PROP_FORMAT =8,
CAP_PROP_MODE =9,
CAP_PROP_BRIGHTNESS =10,
CAP_PROP_CONTRAST =11,
CAP_PROP_SATURATION =12,
CAP_PROP_HUE =13,
CAP_PROP_GAIN =14,
CAP_PROP_EXPOSURE =15,
CAP_PROP_CONVERT_RGB =16,
CAP_PROP_WHITE_BALANCE_BLUE_U =17,
CAP_PROP_RECTIFICATION =18,
CAP_PROP_MONOCROME =19,
CAP_PROP_SHARPNESS =20,
CAP_PROP_AUTO_EXPOSURE =21, // DC1394: exposure control done by camera, user can adjust refernce level using this feature
CAP_PROP_GAMMA =22,
CAP_PROP_TEMPERATURE =23,
CAP_PROP_TRIGGER =24,
CAP_PROP_TRIGGER_DELAY =25,
CAP_PROP_WHITE_BALANCE_RED_V =26,
CAP_PROP_ZOOM =27,
CAP_PROP_FOCUS =28,
CAP_PROP_GUID =29,
CAP_PROP_ISO_SPEED =30,
CAP_PROP_BACKLIGHT =32,
CAP_PROP_PAN =33,
CAP_PROP_TILT =34,
CAP_PROP_ROLL =35,
CAP_PROP_IRIS =36,
CAP_PROP_SETTINGS =37
use class import org.opencv.videoio.Videoio;
vc.open(FD.class.getResource("1.avi").getPath());
double totalFrameNumber = vc.get(Videoio.CAP_PROP_FRAME_COUNT);
System.out.println("\n"+totalFrameNumber);
It seems the bug is solved. Now you should be able to use it as:
VideoCapture vC = new VideoCapture(...);
nbFrames = vC.get(Videoio.CAP_PROP_FRAME_COUNT);

How to extract programmatically video frames?

I need programmatically extract frames from mp4 video file, so each frame goes into a separate file. Please advise on a library that will allow to get result similar to the following VLC command (http://www.videolan.org/vlc/):
vlc v1.mp4 --video-filter=scene --vout=dummy --start-time=1 --stop-time=5 --scene-ratio=1 --scene-prefix=img- --scene-path=./images vlc://quit
Library for any of these Java / Python / Erlang / Haskell will do the job for me.
Consider using the following class by Popscan. The usage is as follows:
VideoSource vs = new VideoSource("file://c:\test.avi");
vs.initialize();
...
int frameIndex = 12345; // any frame
BufferedImage frame = vs.getFrame(frameIndex);
I would personally look for libraries that wrap ffmpeg/libavcodec (the understands-most-things library that many encoders and players use).
I've not really tried any yet so can't say anything about code quality and ease, but the five-line pyffmpeg example suggests it's an easy option - though it may well be *nix-only.

Creating graphs in Matlab and displaying them in a Java program

I have a requirement to produce graphs of matrices and display these graphs on a JSP. The project has been developed in Java and so far all my operations relating to matrices are being performed using the MatLabControl API
http://code.google.com/p/matlabcontrol/ .
I wanted to return the matrices produced by MATLAB (especially eigen value matrices and wavelets). MATLAB provides a function "im2java" that converts graph image from its MATLAB representation to a java.awt.Image. My code used to get the image data in MatlabControl was as follows:
public Image produceEigenValueGraph(final double [][] matrix) {
final double [][] maxEigenValueMatrix =
extractOutMaxEigenValues(matrix);
Image matlabPlotImage = null;
try {
MatlabNumericArray matLabEigenValueMatrix =
new MatlabNumericArray(maxEigenValueMatrix, null);
matLabTypeConverter.setNumericArray("eigen",
matLabEigenValueMatrix);
matLabProxy.setVariable("amountOfTime", matrix.length - 1);
matLabProxy.eval("time");
matLabProxy.eval("plot(time, eigen)");
matLabProxy.eval("frame=getframe");
final Object [] returnedMatlabArguements =
matLabProxy.returningEval("im2java(frame.cdata)", 1);
matlabPlotImage =
(Image)returnedMatlabArguements[0];
} catch (MatlabInvocationException mie) {
mie.printStackTrace();
}
return matlabPlotImage;
}
The code returns a nested exception:
Caused by: java.io.WriteAbortedException: writing aborted;
java.io.NotSerializableException: sun.awt.image.ToolkitImage
Which basically puts an end to any hope of the above code working, unless I am incorrect in my use.
N.B The code does produce a correct graph it fails to return it in java.awt.Image
My questions are:
-Is the above code the correct/only way to return images to a java program from Matlab?
-If it is what would be the best alternatives to using Matlab, Java API or otherwise?
Is this the line that causes the exception?
matlabPlotImage = (Image)returnedMatlabArguements[0];
In answer to your question
"-Is the above code the correct/only way to return images to a java program from Matlab?"
You can call java classes from Matlab so you could also use the java in a Matlab file and call that to replace
final Object [] returnedMatlabArguements = matLabProxy.returningEval("im2java(frame.cdata)", 1);
matlabPlotImage = (Image)returnedMatlabArguements[0];
The error is being thrown because Image is not serializeable. An option would be to save it as a file in some image format (jpg,png,tiff) using either matlab or java and return File instead of Image.
"-If it is what would be the best alternatives to using Matlab, Java API or otherwise?"
Mathworks provide a Java api to perform a number of linear algebra calculations that you could implement.
http://math.nist.gov/javanumerics/jama/#Package
Alternatively the Apache Commons Math project provide a wide range of linear algebraic functions as well as other tools. http://commons.apache.org/math/userguide/linear.html
I would check other posts for suggestions on graphing in java
constructing graphs in Java
Java Graphing Libraries for Web Applicattions?

Categories