我最近一直在使用OpenCV for Android,通过Google的Tesseract库预处理用于光学字符识别的图像 . 一种用于改善图像质量的方法是迭代像素块(~64×64)并基于该区域的平均强度应用阈值 .

我在迭代已保存位图的区域时遇到了问题 . 当我访问 Mat.total() 时,我在图像中获得了正确的像素数,但是基于 Mat.rows()Mat.cols() 的循环将阈值应用于文本的大区域,这迫使我一次迭代一行或两行 . 是否还有另一种操纵它的方法我错过了?我是否错误地初始化了Mat / Bitmap?在具有数千到数百万像素的图像中,我可以使用~100行/列 .

这就是我所拥有的:

// set the bmp to the correct type (just gets the original Bitmap in an asy
    Bitmap img = params[0].copy(Bitmap.Config.ARGB_8888, true);

    // begin image pre-processing by applying threshold and inverting the colors
    Mat mat = new Mat();
    Utils.bitmapToMat(img, mat);

    Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY);
    Core.bitwise_not(mat, mat); // invert the colors for readability

    for(int row = 0; row < mat.rows(); row += 2) {
        for(int col = 0; col < mat.cols(); col += 2) {
            Mat roi = mat.submat(row, row + 1, col, col + 1);
            // gets val[0] because there is only one channel to work with in the
            // grayscale image
            double averageIntensity = Core.mean(roi).val[0];
            // apply the threshold to this square minus a constant "delta" for leeway
            Imgproc.threshold(roi, roi, averageIntensity - THRESHOLD_DELTA, 255.0, Imgproc.THRESH_BINARY);
        }
    }

任何帮助表示赞赏!