title: OpenCV thresholding for wound detection, any better methods?
body: <p>I have been fiddling around with convex hulls in OpenCV trying to detect an irregularly shaped wound (in order to calculate its size later on using a reference marker). The problem is that the wound's irregularity in color is providing a problem with thresholding. I can't exactly figure out what kind of preprocessing do I need or how to set applicable color ranges.</p>
  
  <p>I tried to apply a Gaussian blur, dilation, and fiddle around with the Red ranges, so far to no avail.</p>
  
  <pre><code>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&lt;MatOfPoint&gt; gcontours = new ArrayList&lt;&gt;();
  Mat rhierarchy = new Mat();
  List&lt;MatOfPoint&gt; rcontours = new ArrayList&lt;&gt;();
  Imgproc.findContours(redMat, rcontours, rhierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
  
  List&lt;MatOfPoint&gt; rhullList = new ArrayList&lt;&gt;();
      for (MatOfPoint contour : rcontours) {
          MatOfInt hull = new MatOfInt();
          Imgproc.convexHull(contour, hull);
          Point[] contourArray = contour.toArray();
          Point[] hullPoints = new Point[hull.rows()];
          List&lt;Integer&gt; hullContourIdxList = hull.toList();
          for (int i = 0; i &lt; 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 &lt; rcontours.size(); contourIdx++) {
          double contourArea = Imgproc.contourArea(rcontours.get(contourIdx));
          if (contourArea &gt; rlargest_area) {
              rlargest_area = contourArea;
              rlargest_contour_index = contourIdx;
          }
      }
  
      double rcurrentMax = 0;
      for (MatOfPoint c: rhullList){
          double area= Imgproc.contourArea(c);
          if(area&gt;rcurrentMax){
              rcurrentMax = area;
          }
      }
  
      Imgproc.drawContours(mat, rhullList, rlargest_contour_index, new Scalar(0, 255, 0, 255), 3);
  }
  </code></pre>
  
  <p>Expected result: <a href="https://i.ibb.co/TrmyDG0/Inkedmotasem2-LI.jpg" rel="nofollow noreferrer">https://i.ibb.co/TrmyDG0/Inkedmotasem2-LI.jpg</a></p>
  
  <p>Actual result: <a href="https://i.ibb.co/ccdkCkj/motasem2-jpg-copy2.png" rel="nofollow noreferrer">https://i.ibb.co/ccdkCkj/motasem2-jpg-copy2.png</a></p>
  
  <p>I would appreciate any tips pointing me towards the right direction!</p>