问题描述
可能重复:结果
Rotate CV ::垫使用的CV :: warpAffine抵消目的地形象
code以下能成功地旋转图像,但它切断角落的图像,并将其旋转方向不对!
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旋转图像的背景是黑色的,但我希望它是透明的。
PLUS rotated image's background is black but I want it to be transparent.
难道我做错了什么?
谢谢
Am I doing anything wrong??Thanks
修改解决
首先获得旋转图像的新宽度/高度
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};
//创建新的大小的盒子(newWidth / newHeight)
// create new sized box (newWidth/newHeight)
int pivotX = newWidthHeight[0]/2;
int pivotY = newWidthHeight[1]/2;
//水旋图片
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
Mat targetMat = new Mat(targetSize, scaledImage.type());
int offsetX = (newWidthHeight[0] - scaledImage.width()) / 2;
int offsetY = (newWidthHeight[1] - scaledImage.height()) / 2;
//集中化水印
// Centralizing watermark
Mat waterSubmat = targetMat.submat(offsetY, offsetY + scaledImage.height(), offsetX, offsetX + scaledImage.width());
scaledImage.copyTo(waterSubmat);
Mat rotImage = Imgproc.getRotationMatrix2D(center, waterMarkAngle, 1.0);
Mat resultMat = new Mat(); // CUBIC
Imgproc.warpAffine(targetMat, resultMat, rotImage, targetSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_CONSTANT, colorScalar);
//你用这个样子的 resultMat
// BTW ..还有就是提供链接,该解决方案
// your resultMat with look like this NOT CROPPED Image// BTW.. there is a huge differce between provided link and this solution
推荐答案
我只是通过文档阅读:的
I just read through the documentation: ImgProc.warpAffine
的只是一个摘录:的
void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, [...]);
//Parameters: dsize – Size of the destination image.
请尝试以下操作:
Imgproc.warpAffine(cvImage, dummy, rotImage, dummy.size());
为了与透明度玩,小提琴,并在最后的参数:
In order to play with the transparency, fiddle with the parameter at the end:
Imgproc.warpAffine(cvImage, dummy, rotImage, dummy.size(), INTER_LINEAR, BORDER_TRANSPARENT);
这篇关于在Android的切断图像的边缘OpenCV的图像旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!