2017-02-13 122 views
0

我想使用OpenCV加載圖像,然後將其顯示在窗口上。C++ opencv在窗口上顯示圖像

我知道如何使用opencv加載圖像以及如何使用win32創建窗口,但是如何才能將Opencv中的圖像/墊從窗口中放到窗口中?

這是我從OpenCV中加載圖像:

#include <opencv2/core/core.hpp> 
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/highgui/highgui.hpp> 

#include <iostream> 
#include <string> 
using namespace cv; 

using namespace std; 

int main(int argc, char** argv) 
{ 

    string imageName("C:/image.jpg"); // by default 
    if (argc > 1) 
    { 
     imageName = argv[1]; 
    } 

    Mat image; 



    image = imread(imageName.c_str(), IMREAD_COLOR); 


    if (image.empty())  
    { 
     cout << "Could not open or find the image" << std::endl; 
     return -1; 
    } 


    namedWindow("Display window", WINDOW_AUTOSIZE); 

    imshow("Display window", image); 


    waitKey(0); 
    return 0; 
} 

編輯:我之所以要做到這一點其實並不在運行時創建一個窗口,然後在其上顯示圖像,而是我要使用Win32的FindWindow函數找到一個窗口,然後畫上的圖像:d

+0

'imshow'有什麼問題? – Miki

+0

沒什麼,我只想知道如何在win32窗口中顯示來自opencv的圖像:) – stav

+1

但是...這是一個win32窗口..;) – Miki

回答

0

嗯......

請勿通過調用創建一個新窗口「namedWindow()」。請致電imshow(nameOfExistingWindow, image)

也許它會工作。

+0

謝謝,但我想繪製的窗口實際上沒有名稱,我擁有的是HWND :( – stav

0

我用這很經常與我的MFC項目。如果你只有hwnd,而不是CWnd,那麼你可能需要改變一下。

這適用於8位RGB顏色和1通道單色圖像。

void DrawImage(CWnd *wnd, int width, int height, int bpp, const unsigned char *buffer) 
{ 
    RECT rect; 
    wnd->GetWindowRect(&rect); 
    CDC *dc = wnd->GetDC(); 

    if(bpp == 3) // BGR 
    { 
     BITMAPINFO bmpinfo; 

     memset(&bmpinfo, 0, sizeof(bmpinfo)); 
     bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
     bmpinfo.bmiHeader.biBitCount = 24; 
     bmpinfo.bmiHeader.biClrImportant = 0; 
     bmpinfo.bmiHeader.biClrUsed = 0; 
     bmpinfo.bmiHeader.biCompression = BI_RGB; 
     bmpinfo.bmiHeader.biWidth = width; 
     bmpinfo.bmiHeader.biHeight = -height; 
     bmpinfo.bmiHeader.biPlanes = 1; 
     bmpinfo.bmiHeader.biSizeImage = 0; 
     bmpinfo.bmiHeader.biXPelsPerMeter = 100; 
     bmpinfo.bmiHeader.biYPelsPerMeter = 100; 

     ::SetStretchBltMode(dc->GetSafeHdc(), COLORONCOLOR); 
     ::StretchDIBits( dc->GetSafeHdc(), 
         0, 
         0, 
         rect.right - rect.left, 
         rect.bottom - rect.top, 
         0, 
         0, 
         width, 
         height, 
         buffer, 
         &bmpinfo, 
         DIB_RGB_COLORS, 
         SRCCOPY); 
    } 
    else if (bpp == 1) // monochrome. 
    { 
     char bitmapInfoBuf[sizeof(BITMAPINFO) + 4 * 256]; 
     BITMAPINFO* pBmpInfo = (BITMAPINFO*)bitmapInfoBuf; 

     memset(pBmpInfo, 0, sizeof(BITMAPINFO) + 4 * 256); 
     pBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
     pBmpInfo->bmiHeader.biWidth = width; 
     pBmpInfo->bmiHeader.biHeight = -height; 
     pBmpInfo->bmiHeader.biCompression = BI_RGB; 
     pBmpInfo->bmiHeader.biPlanes = 1; 
     pBmpInfo->bmiHeader.biBitCount = 8; 

     for(int i = 0; i < 256; i++) 
     { 
      pBmpInfo->bmiColors[i].rgbBlue=i; 
      pBmpInfo->bmiColors[i].rgbGreen=i; 
      pBmpInfo->bmiColors[i].rgbRed=i; 
      pBmpInfo->bmiColors[i].rgbReserved=255; 
     } 

     ::SetStretchBltMode(dc->GetSafeHdc(), COLORONCOLOR); 
     ::StretchDIBits(dc->GetSafeHdc(), 
         0, 
         0, 
         rect.right - rect.left, 
         rect.bottom - rect.top, 
         0, 
         0, 
         width, 
         height, 
         buffer, 
         pBmpInfo, 
         DIB_RGB_COLORS, 
         SRCCOPY); 
    } 

    wnd->ReleaseDC(dc); 
} 

void DrawCVImage(cv::Mat image, CWnd *picture) 
{ 
    if (image.cols % 4 == 0) 
    { 
     DrawImage(picture, 
      image.cols, 
      image.rows, 
      image.channels() == 3 ? 3 : 1, 
      image.data); 
    } 
    else 
    { 
     Mat image2(image.rows, image.cols + (4 - image.cols % 4), image.type()); 
     image2 = 0; 
     image.copyTo(image2(Rect(0, 0, image.cols, image.rows))); 

     DrawImage(picture, 
      image2.cols, 
      image2.rows, 
      image2.channels() == 3 ? 3 : 1, 
      image2.data); 
    } 
}