1

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

1 回答 1

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;

}

于 2017-04-26T11:07:02.407 回答