Image Perspective Using Android OpenCV

I am creating an Android application that can convert a perspective image. I want to do the same as the code below.

I tried, but I could not read the C code. Please tell 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); } 
0
source share
2 answers

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

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

 Mat outputMat = inputMat.clone(); 

Using

 Mat outputMat = new Mat(200, 200); 
0
source

Had a similar problem with this, but found a solution. See here for a working example.

-1
source

Source: https://habr.com/ru/post/948311/


All Articles