android - 在android中使用opencv进行图像旋转会切断图像的边缘

标签 android image opencv rotation

<分区>

Possible Duplicate:
Rotate cv::Mat using cv::warpAffine offsets destination image

下面的代码可以成功地旋转图像,但是它会切掉图像的角并且旋转方向错误!!

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

original Image

rotated Image

PLUS 旋转图像的背景是黑色的,但我希望它是透明的。

我做错什么了吗? 谢谢

编辑已解决

首先获取旋转图像的新宽度/高度

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)

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

//现在创建另一个垫子,以便我们可以将其用于映射

Mat targetMat = new Mat(targetSize, scaledImage.type());

int offsetX = (newWidthHeight[0] - scaledImage.width()) / 2;
int offsetY = (newWidthHeight[1] - scaledImage.height()) / 2;

//集中水印

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 看起来像这样 NOT CROPPED Image //顺便说一句..提供的链接和这个解决方案之间存在巨大差异

最佳答案

我刚刚通读了文档: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());

为了玩弄透明度,摆弄最后的参数:

Imgproc.warpAffine(cvImage, dummy, rotImage, dummy.size(), INTER_LINEAR, BORDER_TRANSPARENT);

关于android - 在android中使用opencv进行图像旋转会切断图像的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12852578/

相关文章:

android - 按下后退箭头图标时如何将 Intent 数据发送回上一个 Activity ?

android - 什么是android :contentDescription ="@string/desc" in ImageView's xml?

python - 如何自动裁剪图片中灰色内的白色部分?

python - 我如何在python中暂停录制屏幕

android如何滚动查看 View 的所有小部件

android - Facebook 奖励视频广告加载失败

android - 在 Android 4.1 到 5.0 的 Cordova 应用程序上启用 TLS 1.2

iphone - MFMailComposeViewController 图像方向

python-3.x - 在 Python 中将 RGBA 转换为 RGB

iphone - 使用 HoughCircles 检测和测量瞳孔和虹膜