Mat to CvMat conversion problem (Bug #3413)
Description
I'm using Java SE 6 on Eclipse on Windows XP. I instaled the latest version of OpenCV and downloaded an extra packege of *.jar files for JavaCV from this site: http://code.google.com/p/javacv/ , the javacv-0.6-bin.zip file to be precise.
I'm trying to implement the BOWKMeansTrainer on a descriptor aquiered by the FeatureExtractor class in my Java project. After downloading all the above parts, I've hit a brick wall: although in your documentation it is clearly stated that the Mat atributes can be cast to CvMat without any problems, each time I try it I get the following error:
"Type mismatch: cannot convert from Mat to opencv_core.CvMat"
I haven't found this problem in your bug tracker. Can you tell me where's the problem here?
Associated revisions
Merge pull request #3413 from wangyan42164:denoising_opencl_improvement
History
Updated by Steven Puttemans over 11 years ago
Could you please elaborate on your problem. Please supply the code that generates the error.
Because reading through Java documentation (http://docs.opencv.org/java/) I noticed that it should be possible with simple conversion operators:
_
Partial yet very common cases of this user-allocated data case are conversions from CvMat and IplImage to Mat. For this purpose, there are special constructors taking pointers to CvMat or IplImage and the optional flag indicating whether to copy the data or not.
Backward conversion from Mat to CvMat or IplImage is provided via cast operators Mat.operator CvMat() const and Mat.operator IplImage(). The operators do NOT copy the data.
// C++ code:
IplImage* img = cvLoadImage("greatwave.jpg", 1);
Mat mtx(img); // convert IplImage* -> Mat
CvMat oldmat = mtx; // convert Mat -> CvMat
CV_Assert(oldmat.cols img->width && oldmat.rows img->height &&
oldmat.data.ptr (uchar*)img->imageData && oldmat.step img->widthStep);
Use MATLAB-style array initializers, zeros(), ones(), eye(), for example:
// C++ code:
// create a double-precision identity martix and add it to M.
M += Mat.eye(M.rows, M.cols, CV_64F);
Use a comma-separated initializer:
// C++ code:
// create a 3x3 double-precision identity matrix
Mat M = (Mat_(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);_
Updated by Wojciech Litwinowicz over 11 years ago
Steven Puttemans wrote:
Could you please elaborate on your problem. Please supply the code that generates the error.
Because reading through Java documentation (http://docs.opencv.org/java/) I noticed that it should be possible with simple conversion operators:
The code generating the problem is really basic, but here is the full code with all the nessesary imports. I really have no idea why this occures. Maybe you guys can find the glitch?
import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.*; import java.io.File; import java.io.IOException; import java.util.StringTokenizer; import java.util.Vector; import com.googlecode.javacpp.*; import com.googlecode.javacv.*; import com.googlecode.javacv.cpp.opencv_core.CvMat; import com.googlecode.javacv.cpp.opencv_core.CvTermCriteria; import com.googlecode.javacv.cpp.opencv_core.IplImage; import com.googlecode.javacv.cpp.opencv_core.Ptr; import com.googlecode.javacv.cpp.opencv_features2d.BOWKMeansTrainer; import com.googlecode.javacv.cpp.opencv_flann.KMeansIndexParams; import com.googlecode.javacv.cpp.opencv_nonfree.SIFT; import org.opencv.*; import org.opencv.highgui.*; import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.core.MatOfKeyPoint; import org.opencv.core.Scalar; import org.opencv.core.TermCriteria; import org.opencv.features2d.DescriptorExtractor; import org.opencv.features2d.FeatureDetector; import org.opencv.features2d.Features2d; import org.opencv.features2d.KeyPoint; import javax.imageio.ImageIO; import javax.swing.SwingUtilities; public class Tracker { public static void main(String args[]) throws IOException{ File tmp = new File("opencv_java247.dll"); System.load(tmp.getAbsolutePath()); String url = "test.jpg"; File f = new File(url); BufferedImage buf = ImageIO.read(f); Mat input = Highgui.imread(url, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat desc = new Mat(), out = Highgui.imread(url, Highgui.CV_LOAD_IMAGE_UNCHANGED); //input.put(0, 0, ((DataBufferByte) buf.getRaster().getDataBuffer()).getData()); MatOfKeyPoint keypoints = new MatOfKeyPoint(); DescriptorExtractor detector = DescriptorExtractor.create(DescriptorExtractor.SIFT); FeatureDetector ft = FeatureDetector.create(FeatureDetector.SIFT); ft.detect(out, keypoints); detector.compute(input, keypoints, desc); Features2d.drawKeypoints(input, keypoints, out, new Scalar(255,0,0), 4); Highgui.imwrite("test3.jpg", out); CvMat zonk = desc; //<== the error is here zonk.put(0, 0); int size = 3, ret = 1; CvTermCriteria tc = new CvTermCriteria(TermCriteria.MAX_ITER, 50, 0.001); BOWKMeansTrainer bt = new BOWKMeansTrainer(size, tc, ret, Core.KMEANS_PP_CENTERS); CvMat dict = bt.cluster(zonk); } }
It's a bit messy, but I've tried to get it going in many different ways.
Updated by Daniil Osokin over 11 years ago
Hi Wojciech, JavaCV is not supported by OpenCV. Also, looks like there is no any bugs here, but issue, so right place for this is Q&A site: http://answers.opencv.org. I'm going to close this ticket, but may be Steven knows right answer.
- Assignee set to Wojciech Litwinowicz
- Priority changed from High to Low
- Category set to java bindings
- Target version set to 2.4.8
Updated by Wojciech Litwinowicz over 11 years ago
Ok, thanks anyway.
Updated by Steven Puttemans over 11 years ago
Actually, I had read over the fact that you are using JavaCV. Like Daniil stated, this is not an official supported package so it cannot be guaranteed that the JavaCV code works without bugs. There is no telling if the bug is in the wrapper or in the original source code.
I would suggest moving your question to the proper forums of JavaCV itself: http://code.google.com/p/javacv/issues/list.
You could try the Q&A forum, but we have kept JavaCV questions to the proper fora in the past, so guess this isn't the best approach.
- Assignee deleted (
Wojciech Litwinowicz) - % Done changed from 0 to 100
- Status changed from New to Done
Updated by Steven Puttemans over 11 years ago
As addition, I will cancel the ticket, reopen if you are sure the bug is really in OpenCV source code.
I myself have not enough experience in Java programming for this.
- Status changed from Done to Cancelled