OpenCV Crop Image using four points

我的梦境 提交于 2019-12-13 18:06:24

问题


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

回答1:


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;

}



来源:https://stackoverflow.com/questions/15963464/opencv-crop-image-using-four-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!