title: Wound Segmentation using Wavelet Transform in OpenCV
body: <p>We tried Local Histogram approach for wound segmentation which didn't work well for all kinds of images and then we taught to use Wavelet transform for wound segmentation. </p>
  
  <p>Which Wavelet transform will be good for wound segmentation and some tips to implement it ??</p>
  
  <p>Is there any better way than the wavelet transform to segment wound in all light conditions ??</p>
  
  <p>We also tried Image Clustering Which didn't went that well.</p>
  
  <p>Here are some test cases and clustering program we used.</p>
  
  <pre><code>#include "cv.h"
  #include "highgui.h"
  
  #include &lt;iostream&gt;
  void show_result(const cv::Mat&amp; labels, const cv::Mat&amp; centers, int height, int width);
  int main(int argc, const char * argv[])
  {    
          cv::Mat image = cv::imread("kmean.jpg");
          if ( image.empty() ) {
                  std::cout &lt;&lt; "unable to load an input image\n";
                  return 1;
          }
          //cv::cvtColor(image,image,CV_BGR2HSV);
          std::cout &lt;&lt; "image: " &lt;&lt; image.rows &lt;&lt; ", " &lt;&lt; image.cols &lt;&lt; std::endl;
          assert(image.type() == CV_8UC3);
          cv::imshow("image", image);
  
          cv::Mat reshaped_image = image.reshape(1, image.cols * image.rows);
          std::cout &lt;&lt; "reshaped image: " &lt;&lt; reshaped_image.rows &lt;&lt; ", " &lt;&lt; reshaped_image.cols &lt;&lt; std::endl;
          assert(reshaped_image.type() == CV_8UC1);
          //check0(image, reshaped_image);
  
          cv::Mat reshaped_image32f;
          reshaped_image.convertTo(reshaped_image32f, CV_32FC1, 1.0 / 255.0);
          std::cout &lt;&lt; "reshaped image 32f: " &lt;&lt; reshaped_image32f.rows &lt;&lt; ", " &lt;&lt; reshaped_image32f.cols &lt;&lt; std::endl;
          assert(reshaped_image32f.type() == CV_32FC1);
  
          cv::Mat labels;
          int cluster_number = 4;
          cv::TermCriteria criteria(cv::TermCriteria::COUNT, 100, 1);
          cv::Mat centers;
          cv::kmeans(reshaped_image32f, cluster_number, labels, criteria, 1, cv::KMEANS_PP_CENTERS, centers);
  
          show_result(labels, centers, image.rows,image.cols);
  
          return 0;
  }
  
  void show_result(const cv::Mat&amp; labels, const cv::Mat&amp; centers, int height, int width)
  {
          std::cout &lt;&lt; "===\n";
          std::cout &lt;&lt; "labels: " &lt;&lt; labels.rows &lt;&lt; " " &lt;&lt; labels.cols &lt;&lt; std::endl;
          std::cout &lt;&lt; "centers: " &lt;&lt; centers.rows &lt;&lt; " " &lt;&lt; centers.cols &lt;&lt; std::endl;
          assert(labels.type() == CV_32SC1);
          assert(centers.type() == CV_32FC1);
  
          cv::Mat rgb_image(height, width, CV_8UC3);
          cv::MatIterator_&lt;cv::Vec3b&gt; rgb_first = rgb_image.begin&lt;cv::Vec3b&gt;();
          cv::MatIterator_&lt;cv::Vec3b&gt; rgb_last = rgb_image.end&lt;cv::Vec3b&gt;();
          cv::MatConstIterator_&lt;int&gt; label_first = labels.begin&lt;int&gt;();
  
          cv::Mat centers_u8;
          centers.convertTo(centers_u8, CV_8UC1, 255.0);
          cv::Mat centers_u8c3 = centers_u8.reshape(3);
  
          while ( rgb_first != rgb_last ) {
                  const cv::Vec3b&amp; rgb = centers_u8c3.ptr&lt;cv::Vec3b&gt;(*label_first)[0];
                  *rgb_first = rgb;
                  ++rgb_first;
                  ++label_first;
          }
          cv::imshow("tmp", rgb_image);
  
  
          cv::waitKey();
  }
  </code></pre>
  
  <p><strong>Would-1 with Background :</strong> (two clusters)</p>
  
  <p><img src="https://i.stack.imgur.com/lo6Jv.png" alt="Would-1 with Background"></p>
  
  <p><strong>Would-1 with out Background :</strong> </p>
  
  <p><img src="https://i.stack.imgur.com/aORUy.png" alt="Would-1 with out Background"></p>
  
  <p><strong>Would-2 with Background :</strong> </p>
  
  <p><img src="https://i.stack.imgur.com/nneKt.png" alt="Would-2 with Background"></p>
  
  <p><strong>Would-2 with out Background :</strong>(three clusters)</p>
  
  <p><img src="https://i.stack.imgur.com/vv3OT.png" alt="Would-2 with out Background"></p>
  
  <p>When we remove background we are getting a bit better segmentation, but for removing background we are using grab-cut which relies on manual operation. So we need a substitute for the kmean-clustering for segmenting image (or) some improvements in above code to achieve 100% success cases.</p>
  
  <p>So is there any better way to segment the wounds ??</p>