我需要将图像旋转90度,180度或270度.在OpenCV4Android中我可以使用:
Imgproc.getRotationMatrix2D(new Point(center, center), degrees, 1);
Imgproc.warpAffine(src, dst, rotationMatrix, dst.size());
Run Code Online (Sandbox Code Playgroud)
但是,这是我的图像处理算法的一个巨大瓶颈.当然,通过90度的倍数的简单旋转比最一般的情况简单得多warpAffine
,并且可以更有效地完成.例如,对于180度,我可以使用:
Core.flip(src, dst, -1);
Run Code Online (Sandbox Code Playgroud)
其中-1表示翻转水平轴和垂直轴.是否有类似的优化我可以用于90或270度旋转?
在OpenCV上使用类VideoCapture时如何旋转相机?(Android上的样本人脸检测).我正在旋转画布:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
Matrix matrix = new Matrix();
matrix.preTranslate(
(canvas.getWidth() - bmp.getWidth()) / 2,
(canvas.getHeight() - bmp.getHeight()) / 2);
matrix.postRotate(270f, (canvas.getWidth()) / 2,
(canvas.getHeight()) / 2);
canvas.drawBitmap(bmp, matrix, null);
}
Run Code Online (Sandbox Code Playgroud)
但来自相机的图像不会旋转:面部检测不起作用.
摄像机从以下位置接收流:
protected Bitmap processFrame(VideoCapture capture) {
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
capture.retrieve(mGray,
Highgui.CV_CAP_ANDROID_GREY_FRAME);
Run Code Online (Sandbox Code Playgroud)
更新我做了以下事情:
@Override
protected Bitmap processFrame(VideoCapture capture) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Core.flip(mRgba.t(), mRgba, 0);
}
else {
}
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
capture.retrieve(mDetect_thread.mGray,
Highgui.CV_CAP_ANDROID_GREY_FRAME);
Run Code Online (Sandbox Code Playgroud)
但是不行.当我在portret方向运行程序(在Android设备上) - 程序不启动当我以横向方向运行rogram - 程序工作,但当我旋转设备,程序工作,但显示图像不旋转
我们的计算机一次有数百张图像,我们需要尽可能快地旋转和调整它们.旋转完成90度,180度或270度.
目前我们使用命令行工具GraphicsMagick来旋转图像.旋转图像(5760*3840~22MP)大约需要4到7秒.
以下python代码遗憾地给了我们相同的结果
import cv
img = cv.LoadImage("image.jpg")
timg = cv.CreateImage((img.height,img.width), img.depth, img.channels) # transposed image
# rotate counter-clockwise
cv.Transpose(img,timg)
cv.Flip(timg,timg,flipMode=0)
cv.SaveImage("rotated_counter_clockwise.jpg", timg)
Run Code Online (Sandbox Code Playgroud)
是否有更快的方式使用显卡的电源旋转图像?我们想到了OpenCL和OpenGL,但我们想知道性能提升是否值得注意.
我们使用的硬件相当有限,因为设备应尽可能小.
该软件是官方(闭源)radeon驱动程序的debian 6.