首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Android中使用Opencv旋转图像而不进行裁剪?

如何在Android中使用Opencv旋转图像而不进行裁剪?
EN

Stack Overflow用户
提问于 2019-04-01 23:02:04
回答 1查看 121关注 0票数 2

我正在使用Android中的Opencv来计算检测到的对象的rotation angle,然后我将该对象旋转回其正常位置,以进行进一步的图像训练,如分割和对象匹配。

这就是我到目前为止得到的

代码语言:javascript
复制
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中我无法做到这一点。

我希望你们能帮我做到这一点。

EN

回答 1

Stack Overflow用户

发布于 2019-04-02 05:59:17

对于这个问题,PyImageSearch有一个很好的解释。虽然解决方案是用Python编写的,但我相信您可以很容易地将数学运算转换为Java。

目标是用新输出图像的尺寸编辑旋转矩阵。此输出图像的大小将根据旋转效果进行调整。

参考PyImageSearch的解释中的以下代码,您可以看到在修改后的旋转矩阵中考虑了新输出图像的尺寸:

代码语言:javascript
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55458158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档