我正在使用Android中的Opencv来计算检测到的对象的rotation angle,然后我将该对象旋转回其正常位置,以进行进一步的图像训练,如分割和对象匹配。
这就是我到目前为止得到的
double rect_angle = rbox.angle - 90.0f;
Size rect_size = rbox.size;
double d = rect_size.width;
rect_size.width = rect_size.height;
rect_size.height = d;
M = Imgproc.getRotationMatrix2D(rbox.center, rect_angle, 1.0);
Imgproc.warpAffine(origMat, rotated, M, origMat.size());如果我稍微旋转一下我的物体,结果如下

如果我不旋转物体,这就是我得到的结果

我需要保持对象始终居中。
我的问题类似于这个问题Rotate an image without cropping in OpenCV in C++
但在java中我无法做到这一点。
我希望你们能帮我做到这一点。
发布于 2019-04-02 05:59:17
对于这个问题,PyImageSearch有一个很好的解释。虽然解决方案是用Python编写的,但我相信您可以很容易地将数学运算转换为Java。
目标是用新输出图像的尺寸编辑旋转矩阵。此输出图像的大小将根据旋转效果进行调整。
参考PyImageSearch的解释中的以下代码,您可以看到在修改后的旋转矩阵中考虑了新输出图像的尺寸:
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))https://stackoverflow.com/questions/55458158
复制相似问题