【问题标题】:How to rotate an image withour cropping it using Opencv in Android?如何在 Android 中使用 Opencv 旋转图像而不进行裁剪?
【发布时间】:2019-04-01 15:02:03
【问题描述】:

我在 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 中实现。

我希望你们能帮助我实现这一目标。

【问题讨论】:

  • 究竟是什么阻止了您在 Java 中实现链接答案?更具体地说明您的问题。
  • 您可以从链接的图像中注意到,当对象(coffret)旋转时,某些角不可见,因此我的算法不起作用。我在 java 中实现了链接的答案,但没有运气,问题仍然存在。

标签: android opencv image-processing image-rotation opencv4android


【解决方案1】:

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))

【讨论】:

  • 重新发布答案并提供更多解释以满足 SO 要求。
猜你喜欢
  • 2015-08-23
  • 2014-03-29
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
相关资源
最近更新 更多