如何在Java中使用OpenCV对旋转的图像进行缩放?

3
我正在使用以下方法旋转图像Mat src,角度为angle degrees,利用opencv dll执行此操作。 但是,输出图像需要调整大小和重新缩放。 根据旋转角度如何决定缩放因子以保留原点?目前,我已将缩放因子设置为1.0。 此外,根据旋转角度应如何操纵图像的新尺寸? 1. 在90度旋转后获得的图像: 2. 期望的结果: 如何获得图像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;
    }
2个回答

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

}

0

看看这段代码是否有帮助

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());
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接