HamoudaOthman / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

cvFindContours Stack Overflow after cvAdaptiveThreshold #14

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.Run cvAdaptiveThreshold on an image.
2.Set up the contour variables as demonstrated in the JavaCV example (with 
contourPointer and contour).
3.Get the contours using cvFindContours with the same method demonstrated in 
the main JavaCV example (on the front page).
4. Try getting the CvSeq variable from contourPointer using 
PointerByReference():

CvSeq contours = contourPointer.getStructure();

What is the expected output? What do you see instead?

We expect to get a CvSeq containing all the contours found. Instead, there is a 
stack overflow error, seemingly occuring due to repeated pointer to object 
casts at the C/C++ level.

What version of the product are you using? On what operating system?

I am using Java 1.6, JDK 6, OpenCV 2.1.0, JNA 3.2.5, and JavaCV 20100730. This 
is on the Windows XP OS.

Please provide any additional information below.

Converting to cvThreshold instead of cvAdaptiveThreshold (as in the example) 
fixes the error. However, I am in need of adaptive thresholding for this 
particular program.

Thank you for your work porting this to Java, it's immensely helpful. 
Hopefully, you'll be able to help me figuring out this minor error.

- Nick

Original issue reported on code.google.com by nsul...@gmail.com on 7 Aug 2010 at 12:23

GoogleCodeExporter commented 8 years ago
The cause of this problem is most likely the same as the one for issue #10 
http://code.google.com/p/javacv/issues/detail?id=10 .. will keep you posted for 
something to try

Original comment by samuel.a...@gmail.com on 7 Aug 2010 at 4:14

GoogleCodeExporter commented 8 years ago
Hi Samuel,

Thanks for your response. I tried adding setAutoSynch(false) to all CvSeq 
constructors, but that didn't actually fix the issue. To be certain, the error 
no longer appears, but the contours found, if at all, are 1-2 pixel radius 
circles randomly. Using your test2.java example, none of the contours found 
with the non-modified javacv.jar are found.

I'm mildly stumped, but would like to show you the exact error, on the off 
chance I didn't explain myself correctly:

Exception in thread "main" java.lang.StackOverflowError
    at com.sun.jna.Native.getNativeSize(Native.java:951)
    at com.sun.jna.Native.getNativeSize(Native.java:937)
    at com.sun.jna.Structure.getNativeAlignment(Structure.java:890)
    at com.sun.jna.Structure.calculateSize(Structure.java:823)
    at com.sun.jna.Structure.allocateMemory(Structure.java:282)
    at com.sun.jna.Structure.<init>(Structure.java:177)
    at com.sun.jna.Structure.<init>(Structure.java:167)
    at com.sun.jna.Structure.<init>(Structure.java:163)
    at com.sun.jna.Structure.<init>(Structure.java:154)
    at name.audet.samuel.javacv.jna.cxcore$CvArr.<init>(cxcore.java:2379)
    at name.audet.samuel.javacv.jna.cxcore$CvSeq.<init>(cxcore.java:3060)
    at name.audet.samuel.javacv.jna.cxcore$CvSeq$ByReference.<init>(cxcore.java:3092)
    at sun.reflect.GeneratedConstructorAccessor3.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at com.sun.jna.Structure.newInstance(Structure.java:1159)
    at com.sun.jna.Structure.updateStructureByReference(Structure.java:486)
    at com.sun.jna.Pointer.getValue(Pointer.java:400)
    at com.sun.jna.Structure.readField(Structure.java:517)
    at com.sun.jna.Structure.read(Structure.java:419)
    at com.sun.jna.Structure.autoRead(Structure.java:1368)

loops the following infinitely:

    at com.sun.jna.Structure.updateStructureByReference(Structure.java:490)
    at com.sun.jna.Pointer.getValue(Pointer.java:400)
    at com.sun.jna.Structure.readField(Structure.java:517)
    at com.sun.jna.Structure.read(Structure.java:419)
    at com.sun.jna.Structure.autoRead(Structure.java:1368)

Thank you,
Nick

Original comment by nsul...@gmail.com on 11 Aug 2010 at 12:52

GoogleCodeExporter commented 8 years ago
Once "auto-synch" is disabled, you need to use the readField() method to read 
the native values of the fields in the Structure. The values of the Java fields 
will not be correct.. Did you use readField() in your Java code? 

BTW, I modified CvSeq slightly in this test package to include a static 
autoSynch flag:
    http://www.ok.ctrl.titech.ac.jp/~saudet/javacv.jar
With that, something like the following should disable the default auto-synch 
behavior of any newly constructed CvSeq objects:
    CvSeq.autoSynch = false;
If that works, I plan to add the flag to all Structure in the next release... 
thanks

Original comment by samuel.a...@gmail.com on 12 Aug 2010 at 3:04

GoogleCodeExporter commented 8 years ago
Hi Samuel,

Thanks for the advice. I'm not very familiar with JNA. Is there any way you 
could clarify on readField(). Am I to just replace read() with readField() in 
the cvSeq constructors?

Thank you,
Nick

Original comment by easul...@gmail.com on 12 Aug 2010 at 3:58

GoogleCodeExporter commented 8 years ago
For each individual field, yes.. if you have a field "int number", then instead 
of something like 
    int n = someStructure.number
you replace that with 
    int n = (Integer)someStructure.readField("number")

Original comment by samuel.a...@gmail.com on 12 Aug 2010 at 4:02

GoogleCodeExporter commented 8 years ago
Marking this long-standing issue as fixed, since it should work. If you still 
get stack overflows after setting CvSeq.autoSynch = false, please reopen the 
issue. Otherwise please mark it as /verified/, thank you.

Original comment by samuel.a...@gmail.com on 4 Nov 2010 at 10:40