Member 13882534 Ответов: 0

В формате OpenCV findcontour на Андроид Студио


I am working on a project to detect wounds, the type of wounds are as follow in the attached named(original).

I had tried with the following method to detect the area of wound that is of interest. However, the result of the detection is not of what i want to achieved(see attached named(outputFromAboveMethod)). The final outcome i wish to achieve is in the attached named(WhatIWant)

Could anyone help me pls.


Оригинал
outputFromAboveMethod
whatIWant

Что я уже пробовал:

public class DetectTask extends AsyncTask<Integer, Bitmap, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dlg.setMessage("Processing");
            dlg.show();
        }

        @Override
        protected Bitmap doInBackground(Integer... params) {
            Mat mat = new Mat(bitmap.getWidth(), bitmap.getHeight(), CvType.CV_8UC3);
            Utils.bitmapToMat(bitmap, mat);

            Mat rgbMat = new Mat();
            Imgproc.cvtColor(mat, rgbMat, Imgproc.COLOR_RGBA2BGR);

            Mat dilatedMat = new Mat();
            Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(7, 7));
            Imgproc.morphologyEx(rgbMat, dilatedMat, Imgproc.MORPH_OPEN, kernel);

            //red
            Mat redMat = new Mat();
            Core.inRange(rgbMat, new Scalar(0, 0, 120), new Scalar(100, 100, 255), redMat);

            //find contour
            Mat hierarchy = new Mat();
            List<MatOfPoint> contours = new ArrayList<>();

            Imgproc.findContours(redMat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
            double largest_area =0;
            int largest_contour_index = 0;

            for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
                double contourArea = Imgproc.contourArea(contours.get(contourIdx));
                if (contourArea > largest_area) {
                    largest_area = contourArea;
                    largest_contour_index = contourIdx;
                }
            }

            Imgproc.drawContours(mat, contours, largest_contour_index, new Scalar(0, 255, 0, 255), 3);
            Bitmap outputImage= Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(mat, outputImage);

            return outputImage;
        }

        @Override
        protected void onPostExecute(final Bitmap outputImage) {
            imageview.setImageBitmap(outputImage);
            dlg.dismiss();

        }
    }

0 Ответов