Imgproc.getRectSubPix OpenCV 函数在 Java 中给出错误

Imgproc.getRectSubPix OpenCV function give an error in Java

我在图像上找到了一个 rotatedRect,我想检索这个 rotatedRect 中所有像素的子矩阵。我在这个主题上找到了这个 C++ 代码:http://answers.opencv.org/question/497/extract-a-rotatedrect-area/,这里是我的 Java 代码:

   Mat M = new Mat(), rotated = new Mat(), cropped = new Mat();

    // get angle and size from the bounding box
    double angle = mTable.angle;
    Size rect_size = mTable.size;

    if (mTable.angle < -45.0) {
        angle += 90.0;
        double w = rect_size.width;
        rect_size.width = rect_size.height;
        rect_size.height = w;
    }

    // get the rotation matrix
    M = Imgproc.getRotationMatrix2D(mTable.center, angle, 1.0);

    // perform the affine transformation
    Imgproc.warpAffine(mRgba, rotated, M, mRgba.size(), Imgproc.INTER_CUBIC);

    // crop the resulting image
    Imgproc.getRectSubPix(rotated, rect_size, mTable.center, cropped); //THIS LINE DOESN'T WORK

这是我在 logCat

中的错误
02-17 17:02:34.791: E/cv::error()(3588): OpenCV Error: Unsupported format or combination of formats () in void cvGetRectSubPix(void const*, void*, CvPoint2D32f), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/samplers.cpp, line 556
02-17 17:02:34.791: E/org.opencv.imgproc(3588): imgproc::getRectSubPix_11() caught cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/samplers.cpp:556: error: (-210)  in function void cvGetRectSubPix(void const*, void*, CvPoint2D32f)
02-17 17:02:34.791: W/dalvikvm(3588): threadid=11: thread exiting with uncaught exception (group=0x418ccda0)
02-17 17:02:34.791: E/AndroidRuntime(3588): FATAL EXCEPTION: Thread-29491
02-17 17:02:34.791: E/AndroidRuntime(3588): Process: com.example.testopencv, PID: 3588
02-17 17:02:34.791: E/AndroidRuntime(3588): CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/samplers.cpp:556: error: (-210)  in function void cvGetRectSubPix(void const*, void*, CvPoint2D32f)
02-17 17:02:34.791: E/AndroidRuntime(3588): ]
02-17 17:02:34.791: E/AndroidRuntime(3588):     at org.opencv.imgproc.Imgproc.getRectSubPix_1(Native Method)
02-17 17:02:34.791: E/AndroidRuntime(3588):     at org.opencv.imgproc.Imgproc.getRectSubPix(Imgproc.java:6618)
02-17 17:02:34.791: E/AndroidRuntime(3588):     at com.example.testopencv.MainActivity.continueTrackingRedBall(MainActivity.java:336)
02-17 17:02:34.791: E/AndroidRuntime(3588):     at com.example.testopencv.MainActivity.onCameraFrame(MainActivity.java:155)
02-17 17:02:34.791: E/AndroidRuntime(3588):     at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
02-17 17:02:34.791: E/AndroidRuntime(3588):     at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
02-17 17:02:34.791: E/AndroidRuntime(3588):     at java.lang.Thread.run(Thread.java:841)

getRectSubPix 函数似乎在 Java 中不起作用,有人找到解决方案了吗?

我使用 cv2.getRectSubPix 在 python 上遇到了类似的错误:

    img_cropped = cv2.getRectSubPix(img_rotated, size_xy, center_xy)
cv2.error: /tmp/buildd/ros-hydro-opencv2-2.4.9-2precise-20141231-1923/modules/imgproc/src/samplers.cpp:591: error: (-210)  in function cvGetRectSubPix

我通过将我的图像从 8 位灰度转换为 32 位浮点来解决它。