title: Connect broken lines of detected wrinkles in an image using Java opencv
body: <p>I am working on a program to detect wrinkles in an image taken from a high resolution camera.
  Currently the project is in its starting phase. I have performed the following steps until now: </p>
  
  <ol>
  <li>Convert to grayscale and contrast the image. </li>
  <li>Remove noise using Gaussian Blur.</li>
  <li>Apply Adaptive threshold to detect wrinkles.</li>
  <li>Use dilation to enhance the size of the detected wrinkle and join disparate elements of a single wrinkle as much as possible. </li>
  <li>Remove noise by finding the contours and removing the ones with lesser areas.  </li>
  </ol>
  
  <p>Here is the code of the same : </p>
  
  <pre><code>package Wrinkle.Detection;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.opencv.core.Core;
  import org.opencv.core.CvType;
  import org.opencv.core.Mat;
  import org.opencv.core.MatOfPoint;
  import org.opencv.core.Scalar;
  import org.opencv.core.Size;
  import org.opencv.highgui.Highgui;
  import org.opencv.imgproc.Imgproc;
  
  public class DetectWrinkle {
  
      private Mat sourceImage;
      private Mat destinationImage;
      private Mat thresh;
  
      public void detectUsingThresh(String filename) {
          sourceImage =  Highgui.imread(filename, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
  
          //Contrast
          Mat contrast = new Mat(sourceImage.rows(), sourceImage.cols(), sourceImage.type());
          Imgproc.equalizeHist(sourceImage, contrast);
          Highgui.imwrite("wrinkle_contrast.jpg", contrast);
  
          //Remove Noise
          destinationImage = new Mat(contrast.rows(), contrast.cols(), contrast.type());
          Imgproc.GaussianBlur(contrast, destinationImage,new Size(31,31), 0);
          Highgui.imwrite("wrinkle_Blur.jpg", destinationImage);
  
          //Apply Adaptive threshold
          thresh = new Mat();
          Imgproc.adaptiveThreshold(destinationImage, thresh, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 99, 10);
          Highgui.imwrite("wrinkle_threshold.jpg", thresh);
  
          // dilation 
          Mat element1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new  Size(2*3+1, 2*6+1));
          Imgproc.dilate(thresh, thresh, element1);
          Highgui.imwrite("wrinkle_thresh_dilation.jpg", thresh);
  
          //Find contours
          List&lt;MatOfPoint&gt; contours = new ArrayList&lt;MatOfPoint&gt;(); 
          Mat image32S = new Mat();
          Mat threshClone = thresh.clone();
          threshClone.convertTo(image32S, CvType.CV_32SC1);
          Imgproc.findContours(image32S, contours, new Mat(), Imgproc.RETR_FLOODFILL,Imgproc.CHAIN_APPROX_SIMPLE);
  
          //Find contours with smaller area and color them to black (removing furhter noise)
          Imgproc.cvtColor(thresh, thresh, Imgproc.COLOR_GRAY2BGR); 
          for (int c=0; c&lt;contours.size(); c++) {
              double value = Imgproc.contourArea(contours.get(c));
              if(value&lt;500){
                  Imgproc.drawContours(thresh, contours, c, new Scalar(0, 0, 0), -1); 
              }
          }
          Highgui.imwrite("wrinkle_contour_fill.jpg", thresh);
  
      }
  
      public static void main(String[] args) {
          DetectWrinkle dw = new DetectWrinkle();
          System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
          String imagefile = "wrinkle_guo (1).bmp";
          dw.detectUsingThresh(imagefile);
      }
  }
  </code></pre>
  
  <p><strong>Question:</strong> 
  As you can see from the result shown below in the images, a single wrinkle on the skin is getting broken down into separate small elements. Here, I am trying to connect those elements to show the complete wrinkle by using dilate. Once it is done, I am removing the noise by first detecting the contours, calculating the area of the contours and then removing the contours with area less than a particular value.</p>
  
  <p>However, this is not giving me a proper result so I feel that there could be some better way of joining the broken wrinkle elements. Please help me solve this. </p>
  
  <p>And please pardon me if there is anything wrong with the question as I really need a solution and I am a newbie here. </p>
  
  <p>Following are the images : </p>
  
  <p><a href="https://i.stack.imgur.com/QodmU.jpg" rel="nofollow">Input image</a>
  <a href="https://i.stack.imgur.com/ZVrm3.jpg" rel="nofollow">After getting contours and removing noise by finding contour area</a></p>