Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中使用Opencv裁剪图像时旋转图像?_Android_Opencv_Image Processing_Image Rotation_Opencv4android - Fatal编程技术网

如何在Android中使用Opencv裁剪图像时旋转图像?

如何在Android中使用Opencv裁剪图像时旋转图像?,android,opencv,image-processing,image-rotation,opencv4android,Android,Opencv,Image Processing,Image Rotation,Opencv4android,我在Android中使用Opencv计算检测到的对象的旋转角度,然后将该对象旋转回其正常位置,以进行进一步的图像分割和对象匹配 这就是我到目前为止得到的 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.getRo

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

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

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());
如果我稍微旋转我的物体,结果就是这样

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

我需要使物体始终居中

我的问题与这个问题相似

但我无法用java实现这一点


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

对于这个问题,我们有一个很好的解释。虽然解决方案是用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))

对这个问题有很好的解释。虽然解决方案是用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))

到底是什么阻止了您在Java中实现链接答案?请更具体地说明您的问题。正如您从链接图像中注意到的,当对象(coffert)旋转时,某些角不可见,因此我的算法无法工作。我用java实现了链接答案,但运气不好,问题一直存在。到底是什么阻止了你用java实现链接答案?请更具体地说明您的问题。正如您从链接图像中注意到的,当对象(coffert)旋转时,某些角不可见,因此我的算法无法工作。我用java实现了链接答案,但运气不好,问题一直存在。重新发布答案时需要更多解释以满足SO要求。重新发布答案时需要更多解释以满足SO要求。