Skip to content Skip to sidebar Skip to footer

Rotate Videocapture In Opencv On Android

How to rotate the camera when using class VideoCapture on OpenCV? (Sample Face Detection on Android). I'm rotating the canvas with: if (getResources().getConfiguration().orientati

Solution 1:

Your question is mostly a duplicate of this, except that you are looking for the Android version. It is quite similar but here it is, 90º rotation can be obtained by transposing and then flipping the image:

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

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

For other rotations you can use warpAffine:

Pointcenter=newPoint(x,y);
doubleangle=90;
doublescale=1.0;

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

EDIT - more complete examples follow:

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

        introtatedHeight= mRgba.height();     
        introtatedWidth= (int) Math.round(mRgba.height() * ratio);

        Imgproc.resize(mRgba, mIntermediateMat, newSize(rotatedHeight, rotatedWidth));

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

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

        mIntermediateMat.copyTo(ROI);       
    }


    /** 
     * Image is rotated - cropped-to-fit dst Mat.
     * 
     */privatevoidrotationAffineTutorial(){
        // assuming source image's with and height are a pair value:intcenterX= Math.round(mRgba.width()/2);
        intcenterY= Math.round(mRgba.height()/2);

        Pointcenter=newPoint(centerY,centerX);
        doubleangle=90;
        doublescale=1.0;

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

        introtatedHeight= (int) Math.round(mRgba.height());       
        introtatedWidth= (int) Math.round(mRgba.height() * ratio);

        MatmapMatrix= Imgproc.getRotationMatrix2D(center, angle, scale);

        SizerotatedSize=newSize(rotatedWidth, rotatedHeight);
        mIntermediateMat = newMat(rotatedSize, mRgba.type());

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

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

        mIntermediateMat.copyTo(ROI);
    }

Note:

  • These examples might be orientation-specific, I made them for landscape orientation.
  • You should not call the code from these examples for every video frame. Some of the code should only run once.

Solution 2:

If you only need to do 90, 180, or 270 degrees rotation (Which seems to be your case) you better use Core.flip() which is faster. Here below a method that does it for you:

publicstatic Mat rotate(Mat src, double angle)
{
    Matdst=newMat();
    if(angle == 180 || angle == -180) {
        Core.flip(src, dst, -1);
    } elseif(angle == 90 || angle == -270) {
        Core.flip(src.t(), dst, 1);
    } elseif(angle == 270 || angle == -90) {
        Core.flip(src.t(), dst, 0);
    }

    return dst;
}

Post a Comment for "Rotate Videocapture In Opencv On Android"