2012-02-17 7 views
0

Je fais Android App qui peut faire de l'image de transformation de perspective. Je veux faire la même chose que le code ci-dessous.Image Perspective Transformer en utilisant Android OpenCV

J'ai essayé mais je ne peux pas lire le code C. Conseillez-moi, s'il-vous-plaît!

Merci,

Shoichi

Exemple de code (je veux faire)

#include <cv.h> 
#include <highgui.h> 

int 
main (int argc, char **argv) 
{ 
    IplImage *src_img = 0, *dst_img = 0; 
    CvMat *map_matrix; 
    CvPoint2D32f src_pnt[4], dst_pnt[4]; 

    if (argc >= 2) 
    src_img = cvLoadImage (argv[1], CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); 
    if (src_img == 0) 
    return -1; 
    dst_img = cvCloneImage (src_img); 

    src_pnt[0] = cvPoint2D32f (150.0, 150.0); 
    src_pnt[1] = cvPoint2D32f (150.0, 300.0); 
    src_pnt[2] = cvPoint2D32f (350.0, 300.0); 
    src_pnt[3] = cvPoint2D32f (350.0, 150.0); 

    dst_pnt[0] = cvPoint2D32f (200.0, 200.0); 
    dst_pnt[1] = cvPoint2D32f (150.0, 300.0); 
    dst_pnt[2] = cvPoint2D32f (350.0, 300.0); 
    dst_pnt[3] = cvPoint2D32f (300.0, 200.0); 

    map_matrix = cvCreateMat (3, 3, CV_32FC1); 
    cvGetPerspectiveTransform (src_pnt, dst_pnt, map_matrix); 

    cvWarpPerspective (src_img, dst_img, map_matrix, CV_INTER_LINEAR +  CV_WARP_FILL_OUTLIERS, cvScalarAll (100)); 


    cvNamedWindow ("src", CV_WINDOW_AUTOSIZE); 
    cvNamedWindow ("dst", CV_WINDOW_AUTOSIZE); 
    cvShowImage ("src", src_img); 
    cvShowImage ("dst", dst_img); 
    cvWaitKey (0); 

    cvDestroyWindow ("src"); 
    cvDestroyWindow ("dst"); 
    cvReleaseImage (&src_img); 
    cvReleaseImage (&dst_img); 
    cvReleaseMat (&map_matrix); 

    return 1; 
} 

Mon code

@Override 
public void onCreate(Bundle savedInstanceState) { 
    Log.i(TAG, "onCreate"); 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.main); 
    Resources r = getResources(); 
    Bitmap inputBitmap = BitmapFactory.decodeResource(r, R.drawable.icon); 

    Mat inputMat = Utils.bitmapToMat(inputBitmap); 
    Mat outputMat = inputMat.clone(); 

    List<Point> src_pnt = new ArrayList<Point>(); 
    Point p0 = new Point(75.0, 75.0); 
    src_pnt.add(p0); 
    Point p1 = new Point(75.0, 100.0); 
    src_pnt.add(p1); 
    Point p2 = new Point(100.0, 100.0); 
    src_pnt.add(p2); 
    Point p3 = new Point(100.0, 75.0); 
    src_pnt.add(p3); 
    Mat startM = Converters.vector_Point2f_to_Mat(src_pnt); 

    List<Point> dst_pnt = new ArrayList<Point>(); 
    Point p4 = new Point(75.0, 75.0); 
    dst_pnt.add(p4); 
    Point p5 = new Point(75.0, 100.0); 
    dst_pnt.add(p5); 
    Point p6 = new Point(100.0, 100.0); 
    dst_pnt.add(p6); 
    Point p7 = new Point(100.0, 75.0); 
    dst_pnt.add(p7); 
    Mat endM = Converters.vector_Point2f_to_Mat(dst_pnt); 

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

    Size size = new Size(200.0, 200.0); 
    Scalar scalar = new Scalar(50.0); 

    Imgproc.warpPerspective(inputMat, outputMat, M, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS, Imgproc.BORDER_DEFAULT, scalar); 

    Bitmap outputBitmap = inputBitmap; 
    Utils.matToBitmap(outputMat, outputBitmap); 
    ImageView imageView1 = (ImageView) findViewById(R.id.imageView1); 
    imageView1.setImageBitmap(outputBitmap); 

} 
+0

Avez-vous réussi à faire fonctionner cela? J'ai des problèmes similaires: http://stackoverflow.com/questions/17361693/cant-get-opencvs-warpperspective-to-work-on-android – gleerman

Répondre

0

Vos points de dst_pnt sont les mêmes que src_pnt, de sorte que la matrice de transformation sera l'identité et ne changera pas d'image du tout. Utilisez d'autres points.

En second lieu, je pense que 4ème argument warPerspective (taille) devrait être la taille de outputMap, donc si vous voulez une image 200x200, au lieu de

Mat outputMat = inputMat.clone(); 

Utilisez

Mat outputMat = new Mat(200, 200); 
-1

avait un problème similaire avec cela, mais a trouvé une solution. Voir here pour un exemple de travail.

Questions connexes