sharmishtha kulkarni
sharmishtha kulkarni

Reputation: 81

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

I am using below method to rotate an image Mat src in angle degrees, making use of opencv dll to perform this operation. However, the output image needs to be resized and re-scaled. How the scaling factor should be decided depending on rotation angle so that the origin is retained. Currently I have given scaling factor as 1.0. Also, how the new Size of the image should be manipulated depending on the rotation angle? 1. The image obtained on 90 degrees rotation: 2. Desired result: How can I obtain image no. 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;
    }

Upvotes: 3

Views: 5031

Answers (2)

kg_guo
kg_guo

Reputation: 41

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

}

Upvotes: 4

brad
brad

Reputation: 954

See if this code is helpful

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

Upvotes: 0

Related Questions