如何在 java 中旋转缩放图像(使用 opencv)?

How to scale an image on rotation (using opencv) in java?

我正在使用以下方法以角度旋转图像 Mat src,利用 opencv dll 执行此操作。 但是,输出图像需要调整大小和重新缩放。 应如何根据旋转角度确定比例因子,以便保留原点。目前我将比例因子设为 1.0。 另外,图像的新尺寸应该如何根据旋转角度进行操作? 1. The image obtained on 90 degrees rotation: 2. Desired result: 我怎样才能获得图像编号。 2?

 private static Mat deskew(Mat src, double angle) {
    Point center = new Point(src.width() / 2, src.height() / 2);
    Mat rotImage = Imgproc.getRotationMatrix2D(center, angle, 1.0);
        Size size = new Size(src.width(), src.height());

        Imgproc.warpAffine(src, src, rotImage, size, Imgproc.INTER_LINEAR
                + Imgproc.CV_WARP_FILL_OUTLIERS);
        return src;
    }

查看此代码是否有帮助

void rotateMatCW(const cv::Mat& src, cv::Mat& dst, const double& deg )
    if (deg == 270 || deg == -90){
        // Rotate clockwise 270 degrees
        cv::transpose(src, dst);
        cv::flip(dst, dst, 0);
    }
    else if (deg == 180 || deg == -180){
        // Rotate clockwise 180 degrees
        cv::flip(src, dst, -1);
    }
    else if (deg == 90 || deg == -270){
        // Rotate clockwise 90 degrees
        cv::transpose(src, dst);
        cv::flip(dst, dst, 1);
    }
    else if (deg == 360 || deg == 0 || deg == -360){
        if (src.data != dst.data){
            src.copyTo(dst);
        }
    }
    else
    {
        cv::Point2f src_center(src.cols / 2.0F, src.rows / 2.0F);
        cv::Mat rot_mat = getRotationMatrix2D(src_center, 360 - deg, 1.0);
        warpAffine(src, dst, rot_mat, src.size());
    }
}
public static void main(String[] args) {
    Mat source = Imgcodecs.imread("e://src//lena.jpg");
    Mat rotMat = new Mat(2, 3, CvType.CV_32FC1);
    Mat destination = new Mat(source.rows(), source.cols(), source.type());
    Point center = new Point(destination.cols() / 2, destination.rows() / 2);
    rotMat = Imgproc.getRotationMatrix2D(center, 30, 1);
    Imgproc.warpAffine(source, destination, rotMat, destination.size());
    Imgcodecs.imwrite("E://out//lena-rotate.jpg", destination);

}