2012-10-18 40 views
9

Làm cách nào để xoay camera khi sử dụng lớp VideoCapture trên OpenCV? (Phát hiện khuôn mặt mẫu trên Android). Tôi đang xoay canvas với:Xoay VideoCapture trong OpenCV trên 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); 
} 

nhưng hình ảnh từ Máy ảnh không xoay: Không phát hiện khuôn mặt.

Chiếc máy ảnh này nhận được dòng sau:

protected Bitmap processFrame(VideoCapture capture) { 

    capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); 

    capture.retrieve(mGray, 
    Highgui.CV_CAP_ANDROID_GREY_FRAME); 

CẬP NHẬT tôi đã làm như sau:

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

Nhưng là không làm việc. Khi tôi chạy chương trình theo hướng portret (trên thiết bị android) - chương trình không khởi động Khi tôi chạy rogram theo hướng ngang - công việc lập trình, nhưng khi tôi xoay thiết bị, chương trình làm việc, nhưng hình ảnh trên màn hình không quay

Trả lời

9

Câu hỏi của bạn hầu hết là một duplicate of this, ngoại trừ việc bạn đang tìm kiếm phiên bản Android. Nó là khá tương tự nhưng ở đây nó là, 90º xoay có thể thu được bằng cách transposing và sau đó lật hình ảnh:

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

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

Đối với phép quay khác mà bạn có thể sử dụng 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); 

EDIT - hơn ví dụ hoàn chỉnh theo:

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

Lưu ý:

  • Những ví dụ này có thể định hướng cụ thể, tôi đã làm cho chúng theo hướng ngang.
  • Bạn không nên gọi mã từ các ví dụ này cho mỗi khung hình video. Một số mã chỉ nên chạy một lần.
+0

Tắt khóa học hoạt động. Đó là nhận diện khuôn mặt không hoạt động ngoài hộp với hình ảnh được xoay. –

+0

Điều này không hoạt động – gregm

+0

Bạn có thể xây dựng? –

0

Nếu bạn chỉ cần xoay 90, 180 hoặc 270 độ (trường hợp này có vẻ là trường hợp của bạn), bạn nên sử dụng Core.flip() nhanh hơn. Dưới đây là một phương pháp thực hiện điều đó cho bạn:

public static Mat rotate(Mat src, double angle) 
{ 
    Mat dst = new Mat(); 
    if(angle == 180 || angle == -180) { 
     Core.flip(src, dst, -1); 
    } else if(angle == 90 || angle == -270) { 
     Core.flip(src.t(), dst, 1); 
    } else if(angle == 270 || angle == -90) { 
     Core.flip(src.t(), dst, 0); 
    } 

    return dst; 
}