2012-10-18 16 views
9

OpenCV'de VideoCapture sınıfını kullanırken kamerayı nasıl döndürürün? (Android'de Örnek Yüz Algılama).Android'de OpenCV'de VideoCapture'ı Döndür

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

ama Kameradan görüntü dönmez: Birlikte tuval dönen ediyorum Yüz dont iş algıla.

kamera aşağıdakilerden akışı alır:

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

Ama dont iş geçerli:

protected Bitmap processFrame(VideoCapture capture) { 

    capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); 

    capture.retrieve(mGray, 
    Highgui.CV_CAP_ANDROID_GREY_FRAME); 

GÜNCELLEME aşağıdaki yaptım. Ben program yönelimi (android cihaz üzerinde) koştuğumda - program başlatmayın Ben rogram koştururken yatay yönde - program çalışma, ama ben cihaz döndürmek, program iş, ama görüntü ekranda dönme

cevap

9

Sorunuz, Android sürümünü aradığınız dışında çoğunlukla duplicate of this. Oldukça benzer ama burada 90º dönme görüntüyü aktaran ve sonra saygısız elde edilebilir, geçerli:

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

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

sen warpAffine kullanabileceğiniz diğer rotasyonlar için:

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 - daha tam örnekler izleyin:

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

Not:

  • Bu örnekler yönlendirmeye özgü olabilir, onları yatay yönlendirme için yaptım.
  • Her video karesi için bu örneklerden kodu çağırmamalısınız. Bazı kodlar sadece bir kez çalıştırılmalıdır.
+0

Elbette çalışıyor. Döndürülmüş görüntüler ile kutudan çıkmayan yüz tanımadır. –

+0

Bu işe yaramıyor – gregm

+0

Ayrıntı yapabilir misiniz? –

0

Yalnızca 90, 180 veya 270 derece döndürme yapmanız gerekiyorsa (Durumunuz böyle görünüyor), daha hızlı olan Core.flip() öğesini daha iyi kullanırsınız. İşte sizin için bir yöntem aşağıda:

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