title: OpenCV C++ calcHist to Java
body: <p>I'm trying to get some c++ code to run on my Android device; however, I'm running into a small little problem with the type of <code>Mat</code> I'm using.  The code I'm trying to convert is as follow (the second function calls the first):</p>
  
  <pre><code>static Mat
  histc_(const Mat&amp; src, int minVal=0, int maxVal=255, bool normed=false)
  {
      Mat result;
      // Establish the number of bins.
      int histSize = maxVal-minVal+1;
      // Set the ranges.
      float range[] = { static_cast&lt;float&gt;(minVal), static_cast&lt;float&gt;(maxVal+1) };
      const float* histRange = { range };
      // calc histogram
      calcHist(&amp;src, 1, 0, Mat(), result, 1, &amp;histSize, &amp;histRange, true, false);
      // normalize
      if(normed) {
          result /= (int)src.total();
      }
      return result.reshape(1,1);
  }
  
  static Mat histc(InputArray _src, int minVal, int maxVal, bool normed)
  {
      Mat src = _src.getMat();
      switch (src.type()) {
          case CV_8SC1:
              return histc_(Mat_&lt;float&gt;(src), minVal, maxVal, normed);
              break;
          case CV_8UC1:
              return histc_(src, minVal, maxVal, normed);
              break;
          case CV_16SC1:
              return histc_(Mat_&lt;float&gt;(src), minVal, maxVal, normed);
              break;
          case CV_16UC1:
              return histc_(src, minVal, maxVal, normed);
              break;
          case CV_32SC1:
              return histc_(Mat_&lt;float&gt;(src), minVal, maxVal, normed);
              break;
          case CV_32FC1:
              return histc_(src, minVal, maxVal, normed);
              break;
          default:
              CV_Error(Error::StsUnmatchedFormats, "This type is not implemented yet."); break;
      }
      return Mat();
  }
  </code></pre>
  
  <p>Now my java code combined these 2 functions into 1 since my type is always the same: CV_32SC1.</p>
  
  <pre><code>private Mat histc(Mat src, int minVal, int maxVal)
  {
      Mat result = new Mat();
  
      MatOfInt histSize = new MatOfInt(maxVal - minVal + 1);
  
      MatOfFloat histRange = new MatOfFloat(minVal, maxVal + 1);
      MatOfInt channels = new MatOfInt(0);
      Log.d(TAG, "Type: " + CvType.typeToString(src.type()));
      src.convertTo(src, CvType.CV_32S);
      Imgproc.calcHist(Arrays.asList(src), channels, new Mat(), result, histSize, histRange);
  
      return result.reshape(1,1);
  }
  </code></pre>
  
  <p>I'm getting an error <code>OpenCV Error: Unsupported format or combination of formats () in void cv::calcHist</code> and I found from another question that this is because the type of my <code>src</code> matrix is <code>CV_32SC1</code>.  So my problem is that I don't know how to convert these lines from the second c++ function into Java properly:</p>
  
  <pre><code>case CV_32SC1:
          return histc_(Mat_&lt;float&gt;(src), minVal, maxVal, normed);
          break;
  </code></pre>
  
  <p>I'm trying to figure out how to do something similar to <code>Mat_&lt;float&gt;(src)</code> in Java specifically.</p>
  
  <p>For reference: <a href="https://github.com/Itseez/opencv_contrib/blob/master/modules/face/src/lbph_faces.cpp" rel="nofollow">here</a> is the link to the entire code for what I'm trying to do right now</p>