org.Opencv.core.Mat不能转换为android studio中的java.util.list

时间:2016-10-02 02:39:33

标签: java android opencv

我正在尝试使用opencv 2_4_9来计算直方图以显示我的一个活动,但是这个错误一直在弹出我已经搜索过它到处都找不到任何解决方案专门解决我的问题如果有人可以帮我怎么样解决它.. !!

     private void GetResult()
    {
        try
        {
//            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            img = BitmapFactory.decodeFile(outPutFile.toString());
            image = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
            Utils.bitmapToMat(img, image);

            Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2HSV);

            List<Mat> hsv_planes = new ArrayList<Mat>();
            //splitting into 3 planes r,g,b
            Core.split(image, hsv_planes);
            //the number of bins
            MatOfInt HistSize = new MatOfInt(256);
            //set the ranges for r,g,b
            final MatOfFloat HistRange = new MatOfFloat(0f, 256f);
            boolean accumulate = false;

            Mat h_hist = new Mat();
            Mat s_hist = new Mat();
            Mat v_hist = new Mat();
            //Compute the histograms using calcHist function
            Imgproc.calcHist((List<Mat>) hsv_planes.get(0), new MatOfInt(3), new Mat(), h_hist, HistSize, HistRange, accumulate);
            Imgproc.calcHist((List<Mat>) hsv_planes.get(1), new MatOfInt(3), new Mat(), s_hist, HistSize, HistRange, accumulate);
            Imgproc.calcHist((List<Mat>) hsv_planes.get(2), new MatOfInt(3), new Mat(), v_hist, HistSize, HistRange, accumulate);

            int hist_w = 512;
            int hist_h = 600;
            long bin_w = Math.round((double) hist_w / 256);

            Mat HistImage = new Mat(hist_h, hist_w, CvType.CV_8UC1);
            //Normalization
            Core.normalize(h_hist, h_hist, 3, HistImage.rows(), Core.NORM_MINMAX);
            Core.normalize(s_hist, s_hist, 3, HistImage.rows(), Core.NORM_MINMAX);
            Core.normalize(v_hist, v_hist, 3, HistImage.rows(), Core.NORM_MINMAX);

            for (int i = 1; i < 256; i++) {
                Point p1 = new Point(bin_w * (i - 1), hist_h - Math.round(h_hist.get(i - 1, 0)[0]));
                Point p2 = new Point(bin_w * (i), hist_h - Math.round(h_hist.get(i, 0)[0]));
                Core.line(HistImage, p1, p2, new Scalar(255, 0, 0), 2, 8, 0);

                Point p3 = new Point(bin_w * (i - 1), hist_h - Math.round(s_hist.get(i - 1, 0)[0]));
                Point p4 = new Point(bin_w * (i), hist_h - Math.round(s_hist.get(i, 0)[0]));
                Core.line(HistImage, p3, p4, new Scalar(255, 0, 0), 2, 8, 0);

                Point p5 = new Point(bin_w * (i - 1), hist_h - Math.round(v_hist.get(i - 1, 0)[0]));
                Point p6 = new Point(bin_w * (i), hist_h - Math.round(v_hist.get(i, 0)[0]));
                Core.line(HistImage, p1, p2, new Scalar(255, 0, 0), 2, 8, 0);
            }
            Utils.matToBitmap(HistImage, photoo);
            img_histogram.setImageBitmap(photoo);
        }
        catch (Exception e)
        {
            Toast.makeText(MainActivity.this, e.toString(),Toast.LENGTH_LONG).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您尝试将Mat转换为Mat对象列表,这是不可能的。 初始化不同平面的数组。例如:

        List<Mat> hList = new ArrayList<>();
        hList.add(hsv_planes.get(0));
        List<Mat> sList = new ArrayList<>();
        sList.add(hsv_planes.get(1));
        List<Mat> vList = new ArrayList<>();
        vList.add(hsv_planes.get(2));

        //Compute the histograms using calcHist function
        Imgproc.calcHist(hList, new MatOfInt(3), new Mat(), h_hist, HistSize, HistRange, accumulate);
        Imgproc.calcHist(sList, new MatOfInt(3), new Mat(), s_hist, HistSize, HistRange, accumulate);
        Imgproc.calcHist(vList, new MatOfInt(3), new Mat(), v_hist, HistSize, HistRange, accumulate);