title: opencv java modify pixel values
body: <p>I've been trying to convert some opencv C++ code in opencv java and I can't seem to get pixel division to work properly. I take a meanshiftsegmented mat that I convert to grayscale then to 32F.</p>
  
  <p>I then compare the most downsampled then upsampled image (which is comprised of the gray meanshift mat) to the original gray meanshift mat.</p>
  
  <p>I've already read <a href="https://stackoverflow.com/questions/17035005/using-get-and-put-to-access-pixel-values-in-opencv-for-java">Using get() and put() to access pixel values in OpenCV for Java</a></p>
  
  <p>however, it and others like it do not work. The error message I am getting is invalid mat type 5. However, even if I were able to see the saliency map I am positive it is wrong. This is because when I pass in image 001.jpg in c++ I am supposed to see the original image + red square around the objects. In java, I am only seeing the original image at the end.</p>
  
  <p>NOTE : </p>
  
  <p>AbstractImageProvider.deepCopy(AbstractImageProvider.matToBufferedImage(Saliency),disp);</p>
  
  <p>Is an API call that works when I attempt to show the original mat, meanShift mat, and the gray meanShift mat. It fails at showing saliency.</p>
  
  <p>c++</p>
  
  <p>I only did a channel split because I was testing out other colorspaces, however in java I only want to use grayscale.</p>
  
  <pre><code>  input = MeanShift.clone();
    input.convertTo(input, CV_32F);
  
    for(int i = 0; i &lt; Pyramid_Size; i++){DS_Pyramid[i] = input.clone();}
  
    for (int i = 0; i &lt; Pyramid_Size; i++){ 
      for (int k = 0; k &lt;= i; k++){ // Why don't I just downsamplex3 a copy of MeanShift.clone then upsamplex3 that same one? ...
        pyrDown (DS_Pyramid[i], DS_Pyramid[i], Size(DS_Pyramid[i].cols/2, DS_Pyramid[i].rows/2));
        US_Pyramid[i] = DS_Pyramid[i].clone();
      }
      for (int j = 0; j &lt;= i; j++){
        pyrUp (US_Pyramid[i], US_Pyramid[i], Size(US_Pyramid[i].cols*2, US_Pyramid[i].rows*2));
      }
    }
  
    top = US_Pyramid[Pyramid_Size - 1].clone(); // most down sampled layer, up sampled.
    split(top, top_chs);
    split(input.clone(), meanShift_chs); // split into channels result
    split(input.clone(), sal_chs); // holder to use for compare
  
    float top_min = 1.0;
    float ms_min = 1.0;
    for (int i = 0; i &lt; top.rows; i++){   // find the smallest value in both top and meanShift
      for (int k = 0; k &lt; top.cols; k++){ // this is so you can sub out the 0 with the min value
        for (int j = 0; j &lt; top.channels(); j++){ // later on
      float a = top_chs[j].at&lt;float&gt;(i,k);
      float b = meanShift_chs[j].at&lt;float&gt;(i,k);
      if (a &lt; top_min &amp;&amp; a &gt;= 0) {top_min = a;} // make sure you don't have a top_min of zero... that'd be bad.
      if (b &lt; ms_min &amp;&amp; b &gt;= 0)  { ms_min = b;}
        }
      }
    }
  
    for (int i = 0; i &lt; top.rows; i++){
      for (int k = 0; k &lt; top.cols; k++){
        for (int j = 0; j &lt; top.channels(); j++){
      float a,b,c;
      a = top_chs[j].at&lt;float&gt;(i,k);
      b = meanShift_chs[j].at&lt;float&gt;(i,k);
  
      if (a &lt;= 0){a = top_min;} // make sure you don't divide by zero
      if (b &lt;= 0){b = ms_min;} // make sure you really don't divide by zero
      if (a &lt;= b){c = 1.0 - a/b;}
      else {c = 1.0 - b/a;}
  
      // c = sqrt(c); // makes stuff more salient, but makes noise pop out too
      sal_chs[j].at&lt;float&gt;(i,k) = c;
        }
      }
    }
    merge(sal_chs, Saliency); // combine into saliency map
    imshow("saliency", Saliency);
  </code></pre>
  
  <p>java</p>
  
  <pre><code>    MeanShift = inputImage.clone();
  
      Imgproc.pyrMeanShiftFiltering(MeanShift, MeanShift, MeanShift_spatialRad, MeanShift_colorRad);
      Imgproc.cvtColor(MeanShift, MeanShift, Imgproc.COLOR_BGR2GRAY);
  
      MeanShift.convertTo(MeanShift, CvType.CV_32F);                 // 32F between 0 - 1. ************** IMPORTANT LINE
  
      for (int i = 0; i &lt; PyrSize; i++){
          DS_Pyramid.add(new Mat());
          UP_Pyramid.add(new Mat());
      }
  
      for (int i = 0; i &lt; PyrSize; i++){    
          DS_Pyramid.set(i, MeanShift);
      }       
  
      for (int i = 0; i &lt; PyrSize; i++){
          for(int k = 0; k &lt;= i; k++){                               // At 0 is downsampled once, second twice, third 3 times. 
          Imgproc.pyrDown(DS_Pyramid.get(i), DS_Pyramid.get(i)); // pyrDown by default img.width / 2 img height / 2
          Mat a = new Mat();                                     // save the sampled down at i
          a = DS_Pyramid.get(i);                            
          UP_Pyramid.add(a);
          }
          for (int j = 0; j &lt;= i; j++){
          Imgproc.pyrUp(UP_Pyramid.get(i),UP_Pyramid.get(i));    
          }                                                          
      }
      top = UP_Pyramid.get(PyrSize-1);
      bot = MeanShift.clone();
      Saliency = MeanShift.clone();
  
  
      //http://answers.opencv.org/question/5/how-to-get-and-modify-the-pixel-of-mat-in-java/
      //http://www.tutorialspoint.com/java_dip/applying_weighted_average_filter.htm
  
      for (int i = 0; i &lt; top.rows(); i++){
          for (int j = 0; j &lt; top.cols(); j++){
          int index = i * top.rows() + j;
  
          float[] top_temp = top.get(i, j);
          float[] bot_temp  = bot.get(i,j);
          float[] sal_temp = bot.get(i,j);
  
          if (top_temp[0] &lt;= bot_temp[k]){sal_temp[0] = 1.0f - (top_temp[0]/bot_temp[0]);}
          else                           {sal_temp[0] = 1.0f - (bot_temp[0]/top_temp[0]);}
  
          Saliency.put(i,j, sal_temp);
          }
      }
  
      AbstractImageProvider.deepCopy(AbstractImageProvider.matToBufferedImage(Saliency),disp);
  </code></pre>