IplImage <--> Mat conversion

566 views
Skip to first unread message

Mauro Nonnis

unread,
Aug 6, 2015, 8:50:47 PM8/6/15
to javacv
I'm facing issues while converting IplImages to Mat. When I run the simple code below I get a fatal error runtime on the third instruction:

IplImage origImg = cvLoadImage(workingDir+filename, CV_LOAD_IMAGE_COLOR);
Mat imgMat = new Mat(origImg);
IplImage origImg2 = new IplImage(imgMat);

This is what I founded in console:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000000011a2ad060, pid=2703, tid=4867
#
# JRE version: Java(TM) SE Runtime Environment (7.0_60-b19) (build 1.7.0_60-b19)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.60-b09 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C  [libopencv_core.3.0.dylib+0x16e060]  _IplImage::_IplImage(cv::Mat const&)+0x80
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again


I'm pretty sure that origiImg is properly initialized (I used it for other stuff), and the result no change with other Mat initialization, like this:

CvMat cvMatImg = origiImg.asCvMat(); 
Mat imgMat = new Mat(cvMatImg);
IplImage origImg2 = new IplImage(imgMat);

In this case the error is the following:

OpenCV Error: Assertion failed (m.dims <= 2) in _IplImage, file .../javacpp-presets-master/opencv/cppbuild/macosx-x86_64/opencv-3.0.0/modules/core/src/matrix.cpp, line 5411
Exception in thread "main" java.lang.RuntimeException: .../javacpp-presets-master/opencv/cppbuild/macosx-x86_64/opencv-3.0.0/modules/core/src/matrix.cpp:5411: error: (-215) m.dims <= 2 in function _IplImage
at org.bytedeco.javacpp.opencv_core$IplImage.allocate(Native Method) at org.bytedeco.javacpp.opencv_core$IplImage.<init>(opencv_core.java:3229) at org.bytedeco.javacv.OpenCVFaceRecognizer.detectFace(OpenCVFaceRecognizer.java:142) at org.bytedeco.javacv.OpenCVFaceRecognizer.main(OpenCVFaceRecognizer.java:171) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Thanks in advance for your help.

Amrita

unread,
Aug 8, 2015, 2:34:13 PM8/8/15
to javacv
Hi Mauro,

I tried recreating the error you got, but the code worked fine for me. Maybe someone else will pitch in later, but for now perhaps you can try this:

String filePath = workingDir.concat(filename);
System.out.println(filePath) ;//just to check
CvMat image = cvLoadImageM(filePath,CV_LOAD_IMAGE_COLOR);
Mat imgMat = new Mat(image);
IplImage origImg = imgMat.asIplImage();


Also, this is probably not the problem but have you checked that the path to your image file is correct using a print statement? For example, are you sure it includes a separator before filename? And does filename end with the proper extension?

Samuel Audet

unread,
Aug 8, 2015, 10:12:40 PM8/8/15
to jav...@googlegroups.com
On 08/06/2015 11:19 PM, Mauro Nonnis wrote:
> I'm facing issues while converting IplImages to Mat. When I run the simple
> code below I get a fatal error runtime on the third instruction:
>
> IplImage origImg = cvLoadImage(workingDir+filename, CV_LOAD_IMAGE_COLOR);
>
> Mat imgMat = new Mat(origImg);
> IplImage origImg2 = new IplImage(imgMat);

They changed the API for that starting with OpenCV 3.0. Try this:
Mat imgMat = cvarrToMat(origImg);
IplImage origImg2 = new IplImage(imgMat);

Samuel

Mauro Nonnis

unread,
Aug 11, 2015, 10:13:08 AM8/11/15
to javacv
Thanks, now seems to work.
I noticed that many API changes came up with OpenCV 3.0. Can you suggest me the best approach to find the new APIs when oldest are no more available?

Thanks.

Samuel Audet

unread,
Aug 12, 2015, 8:34:26 PM8/12/15
to jav...@googlegroups.com
On 08/11/2015 11:13 PM, Mauro Nonnis wrote:
> Thanks, now seems to work.
> I noticed that many API changes came up with OpenCV 3.0. Can you suggest
> me the best approach to find the new APIs when oldest are no more available?

There appears to be a transition guide here:
http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html
But it's not very extensive, so when it doubt ask questions I guess :)

Samuel

Reply all
Reply to author
Forward
0 new messages