2

我一直在摆弄 OpenCV 中的凸包,试图检测不规则形状的伤口(以便稍后使用参考标记计算其大小)。问题是伤口颜色不规则导致阈值问题。我无法完全弄清楚我需要什么样的预处理或如何设置适用的颜色范围。

我尝试应用高斯模糊、膨胀和摆弄红色范围,但到目前为止无济于事。

private void detectWound(String procpath) {

Bitmap bitmap = BitmapFactory.decodeFile(procpath);

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 ghierarchy = new Mat();
List<MatOfPoint> gcontours = new ArrayList<>();
Mat rhierarchy = new Mat();
List<MatOfPoint> rcontours = new ArrayList<>();
Imgproc.findContours(redMat, rcontours, rhierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

List<MatOfPoint> rhullList = new ArrayList<>();
    for (MatOfPoint contour : rcontours) {
        MatOfInt hull = new MatOfInt();
        Imgproc.convexHull(contour, hull);
        Point[] contourArray = contour.toArray();
        Point[] hullPoints = new Point[hull.rows()];
        List<Integer> hullContourIdxList = hull.toList();
        for (int i = 0; i < hullContourIdxList.size(); i++) {
            hullPoints[i] = contourArray[hullContourIdxList.get(i)];
        }
        rhullList.add(new MatOfPoint(hullPoints));
    }

    double rlargest_area =0;
    int rlargest_contour_index = 0;
    for (int contourIdx = 0; contourIdx < rcontours.size(); contourIdx++) {
        double contourArea = Imgproc.contourArea(rcontours.get(contourIdx));
        if (contourArea > rlargest_area) {
            rlargest_area = contourArea;
            rlargest_contour_index = contourIdx;
        }
    }

    double rcurrentMax = 0;
    for (MatOfPoint c: rhullList){
        double area= Imgproc.contourArea(c);
        if(area>rcurrentMax){
            rcurrentMax = area;
        }
    }

    Imgproc.drawContours(mat, rhullList, rlargest_contour_index, new Scalar(0, 255, 0, 255), 3);
}

预期结果:https ://i.ibb.co/TrmyDG0/Inkedmotasem2-LI.jpg

实际结果:https ://i.ibb.co/ccdkCkj/motasem2-jpg-copy2.png

我将不胜感激任何提示我朝着正确方向前进!

4

0 回答 0