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?
Related
I have a java image analysis program and am trying to get it to run on android. It starts like this (working on standard java):
import java.awt.image.BufferedImage;
public MaskImage(BufferedImage srcImage) {
super(srcImage.getWidth(), srcImage.getHeight(), srcImage.getType());
this.srcImage = srcImage;
this.height = srcImage.getHeight();
this.width = srcImage.getWidth();
}
When I try to get it working on android studio however I change bufferedimage to Bitmap, but still run into a couple of problems:
I cannot extend Bitmap in the same way I could BufferedImage
super(srcImage.getWidth(), srcImage.getHeight(), srcImage.getType());
this line runs into multiple issues, Says:
expected Parameters; actual: Arguments;
Thanks For any Help!
As you already know, Bitmap is final. By the very definition of final classes in Java, this means you cannot subclass it.
You will need to find an alternate strategy for accomplishing whatever you are trying to accomplish. Perhaps creating a class that wraps a Bitmap is one solution.
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);
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);
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?
I want put a barcode in my page and can preview it. The barcode generator is google.zxing and my reporting tool is iReport.
But i dont know, how to configure Image Expression and Expression Class of an image in iReport.
The two key ideas are first to write a bit of Java code to create the relevant image and then to design the report to reference this code appropriately. Perhaps the simplest way to generate the image is in a scriptlet like this:
package com.jaspersoft.alliances.mdahlman;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
public class QRCodeScriptlet extends JRDefaultScriptlet {
public void afterDetailEval() throws JRScriptletException {
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = null;
try {
matrix = writer.encode(getFieldValue("barcode_text").toString(), BarcodeFormat.QR_CODE, 256, 256);
this.setVariableValue("BarCodeImage", MatrixToImageWriter.toBufferedImage(matrix) );
} catch (WriterException e) {
e.printStackTrace();
}
}
}
That's full of hard-coded ugliness, but the key ideas are all shown. Then you need to define the report like this:
Sample query: select 'some text' as barcode_text
I included this only to reinforce the point that my scriptlet hard-codes the field name barcode_text. (This is bad.)
Variable: BarCodeImage of type java.awt.image.BufferedImage with calculation System.
This name is hard-coded in the scriptlet too. (This is equally bad.)
Add to iReport's classpath:
The compiled scriptlet .jar file
core.jar (from ZXing)
javase.jar (from ZXing)
Add an Image element to the report with Expression $V{BarCodeImage}.
The result is a happy happy QR-code in your generated JasperReport:
I recall a sample that I have seen which does things much more cleanly. It actually included a nice plug-in so you could easily install this functionality into iReport with minimal effort. If I can track that down, then I'll update this post. But until then this at least covers all of the critical points.
The image expression should return any subclass of java.awt.Image. The easiest way to achieve this is to use your own helper class to generate the Image. You can create a static method that generates a barcode from a Stringand call that method from IReport.
In the case of ZXing I don't know the method to use, but I can tell what I use as ImageExpression using the Barbecue library.
net.sourceforge.barbecue.BarcodeImageHandler.getImage(
MyBarcodeGenerator.getFromString($F{field})
MyBarcodeGenerator class contains the method getFromString(...) that returns a net.sourceforge.barbecue.Barcode in my case a net.sourceforge.barbecue.linear.code39.Code39Barcode
The Expression Class is ignored.
--Edited:
To encode an Image in zxing you should use MatrixToImageWriter
The following code will encode a QRCode into a BufferedImage which you can use in the Image Expression field:
MatrixToImageWriter.toBufferedImage(new QRCodeWriter().encode("BARCODE CONTENT", BarcodeFormat.QR_CODE, 400 /*Width*/, 400/*Height*/));