每次都有不同的结果

时间:2020-06-16 11:48:55

标签: android opencv

我是android上的新程序员。 当我在Java上运行代码时,得到的结果是:i = 0和j = 0的图像蒙版: enter image description here

    private void recognizeAnswers(){

    for(int i = 0; i< bubbles.size(); i+=options.length) {

        List<MatOfPoint> rows = bubbles.subList(i, i+options.length);

        int[][] filled = new int[rows.size()][4];

        for (int j = 0; j < rows.size(); j++) {

            MatOfPoint col = rows.get(j);
            List<MatOfPoint> list = Arrays.asList(col);
            Mat mask = new Mat(thresh.size(), CvType.CV_8UC1);
            drawContours(mask.submat(roi), list, -1, new Scalar(255, 0, 0), -5);
            Mat conjuction = new Mat(thresh.size(), CvType.CV_8UC1);
            Core.bitwise_and(thresh, mask, conjuction);

但是,当我在Adroid Studio上运行相同的代码时,得到的结果是:i = 0和j = 0的图像蒙版: enter image description here

这对于第一张图像是错误的,在两种情况下其他图像都是相同的。两种情况下的变量列表都相同。

当我在下面的代码中实现saveImage方法时,更感到惊讶的是,android中的所有图像都会出错:

private void recognizeAnswers(){

    for(int i = 0; i< bubbles.size(); i+=options.length) {

        List<MatOfPoint> rows = bubbles.subList(i, i+options.length);

        int[][] filled = new int[rows.size()][4];

        for (int j = 0; j < rows.size(); j++) {

            MatOfPoint col = rows.get(j);
            List<MatOfPoint> list = Arrays.asList(col);
            Mat mask = new Mat(thresh.size(), CvType.CV_8UC1);
            drawContours(mask.submat(roi), list, -1, new Scalar(255, 0, 0), -5);
            Mat conjuction = new Mat(thresh.size(), CvType.CV_8UC1);
            Core.bitwise_and(thresh, mask, conjuction);
            saveImage(mask,"mask", i, j);

这是saveImage方法:

   private void SaveImage(Mat Image, String n, int i, int j, String Folder) {
    Bitmap finalBitmap = Bitmap.createBitmap(Image.cols(), Image.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(Image, finalBitmap);
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images" + Folder);
    ///System.out.println("mydir: " + myDir);
    if (!myDir.exists()) {
        myDir.mkdirs();
    }
    Random generator = new Random();
    String fname = n + "_" + i +"_" + j +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ())
        file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

谁能告诉我解决错误的原因和解决方法? 谢谢! 对不起,我的英语知识!

0 个答案:

没有答案