'Image Perspective Transform using Android OpenCV

I am making Android App which can make Perspective Transform Image. I want to do same thing like below code.

I tried but I cant read C code. Please advise me!

Thanks,

Shoichi

Sample Code(I want to do)

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

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

}


Solution 1:[1]

Your dst_pnt points are same as src_pnt, so transformation matrix will be identity and will not change image at all. Use some other points.

Secondly, I think 4th argument to warPerspective (size) should be size of outputMap, so if you want a 200x200 image,instead of

Mat outputMat = inputMat.clone();

Use

Mat outputMat = new Mat(200, 200);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomasz Niedabylski