2015-12-10 140 views
0

我的目標是做一個對象跟蹤。WinRT C++(Win10),opencv HSV色彩空間,圖像顯示,工件

我有一個UWP c#應用程序和鏈接的c + + windows運行時組件。在那裏我可以獲得訪問opencv。

  • 在c#中,MediaCapture返回SoftwareBitmap。
  • 然後我將SoftwareBitmap傳遞給C++並將其轉換爲opencv的Mat。
  • 然後cvtColor(cvFrame, cvFrame, COLOR_BGR2HSV);
  • ,然後再轉換回SoftwareBitmap,與BitmapPixelFormat :: Bgra8和BitmapAlphaMode(僅Bgra8由軟件位圖接受)::忽略

而且有我有什麼screen

如果我跳過cvtColor一部分,只是轉換爲CV ::墊和回SoftwareBitmap,我已經有了一個不錯的圖像結果...

我知道圖片頻道存在問題,但我失去了在哪裏挖。 我認爲cvtColor之後的cv :: Mat是正確的......但是我想看看圖片創建的中間步驟。 (HSV,門檻,然後結果)

任何意見,將不勝感激。

PS 我試過imwrite但沒有創建文件。自/至

Mat Convert(SoftwareBitmap^ from) 
{ 
    BitmapBuffer^ bmpBuffer = from->LockBuffer(BitmapBufferAccessMode::ReadWrite); 

    unsigned char* pPixels = GetPointerToPixelData(bmpBuffer->CreateReference()); 

    Mat mat(from->PixelHeight, from->PixelWidth, CV_8UC4); 
    memcpy(mat.data, pPixels, 4 * from->PixelHeight * from->PixelWidth); 

    return mat; 
} 

SoftwareBitmap^ Convert(Mat from) 
{ 
    SoftwareBitmap^ sBitmap = ref new SoftwareBitmap(BitmapPixelFormat::Bgra8, from.cols, from.rows, BitmapAlphaMode::Ignore); 
    BitmapBuffer^ bmpBuffer = sBitmap->LockBuffer(BitmapBufferAccessMode::ReadWrite); 
    auto reference = bmpBuffer->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<IBufferByteAccess> bufferByteAccess; 
    ComPtr<IInspectable> insp((IInspectable*)buffer); 
    ThrowIfFailed(insp.As(&bufferByteAccess)); 

    unsigned char* pixels = nullptr; 
    ThrowIfFailed(bufferByteAccess->Buffer(&pixels)); 

    return pixels; 
} 

unsigned char* GetPointerToPixelData(IMemoryBufferReference^ reference) 
{ 
    ComPtr<IMemoryBufferByteAccess> bufferByteAccess; 

    ThrowIfFailed(reinterpret_cast<IInspectable*>(reference)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess))); 

    unsigned char* pixels = nullptr; 
    unsigned int capacity = 0; 
    ThrowIfFailed(bufferByteAccess->GetBuffer(&pixels, &capacity)); 

    return pixels; 
} 

回答

0

好吧,我想出瞭解決方案。

//convert from SoftwareBitmap to Mat 
Mat cvFrame = this->Convert(frame); 

//change color space 
cvtColor(cvFrame, cvFrame, COLOR_BGR2HSV); 

// split chanels 
vector<Mat> hsvChannels(3); 
split(cvFrame, hsvChannels); 

//create empty chanel 
Mat empty; 
empty = Mat::zeros(cvFrame.rows, cvFrame.cols, CV_8UC1); 

// create hsv with empty alpha 
vector<Mat> channels; 
channels.push_back(hsvChannels[0]); //h 
channels.push_back(hsvChannels[1]); //s 
channels.push_back(hsvChannels[2]); //v 
channels.push_back(empty); //a 

//put back to Mat 
merge(channels, cvFrame); 

// convert back to SoftwareBitmap 
return this->Convert(cvFrame); 
1

更新 轉換由於你沒有張貼,做自/至SoftwareBitmap/CV實際轉換的代碼::墊。

我最好的猜測是3通道圖像數據顯示好像有4通道。

+0

我想指出 - 儘管它沒有作爲答案發布 - 我的猜測是對的。 – boaz001