问题一:
我试过什么?
我正在构建具有文档识别和透视校正功能的文档扫描仪应用程序。为此,我正在使用 OpenCV。以下是步骤:
Bitmap bitmap = CameraImageUtils.decodeSampledBitmapFromResource(data[0], CameraConfigurationUtils.MIN_FRAME_WIDTH, CameraConfigurationUtils.MIN_FRAME_HEIGHT);
Mat srcMat = new Mat(height, width, CvType.CV_8UC3);
Utils.bitmapToMat(sourceBitmap, srcMat);
Mat imgSource = new Mat(srcMat.size(), CvType.CV_8UC1);
Imgproc.cvtColor(srcMat, imgSource, Imgproc.COLOR_RGB2GRAY, 4);
Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(5, 5), 5);
Imgproc.Canny(imgSource, imgSource, 50, 50);
//find the contours
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1;
Log.d("size", Integer.toString(contours.size()));
MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint largest_contour = contours.get(0);
for (int idx = 0; idx < contours.size(); idx++) {
temp_contour = contours.get(idx);
double contourarea = Imgproc.contourArea(temp_contour);
//compare this contour to the previous largest contour found
if (contourarea > maxArea) {
//check if this contour is a square
MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray());
int contourSize = (int) temp_contour.total();
MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true);
if (approxCurve_temp.total() == 4) {
maxArea = contourarea;
maxAreaIdx = idx;
approxCurve = approxCurve_temp;
largest_contour = temp_contour;
}
}
}
问题是什么 ?
我正在尝试绘制所有轮廓,但文档本身从未被识别为轮廓。
我在这里做错了什么?该应用程序的主要目标是在实时相机预览中检测文档,当用户拍照时,仅显示缩小尺寸(< 160kb)的文档并上传图片。
问题二:
我正在为 OpenCv 使用静态初始化程序,这会增加应用程序的大小,我不想要这个。 OpenCV 有替代方案吗?
最佳答案
我无法发表评论,但我会尝试为您的 做出回答问题一 .
您的原始图像在哪里?
你需要有类似 的东西二进制图像 它由 0 和 255 个彩色像素组成,是一个灰度图像(所以你只有 1 个 channel )。您需要完整的图像 block 才能使用 OpenCV findContour()
发挥作用。
到那时我相信你会找到你的轮廓。您的代码没有问题。
PS。您甚至不需要使用 Canny 进行轮廓检测。仅当您真的想获得图像的边缘时才使用它。
希望能帮助到你。
关于android - 使用 OpenCV 检测最大轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39008380/