使用Android Opencv计算直方图中曲线下的面积

时间:2019-03-14 12:28:18

标签: java android opencv

我正在做一个Android项目,其中在图片中有两个点,我需要计算两个点的平均黑白。一个是小而略微透明的,另一个是黑又大的圆圈(点)。所以我想计算平均b / w或差b / w。点的颜色为红色。我在Matlab中通过制作直方图并选择两个直方图来做到这一点。 Matlab给我曲线下的面积,然后我可以轻松计算出两个图像的差值或平均黑白。我制作了直方图,但无法获得平均黑白两点。我的Opencv直方图代码是

Mat rgba = new Mat();
                Utils.bitmapToMat(bitmap, rgba);

                // Get the bitmap size.
                Size rgbaSize = rgba.size();
                // Set the amount of bars in the histogram.
                int histSize = 256;
                MatOfInt histogramSize = new MatOfInt(histSize);

                // Set the height of the histogram and width of the bar.
                int histogramHeight = (int) rgbaSize.height;
                int binWidth = 5;

                // Set the value range.
                MatOfFloat histogramRange = new MatOfFloat(0f, 256f);

                // Create two separate lists: one for colors and one for channels (these will be used as separate datasets).
                Scalar[] colorsRgb = new Scalar[]{new Scalar(220, 0, 0, 255), new Scalar(0, 200, 0, 255), new Scalar(0, 0, 200, 255)};
                MatOfInt[] channels = new MatOfInt[]{new MatOfInt(0), new MatOfInt(1), new MatOfInt(2)};

                // Create an array to be saved in the histogram and a second array, on which the histogram chart will be drawn.
                Mat[] histograms = new Mat[]{new Mat(), new Mat(), new Mat()};
                Mat histMatBitmap = new Mat(rgbaSize, CvType.CV_8UC3, new Scalar(256, 256, 256));
               // Mat histMatBitmap = new Mat(rgbaSize, rgba.type());

                for ( int i = 0; i < channels.length; i++ ) {
                    Imgproc.calcHist(Collections.singletonList(rgba), channels[0], new Mat(), histograms[i], histogramSize, histogramRange);
                    Core.normalize(histograms[i], histograms[i], histogramHeight, 0, Core.NORM_INF);
                    for ( int j = 0; j < histSize; j++ ) {
                        Point p1 = new Point(binWidth * (j - 1), histogramHeight - Math.round(histograms[i].get(j - 1, 0)[0]));
                        Point p2 = new Point(binWidth * j, histogramHeight - Math.round(histograms[i].get(j, 0)[0]));
                        Imgproc.line(histMatBitmap, p1, p2, colorsRgb[0], 3, 8, 0);
                    }
                }


                //convert Mat to bitmap
                Bitmap graphBitmap = Bitmap.createBitmap(histMatBitmap.cols(), histMatBitmap.rows(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(histMatBitmap, graphBitmap);

                // show histogram
                histogramImageView.setImageBitmap(graphBitmap)

原始代码Link

我的图片是 enter image description here

结果图像为enter image description here

0 个答案:

没有答案