gpt4 book ai didi

java - 在 Android 上的 OpenCV 中旋转 VideoCapture

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:31 25 4
gpt4 key购买 nike

在 OpenCV 上使用 VideoCapture 类时如何旋转相机? (Android 上的人脸检测示例)。我正在旋转 Canvas :

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

但来自相机的图像不旋转:人脸检测不工作。

相机从以下位置接收流:

protected Bitmap processFrame(VideoCapture capture) {

capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);

capture.retrieve(mGray,
Highgui.CV_CAP_ANDROID_GREY_FRAME);

更新我做了以下事情:

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

但是不起作用。当我在 portret 方向(在 android 设备上)运行程序时 - 程序不启动当我在横向运行 rogram - 程序工作,但是当我旋转设备时,程序工作,但显示的图像不旋转

最佳答案

您的问题主要是 duplicate of this , 除了您正在寻找 Android 版本。它非常相似,但在这里,可以通过转置然后翻转图像来获得 90º 旋转:

# rotate 90º counter-clockwise
Core.flip(mRgba.t(), mRgba, 0); //mRgba.t() is the transpose

# rotate 90º clockwise
Core.flip(mRgba.t(), mRgba, 1);

对于其他旋转,您可以使用 warpAffine :

Point center = new Point(x,y);
double angle = 90;
double scale = 1.0;

Mat mapMatrix = Imgproc.getRotationMatrix2D(center, angle, scale);
Imgproc.warpAffine(srcMat, dstMat, mapMatrix, Imgproc.INTER_LINEAR);

编辑 - 更完整的示例如下:

    /** 
* Image is first resized-to-fit the dst Mat and then rotated.
* mRgba is the source image, mIntermediateMat should have the same type.
*/
private void rotationTutorial(){
double ratio = mRgba.height() / (double) mRgba.width();

int rotatedHeight = mRgba.height();
int rotatedWidth = (int) Math.round(mRgba.height() * ratio);

Imgproc.resize(mRgba, mIntermediateMat, new Size(rotatedHeight, rotatedWidth));

Core.flip(mIntermediateMat.t(), mIntermediateMat, 0);

Mat ROI = mRgba.submat(0, mIntermediateMat.rows(), 0, mIntermediateMat.cols());

mIntermediateMat.copyTo(ROI);
}


/**
* Image is rotated - cropped-to-fit dst Mat.
*
*/
private void rotationAffineTutorial(){
// assuming source image's with and height are a pair value:
int centerX = Math.round(mRgba.width()/2);
int centerY = Math.round(mRgba.height()/2);

Point center = new Point(centerY,centerX);
double angle = 90;
double scale = 1.0;

double ratio = mRgba.height() / (double) mRgba.width();

int rotatedHeight = (int) Math.round(mRgba.height());
int rotatedWidth = (int) Math.round(mRgba.height() * ratio);

Mat mapMatrix = Imgproc.getRotationMatrix2D(center, angle, scale);

Size rotatedSize = new Size(rotatedWidth, rotatedHeight);
mIntermediateMat = new Mat(rotatedSize, mRgba.type());

Imgproc.warpAffine(mRgba, mIntermediateMat, mapMatrix, mIntermediateMat.size(), Imgproc.INTER_LINEAR);

Mat ROI = mRgba.submat(0, mIntermediateMat.rows(), 0, mIntermediateMat.cols());

mIntermediateMat.copyTo(ROI);
}

注意:

  • 这些示例可能是特定于方向的,我制作它们是为了横向。
  • 您不应为每个视频帧调用这些示例中的代码。有些代码应该只运行一次。

关于java - 在 Android 上的 OpenCV 中旋转 VideoCapture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12949793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com