title: OpenCv Image conversion
body: <p>When trying to do the watershed method i got this error:<code>Unsupported format or combination of formats (Only 32-bit, 1-channel output images are supported) in cvWatershed</code></p>
  
  <p>I think this is because my markers has 3 channels and a depth of 8. I think i need to convert this 3channel 8 depth image to a 32 bit 1 channel image. My question is am i right? and how do i do this conversion?</p>
  
  <p>EDIT: Updated code with solutions</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package kitouch;
  
  import org.OpenNI.*;
  import com.googlecode.javacpp.Loader;
  import com.googlecode.javacv.*;
  import com.googlecode.javacv.cpp.*;
  import static com.googlecode.javacv.cpp.opencv_core.*;
  import static com.googlecode.javacv.cpp.opencv_imgproc.*;
  import static com.googlecode.javacv.cpp.opencv_calib3d.*;
  import static com.googlecode.javacv.cpp.opencv_objdetect.*;
  import static com.googlecode.javacv.cpp.opencv_highgui.*;
  import java.nio.ShortBuffer;
  import java.awt.*;
  import java.awt.image.*;
  
  /**
   *
   * @author olivierjanssens
   */
  public class watherShedExample {
  
  
     // CanvasFrame frame5 = new CanvasFrame("Some T");
      public static void main(String s[])
      {
      CanvasFrame frame1 = new CanvasFrame("Foreground");
      CanvasFrame frame2 = new CanvasFrame("Dilated");
      CanvasFrame frame3 = new CanvasFrame("Background");
      CanvasFrame frame4 = new CanvasFrame("Markers");
      CanvasFrame frame5 = new CanvasFrame("Watershed");
          // Read input image
      IplImage image = cvLoadImage("/Users/olivierjanssens/Downloads/images/group.jpg");
      IplImage test = cvLoadImage("/Users/olivierjanssens/Downloads/images/binary.bmp");
  
      IplImage binary = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
       cvCvtColor(test, binary, CV_BGR2GRAY);
  
      // Eliminate noise and smaller objects, repeat erosion 6 times
      IplImage fg = cvCreateImage(cvGetSize(binary), binary.depth(), binary.nChannels() /* channels */);
      cvErode(binary, fg, null /* 3x3 square */ , 6 /* iterations */);
      frame1.showImage(fg);
  
  
      // Identify image pixels pixels objects
      IplImage bg = cvCreateImage(cvGetSize(binary), binary.depth(), binary.nChannels() /* channels */);
      cvDilate(binary, bg, null /* 3x3 square */ , 6 /* iterations */);
      frame2.showImage(bg);
  
      cvThreshold(bg, bg, 1 /* threshold */ , 128 /* max value */ , CV_THRESH_BINARY_INV);
      frame3.showImage(bg);
  
      // Create marker image
      IplImage markers = cvCreateImage(cvGetSize(binary), IPL_DEPTH_8U, binary.nChannels() /* channels */);
      cvAdd(fg, bg, markers, null);
      frame4.showImage(markers);
  /*
       * TEST SOLUTION 1
      IplImage gray = cvCreateImage(cvGetSize(markers), IPL_DEPTH_8U, 1); 
      cvCvtColor(markers, gray, CV_BGR2GRAY);
  
      IplImage img32bit1chan = cvCreateImage(cvGetSize(gray), IPL_DEPTH_32F, 1);
  
      double ve;
      for (int i = 0; i &lt; gray.width(); i++) // markers width
      {
         for (int j = 0; j &lt; gray.height(); j++) // markers height
         {
             ve = cvGetReal2D((IplImage)gray, j, i); 
             cvSetReal2D((IplImage)img32bit1chan , i, j, ve);
         }
      }
  */
  
      //SOLUTION 2 
      IplImage markers32f = cvCreateImage(cvGetSize(binary), IPL_DEPTH_32F, binary.nChannels());
      cvConvertScale(markers, markers32f, 1, 0); // converts from IPL_DEPTH_8U to IPL_DEPTH_32F
  
  
      cvWatershed(image, markers32f);
  
      frame5.showImage(image);
  
      }
  }
  </code></pre>