8
Possible Duplicate:
Rotate cv::Mat using cv::warpAffine offsets destination image
Code below can rotate an image successfully but it cuts off corners of the image and it rotates in wrong direction!!
Mat cvImage = Highgui.imread("mnt/sdcard/canvasgrid.png");
int degrees = 20;
Point center = new Point(cvImage.cols()/2, cvImage.rows()/2);
Mat rotImage = Imgproc.getRotationMatrix2D(center, degrees, 1.0);
Mat dummy = cvWaterImage;
Imgproc.warpAffine(cvImage, dummy, rotImage, cvImage.size());
rotatedImage = dummy;
Highgui.imwrite("mnt/sdcard/imageRotate.png",rotatedImage);
PLUS rotated image's background is black but I want it to be transparent.
Am I doing anything wrong?? Thanks
EDIT SOLVED
first of all get new width/height of rotated image
double radians = Math.toRadians(rotationAngle);
double sin = Math.abs(Math.sin(radians));
double cos = Math.abs(Math.cos(radians));
int newWidth = (int) (scaledImage.width() * cos + scaledImage.height() * sin);
int newHeight = (int) (scaledImage.width() * sin + scaledImage.height() * cos);
int[] newWidthHeight = {newWidth, newHeight};
// create new sized box (newWidth/newHeight)
int pivotX = newWidthHeight[0]/2;
int pivotY = newWidthHeight[1]/2;
// rotating water image
org.opencv.core.Point center = new org.opencv.core.Point(pivotX, pivotY);
Size targetSize = new Size(newWidthHeight[0], newWidthHeight[1]);
// now create another mat, so we can use it for mapping
Ma