使用安卓OpenCV进行图像透视变换[英] Image Perspective Transform using Android OpenCV

本文是小编为大家收集整理的关于使用安卓OpenCV进行图像透视变换的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在制作Android应用程序,可以制作透视变换图像. 我想做同样的事情,例如下面的代码.

我尝试过,但我无法阅读C代码.请告知我!

谢谢,

shoichi

示例代码(我想做)

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

我的代码

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

}

推荐答案

您的DST_PNT点与SRC_PNT相同,因此转换矩阵将是身份,并且根本不会更改图像.使用其他点.

其次,我认为第四个论点(尺寸)应该是输出图的大小,因此,如果您想要200x200图像,而不是

Mat outputMat = inputMat.clone();

使用

Mat outputMat = new Mat(200, 200);

本文地址:https://www.itbaoku.cn/post/102647.html

问题描述

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

}

推荐答案

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