使用四点OpenCV裁剪图像 [英] OpenCV Crop Image using four points

查看:1039
本文介绍了使用四点OpenCV裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试裁剪图像,然后才能显示图像。我在两个数组中有四个点,它会形成一个矩形。我有一个代码,我认为可以完成这项工作,但它给出了一个错误。这是我希望你可以帮助我的代码。我正在使用android并且只使用Java。

I am trying to crop an image then be able to display the image. I have four points in two arrays and it would make a rectangle shape. I have a code that i think would do the job but it is giving an error. Here is the code i hope you can help me out. I am working with android and only with Java.

int startX =locListX.get(0);
int startY =locListY.get(0);
Collections.sort(locListX);
Collections.sort(locListY);
int width=locListX.get(3)-locListX.get(0);
int height=locListY.get(3)-locListY.get(0);
Mat image = mFind;
Rect rectCrop = new Rect(startX, startY, width, height);
Mat imCrop=  new Mat(image,rectCrop); 
Utils.matToBitmap(imCrop, bmp5);
mGrayView.setImageBitmap(bmp5);


推荐答案

在这种方法中,你得到四点文件然后你可以使用下面的方法剪切这个图像:

In this method you get four point and mat of document then you can cut this image using below method:

       public Bitmap warpDisplayImage(Mat inputMat) {
List<Point> newClockVisePoints = new ArrayList<>();

int resultWidth = inputMat.width();
int resultHeight = inputMat.height();

Mat startM = Converters.vector_Point2f_to_Mat(orderRectCorners(Previes method four poit list(like : List<Point> points)));

Point ocvPOut4 = new Point(0, 0);
Point ocvPOut1 = new Point(0, resultHeight);
Point ocvPOut2 = new Point(resultWidth, resultHeight);
Point ocvPOut3 = new Point(resultWidth, 0);



    ocvPOut3 = new Point(0, 0);
    ocvPOut4 = new Point(0, resultHeight);
    ocvPOut1 = new Point(resultWidth, resultHeight);
    ocvPOut2 = new Point(resultWidth, 0);
}

Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);

List<Point> dest = new ArrayList<Point>();
dest.add(ocvPOut3);
dest.add(ocvPOut2);
dest.add(ocvPOut1);
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), Imgproc.INTER_CUBIC);


Bitmap descBitmap = Bitmap.createBitmap(outputMat.cols(), outputMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(outputMat, descBitmap);



return descBitmap;

}

这篇关于使用四点OpenCV裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆