[英]How to crop rectangles in opencv?
我设法从捕获的图像中绘制两个矩形。 问题是,如何裁剪它们并单独保存它们,以便我可以在每个OCR中执行我的OCR? 我尝试搜索,但找不到想要的东西...
到目前为止我所做的..
Mat imageMat = new Mat();
Mat resizeImg = new Mat();
Mat hierarchy = new Mat();
//Size(width,height)
Size imageSize = new Size(1000,800);
Utils.bitmapToMat(image, imageMat);
//Resize Image
Imgproc.resize(imageMat,resizeImg,imageSize);
//convert the resized image to bitmap
image = Bitmap.createBitmap(resizeImg.cols(), resizeImg.rows(),
Bitmap.Config.ARGB_8888);
//convert back to Mat
Utils.bitmapToMat(image, imageMat);
//Create rectangles
Imgproc.rectangle(imageMat,new Point(0,150), new Point(500,250), new Scalar(255, 0, 255),1);
Imgproc.rectangle(imageMat,new Point(0,500), new Point(650,650), new Scalar(255, 0, 255),1);
//What I have tried doing
Rect rectCrop = new Rect(0,150,500,250);
Mat image_output= imageMat.submat(rectCrop);
Bitmap Cropimage = Bitmap.createBitmap(imageMat.cols(), imageMat.rows(), Bitmap.Config.ARGB_8888);
Utils.bitmapToMat(Cropimage, image_output);
//TODO: save only the content in the rectangles to MAT
//testing purpose
Utils.matToBitmap(imageMat,image);
imageView.setImageBitmap(image);
这是获取矩形裁剪图像的方法。
Rect rect = new Rect(x1, y1, x2, y2);
Mat cropedMat = new Mat(mat, rect);
如果需要,可以将裁剪的垫转换为位图
Bitmap Cropedimage = Bitmap.createBitmap(cropedMat.cols(), cropedMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(cropedMat, Cropedimage);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.