OpenCV阈值用于伤口检测,还有更好的方法吗?

时间:2019-07-08 22:58:05

标签: java opencv image-processing image-segmentation image-thresholding

我一直在弄弄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

我将感谢所有提示,指出正确的方向!

0 个答案:

没有答案