title: opencv: how to save float array as an image
body: <p>I'm absolutly new to c++ and the opencv library. I was browsing through stackoverflow and also searched the web, but couldn't find an answer to my question. </p>
  
  <p>How can I save a float array to an image in c++?  Currently (as an exercise) I'm just manually converting the image to grayscale (although I read that this can be done with opencv). </p>
  
  <p>Later on I plan to do some filter operations and other pixel operations. That's why I want to use a float-array (of size 512*512). </p>
  
  <p>Here's the code. All I know is, that the float array has to be converted to int or char   or uint8 or cv::Mat, so that it can be saved as a .png, but I didnt't know how to.</p>
  
  <p>Any hints or links are highly appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  
  int main(void)
  {
    // load in image using opencv and convert to char
    cv::Mat myImg = cv::imread("~/Lenna.png", CV_LOAD_IMAGE_COLOR);
    unsigned char *img = (unsigned char*)(myImg.data); //somehow only works for unsigned char and not for float (SegFault)
  
    float *r = new float[512*512]; 
    float *g = new float[512*512]; 
    float *b = new float[512*512]; 
    float *gray = new float[512*512];
  
  
    // 3*iCol, bc every pixel takes 3 bytes (one for R channel/B channel /G channel).
    // see http://answers.opencv.org/question/2531/image-data-processing-in-cvmat/
    // faster to loop through rows first and then through colums (bc MAT stored in row-major order)
    uint iPix = 0;
    for(int iRow=0; iRow&lt;512 ;iRow++){
      for(int iCol=0; iCol&lt;512 ;iCol++){
        b[iPix] = (float) img[myImg.step * iRow + 3*iCol     ];
        g[iPix] = (float) img[myImg.step * iRow + 3*iCol + 1 ];
        r[iPix] = (float) img[myImg.step * iRow + 3*iCol + 2 ];
        gray[iPix] = (float) 0.0722*b[iPix] + 0.2126*r[iPix] + 0.7152*g[iPix];
        iPix++;
      }
    }
  
  
    //write image to file (NOT WORKING!)
    cv::imwrite("~/imgOut.png",  (cv::Mat) gray);
  }
  </code></pre>