title: How do i convert a P2 pgm to a P5 pgm using Opencv?
body: <p>i'm using a chan-vese segmentation code that only works with P5 pgm files, but i need to use it with P2 pgm files as well.
  i am using OpenCV to try to convert it from P2 to P5, the problem i'm having is that i get an error when i try to save the image to binary format (used in P5). here the code:</p>
  
  <pre><code>Mat imag(imread("path_to_file.pgm", CV_LOAD_IMAGE_GRAYSCALE));
  
  vector&lt;int&gt; param;
  param.push_back(CV_IMWRITE_PXM_BINARY);
  param.push_back(1);
  
  try
      {
          imwrite("P5_file.pgm", imag, param);
      }
      catch (const std::exception &amp;erro)
      {
          cout &lt;&lt; &amp;erro;
      }
  </code></pre>
  
  <p>When debugging in Visual Studio, when the function imwrite is used i get an error in xutility internal header, i have no idea where this is from, but it shows me that its an access violation. _Pnext.</p>
  
  <p>if i try to change the flag for reading the image, then i get:</p>
  
  <p>Error: Bad argument (Portable bitmap(.pgm) expects gray image) </p>
  
  <p>what is wrong here?</p>