我在图像上找到一个rotatedRect,我想检索此rotatedRect中所有像素的subMatrice。我在以下主题中找到了这个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位浮点来解决它。