OpenCV裁剪图像使用四点

时间:2013-04-12 05:04:44

标签: java android image opencv crop

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

1 个答案:

答案 0 :(得分:0)

在这种方法中,您可以获得四个点和文档,然后您可以使用以下方法剪切此图像:

       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;

}