2013-06-28 18 views
5

私はAndroidアプリケーションでquad-quadシステムを実装するのに苦労しています。目的は、ユーザーが写真を撮り、4つのコーナーポイントを追加し、そのクワッドをイメージから四角形として抽出させることです。OpenCVのwarpPerspectiveをAndroidで動作させることができません

私はとthis questionを見て、これにOpenCVを使用しました。結果のコードは以下です:テスト中

public static Bitmap warp(Bitmap image, MyPoint p1, MyPoint p2, MyPoint p3, MyPoint p4) { 
    int resultWidth = 500; 
    int resultHeight = 500; 

    Mat inputMat = new Mat(image.getHeight(), image.getHeight(), CvType.CV_8UC4); 
    Utils.bitmapToMat(image, inputMat); 
    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4); 

    Point ocvPIn1 = new Point(p1.getX(), p1.getY()); 
    Point ocvPIn2 = new Point(p2.getX(), p2.getY()); 
    Point ocvPIn3 = new Point(p3.getX(), p3.getY()); 
    Point ocvPIn4 = new Point(p4.getX(), p4.getY()); 
    List<Point> source = new ArrayList<Point>(); 
    source.add(ocvPIn1); 
    source.add(ocvPIn2); 
    source.add(ocvPIn3); 
    source.add(ocvPIn4); 
    Mat startM = Converters.vector_Point2f_to_Mat(source); 

    Point ocvPOut1 = new Point(0, 0); 
    Point ocvPOut2 = new Point(0, resultHeight); 
    Point ocvPOut3 = new Point(resultWidth, resultHeight); 
    Point ocvPOut4 = new Point(resultWidth, 0); 
    List<Point> dest = new ArrayList<Point>(); 
    dest.add(ocvPOut1); 
    dest.add(ocvPOut2); 
    dest.add(ocvPOut3); 
    dest.add(ocvPOut4); 
    Mat endM = Converters.vector_Point2f_to_Mat(dest);  

    Mat perspectiveTransform = new Mat(3, 3, CvType.CV_32FC1); 
    Core.perspectiveTransform(startM, endM, perspectiveTransform); 

    Imgproc.warpPerspective(inputMat, 
          outputMat, 
          perspectiveTransform, 
          new Size(resultWidth, resultHeight), 
          Imgproc.INTER_CUBIC); 

    Bitmap output = Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.RGB_565); 
    Utils.matToBitmap(outputMat, output); 
    return output; 
} 

、私は、コーナーポイントの順序は、左上、左下、右下、右上であることを確認してください。

奇妙なことは、結果が常に同じではないということです。たいていの場合、それは単一の色の四角形、時には黒い四角形、時には色が異なる対角線を表示します。 startM = endMを試しても、非確定的な振る舞いになります。

私はここで何が欠けていますか?

+0

これはいくらか動作しますが、何度かm最終的なビットマップの回転および/または回転コピーを生成する。 ?これが欲しいのですか? –

答えて

5

はそれを見つけ、問題は、これらの行にあった。このことにより、交換する必要があります

Mat perspectiveTransform = new Mat(3, 3, CvType.CV_32FC1); 
Core.perspectiveTransform(startM, endM, perspectiveTransform); 

Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM); 
+1

ありがとう@Aegoinsは私の努力を大いに救った。 – DearDhruv

+0

こんにちは、私はあなたと同じことに従っていますが、私は奇妙な出力を得ています。実際には私の入力は名刺であり、透視変換を使ってイメージからカードだけを取り出したいと思っています。私が得ている出力イメージは、カードの色の1つからの色を持つ単なるブランクのカラーイメージです。 – Manpreet

+0

@Aegonisこれはいくつかの仕組みですが、最終的なビットマップのミラーコピーを与えることがあります。 ?これが欲しいのですか? –

1

私はあなたのコードといくつかの問題を抱えていたので、私が得るまで、私は変更を加えました実際のバージョンでは、質問コードに問題があった場合、私のコードは次のようになります。

Bitmap warp(Bitmap image, Point topLeft, Point topRight, Point bottomLeft, Point bottomRight) { 
    int resultWidth = (int)(topRight.x - topLeft.x); 
    int bottomWidth = (int)(bottomRight.x - bottomLeft.x); 
    if(bottomWidth > resultWidth) 
     resultWidth = bottomWidth; 

    int resultHeight = (int)(bottomLeft.y - topLeft.y); 
    int bottomHeight = (int)(bottomRight.y - topRight.y); 
    if(bottomHeight > resultHeight) 
     resultHeight = bottomHeight; 

    Mat inputMat = new Mat(image.getHeight(), image.getHeight(), CvType.CV_8UC1); 
    Utils.bitmapToMat(image, inputMat); 
    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC1); 

    List<Point> source = new ArrayList<>(); 
    source.add(topLeft); 
    source.add(topRight); 
    source.add(bottomLeft); 
    source.add(bottomRight); 
    Mat startM = Converters.vector_Point2f_to_Mat(source); 

    Point ocvPOut1 = new Point(0, 0); 
    Point ocvPOut2 = new Point(resultWidth, 0); 
    Point ocvPOut3 = new Point(0, resultHeight); 
    Point ocvPOut4 = new Point(resultWidth, resultHeight); 
    List<Point> dest = new ArrayList<>(); 
    dest.add(ocvPOut1); 
    dest.add(ocvPOut2); 
    dest.add(ocvPOut3); 
    dest.add(ocvPOut4); 
    Mat endM = Converters.vector_Point2f_to_Mat(dest); 

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM); 

    Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight)); 

    Bitmap output = Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.ARGB_8888); 
    Utils.matToBitmap(outputMat, output); 
    return output; 
} 
+0

このコードはうまく動作しますか、それは私のために悪くないです – Ata

+0

@Ata何が問題でしたか? – jonathanrz

関連する問題