我已经使用Android中的Opencv作为RotatedRect成功检测到一个对象,并且需要将该矩形提取到新的Mat上,以便对其进行更多处理。
我正在尝试使用getRectSubPix函数,但是在相机预览中出现了黑色Mat,但没有崩溃。
这是我当前正在使用的代码
MatOfPoint2f new_mat = new MatOfPoint2f( contours.get(largest_idx).toArray() );
RotatedRect rbox = Imgproc.minAreaRect(new_mat);
Point vertices [] = new Point[4];
rbox.points(vertices);
List<MatOfPoint> boxContours = new ArrayList<>();
boxContours.add(new MatOfPoint(vertices));
for(int i=0; i<4; ++i)
{
Imgproc.line(origMat, vertices [i], vertices [(i+1)%4], new Scalar(255,0,0));
}
// get angle and size from the bounding box
double rect_angle = rbox.angle;
Size rect_size = rbox.size;
if (rbox.angle < -45.0) {
rect_angle += 90.0;
double d = rect_size.width;
rect_size.width = rect_size.height;
rect_size.height = d;
}
// get the rotation matrix
M = Imgproc.getRotationMatrix2D(rbox.center, rect_angle, 1.0);
// perform the affine transformation
Imgproc.warpAffine(origMat, rotated, M, origMat.size());
Imgproc.cvtColor(rotated, rotated, Imgproc.COLOR_BGRA2BGR);
// crop the resulting image
Imgproc.getRectSubPix(rotated, rect_size, rbox.center, cropped);
我发现getRectSubPix接受32位浮点。
我试图像这样转换输入
rotated.convertTo(rotated, CvType.CV_32F);
但结果没有变化。
怎么了?我该怎么办?