title: WinRT C++ (Win10), opencv HSV color space, image display, artifacts
body: <p>My goal is to make an object tracking. </p>
  
  <p>I have a UWP c# app and linked c++ windows runtime component. There I can obtain acces to opencv.</p>
  
  <ul>
  <li>In c#, MediaCapture return SoftwareBitmap. </li>
  <li>Then I pass SoftwareBitmap to c++ and convert it to opencv's Mat. </li>
  <li>Then <code>cvtColor(cvFrame, cvFrame, COLOR_BGR2HSV);</code>  </li>
  <li>And then convert back to SoftwareBitmap, with BitmapPixelFormat::Bgra8 (<em>only Bgra8 is accepted by Software Bitmap</em>) and BitmapAlphaMode::Ignore</li>
  </ul>
  
  <p>And there is what I've got <a href="https://onedrive.live.com/redir?resid=357572637B8B8769!150460&amp;authkey=!ACmXOuwLkoOyxFY&amp;v=3&amp;ithint=photo%2CPNG" rel="nofollow">screen</a></p>
  
  <p>If I skip a <code>cvtColor</code> part and just convert to cv::Mat and back to SoftwareBitmap, I've got a nice image in result ...</p>
  
  <p>I understand that there is a problem with image's channels ... but I've lost where to dig.
  I assume that cv::Mat after <code>cvtColor</code> is right ... but I want to see intermediate steps of a picture creation. (HSV, threshold, and then result)</p>
  
  <p>Any advice would be appreciated.</p>
  
  <p>PS
  I've tried <code>imwrite</code> but no file is created.</p>
  
  <p><strong>Update</strong>
  Conversion from/to</p>
  
  <pre><code>Mat Convert(SoftwareBitmap^ from)
  {
      BitmapBuffer^ bmpBuffer = from-&gt;LockBuffer(BitmapBufferAccessMode::ReadWrite);
  
      unsigned char* pPixels = GetPointerToPixelData(bmpBuffer-&gt;CreateReference());
  
      Mat mat(from-&gt;PixelHeight, from-&gt;PixelWidth, CV_8UC4);
      memcpy(mat.data, pPixels, 4 * from-&gt;PixelHeight * from-&gt;PixelWidth);
  
      return mat;
  }
  
  SoftwareBitmap^ Convert(Mat from)
  {
      SoftwareBitmap^ sBitmap = ref new SoftwareBitmap(BitmapPixelFormat::Bgra8, from.cols, from.rows, BitmapAlphaMode::Ignore);
      BitmapBuffer^ bmpBuffer = sBitmap-&gt;LockBuffer(BitmapBufferAccessMode::ReadWrite);
      auto reference = bmpBuffer-&gt;CreateReference();
  
      unsigned char* dstPixels = GetPointerToPixelData(reference);
      memcpy(dstPixels, from.data, from.step.buf[1] * from.cols*from.rows);
  
      return sBitmap;
  }
  
  unsigned char* GetPointerToPixelData(IBuffer^ buffer)
  {
      ComPtr&lt;IBufferByteAccess&gt; bufferByteAccess;
      ComPtr&lt;IInspectable&gt; insp((IInspectable*)buffer);
      ThrowIfFailed(insp.As(&amp;bufferByteAccess));
  
      unsigned char* pixels = nullptr;
      ThrowIfFailed(bufferByteAccess-&gt;Buffer(&amp;pixels));
  
      return pixels;
  }
  
  unsigned char* GetPointerToPixelData(IMemoryBufferReference^ reference)
  {
      ComPtr&lt;IMemoryBufferByteAccess&gt; bufferByteAccess;
  
      ThrowIfFailed(reinterpret_cast&lt;IInspectable*&gt;(reference)-&gt;QueryInterface(IID_PPV_ARGS(&amp;bufferByteAccess)));
  
      unsigned char* pixels = nullptr;
      unsigned int capacity = 0;
      ThrowIfFailed(bufferByteAccess-&gt;GetBuffer(&amp;pixels, &amp;capacity));
  
      return pixels;
  }
  </code></pre>