C++ c++;在窗口上显示图像的opencv

C++ c++;在窗口上显示图像的opencv,c++,windows,opencv,winapi,C++,Windows,Opencv,Winapi,我想使用OpenCV加载图像,然后在窗口上显示它 我知道如何使用opencv加载图像,以及如何使用win32创建窗口,但之后如何将opencv中的图像/mat放在窗口上 以下是我从opencv加载图像的方式: #include <opencv2/core/core.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream

我想使用OpenCV加载图像,然后在窗口上显示它

我知道如何使用opencv加载图像,以及如何使用win32创建窗口,但之后如何将opencv中的图像/mat放在窗口上

以下是我从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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int main(int argc,字符**argv)
{
字符串imageName(“C:/image.jpg”);//默认情况下
如果(argc>1)
{
imageName=argv[1];
}
Mat图像;
image=imread(imageName.c_str(),imread_COLOR);
if(image.empty())
{
好吧

不要通过调用“namedWindow()”来创建新窗口

然后调用
imshow(现有窗口的名称、图像)


也许它会起作用。

我经常在我的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);
    }
}
void DrawImage(CWnd*wnd、int-width、int-height、int-bpp、const unsigned char*buffer)
{
RECT-RECT;
wnd->GetWindowRect(&rect);
CDC*dc=wnd->GetDC();
如果(bpp==3)//BGR
{
BITMAPINFO-bmpinfo;
memset(&bmpinfo,0,sizeof(bmpinfo));
bmpinfo.bmiHeader.biSize=sizeof(BitMapInfo头文件);
bmpinfo.bmiHeader.biBitCount=24;
bmpinfo.bmiHeader.biClrImportant=0;
bmpinfo.bmiHeader.biclrued=0;
bmpinfo.bmiHeader.biCompression=BI_RGB;
bmpinfo.bmiHeader.biWidth=宽度;
bmpinfo.bmiHeader.biHeight=-高度;
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.bottom-rect.top,
0,
0,
宽度,
高度,
缓冲器
&bmpinfo,
DIB_RGB_颜色,
复印件);
}
else if(bpp==1)//单色。
{
字符BITMAPINFO[sizeof(BITMAPINFO)+4*256];
BITMAPINFO*pBmpInfo=(BITMAPINFO*)BITMAPINFO;
memset(pBmpInfo,0,sizeof(BITMAPINFO)+4*256);
pBmpInfo->bmiHeader.biSize=sizeof(BitMapInfo头文件);
pBmpInfo->bmiHeader.biWidth=宽度;
pBmpInfo->bmiHeader.biHeight=-height;
pBmpInfo->bmiHeader.biCompression=BI_RGB;
pBmpInfo->bmiHeader.biPlanes=1;
pBmpInfo->bmiHeader.biBitCount=8;
对于(int i=0;i<256;i++)
{
pBmpInfo->bmiColors[i].rgbBlue=i;
pBmpInfo->bmiColors[i].rgbGreen=i;
pBmpInfo->bmiColors[i].rgbreed=i;
pBmpInfo->bmiColors[i].rgbReserved=255;
}
::SetStretchBltMode(dc->GetSafeHdc(),COLORONCOLOR);
::StretchDIBits(dc->GetSafeHdc(),
0, 
0, 
右,右,左,
rect.bottom-rect.top,
0, 
0, 
宽度,
高度,
缓冲器
pBmpInfo,
DIB_RGB_颜色,
复印件);
}
wnd->ReleaseDC(dc);
}
void DrawCVImage(cv::Mat image,CWnd*图片)
{
如果(image.cols%4==0)
{
绘图图像(图片,
image.cols,
image.rows,
image.channels()==3?3:1,
图像(数据);
}
其他的
{
Mat image2(image.rows,image.cols+(4-image.cols%4),image.type());
图像2=0;
copyTo(image2(Rect(0,0,image.cols,image.rows));
绘图图像(图片,
图2.cols,
图2.2行,
image2.channels()==3?3:1,
图像2.数据);
}
}

imshow有什么问题吗?
?没什么,我只是想知道如何在win32窗口上显示opencv中的图像:)但是……这是一个win32窗口……;)老实说,这是因为我不想在程序运行时创建一个新窗口来打开图像。我实际上想使用win32找到一个已经存在的窗口2的FindWindow函数,然后在此基础上绘制:pCheck谢谢,但我想要绘制的窗口实际上没有名称,我所拥有的只是它的一个HWND:(