bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.42k stars 1.57k forks source link

HOGDescriptor.compute doesn't work #2060

Closed tomaszkalisiak-digica closed 12 months ago

tomaszkalisiak-digica commented 12 months ago

The method has three overloads that I tried out.

public native void compute(@ByVal Mat img,
                             @StdVector float[] descriptors);
public native void compute(@ByVal Mat img,
                             @StdVector FloatBuffer descriptors);
public native void compute(@ByVal Mat img,
                             @StdVector FloatPointer descriptors);

I also have this line

var descriptorSize = (int) hog.getDescriptorSize(); // 576

I tried the following:

var arr = new float[descriptorSize];
hog.compute(image, arr);

and

var features = new FloatPointer(descriptorSize);
hog.compute(image, features);

and

var features = new FloatPointer(descriptorSize);
var hogMat = new Mat(1, descriptorSize, CV_32FC1, features);
hog.compute(image, hogMat.getFloatBuffer());

I always get one of those errors:

free(): invalid next size (normal)
Aborted (core dumped)

or

corrupted size vs. prev_size
Aborted (core dumped)

or

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fcbfe60bc8e, pid=234668, tid=234669
#
# JRE version: OpenJDK Runtime Environment Temurin-17.0.7+7 (17.0.7+7) (build 17.0.7+7)
# Java VM: OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (17.0.7+7, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64)
# Problematic frame:
# C  [libopencv_objdetect.so.407+0x6bc8e]
#
# Core dump will be written. Default location: /mnt/wslg/dumps/core.%e.234668
#
# An error report file with more information is saved as:
# /home/bobini/JavaCV-test-wsl/hs_err_pid234668.log
#
# If you would like to submit a bug report, please visit:
#   https://github.com/adoptium/adoptium-support/issues
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Aborted (core dumped)

I load my hog descriptor from an xml, I made it in Python with opencv-python 4.7.0.68. It works in my C++ project, which uses OpenCV 4.5.5, so I think it's fine. I also load an SVM model and that one works fine.

Is it possible to use HOGDescriptor.compute in any way?

tomaszkalisiak-digica commented 12 months ago

I rewrote my xml into a constructor:

private final HOGDescriptor hog = new HOGDescriptor(new Size(64, 64), new Size(8, 8), new Size(8, 8),
            new Size(8, 8), 9, 0, 2.0, 0, 0.2, true, 64, true);

Feel free to test with this.

saudet commented 12 months ago

Could you provide a working example in C++? Just the constructor + compute is fine.

tomaszkalisiak-digica commented 12 months ago

Turns out that the input image wasn't resized to the right size. There was no assert for that inside OpenCV, it just crashed.

saudet commented 12 months ago

Please report upstream! https://github.com/opencv/opencv/issues