如何利用opencv 显示 镜像的应用程序呢?
就是应用程序在干嘛,用opencv的窗口显示。
通过下面的程序来达到这个目的,如果有更好的方式请联系我,谢谢拉!
#include "stdafx.h"
#include <time.h>
#include <vector>
#include <stdio.h>
#include <comdef.h>
#include <iostream>
#include <atlimage.h>
#include <Windows.h>
#include <tchar.h>
using namespace std;
#include <opencv.hpp>
using namespace cv;
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置连接器选项
IplImage* hBitmapToIpl(HBITMAP hBmp)
{
BITMAP bmp;
GetObject(hBmp,sizeof(BITMAP),&bmp);
int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
IplImage* img = cvCreateImage(cvSize(bmp.bmWidth,bmp.bmHeight),depth,nChannels);
BYTE *pBuffer = new BYTE[bmp.bmHeight*bmp.bmWidth*nChannels];
GetBitmapBits(hBmp,bmp.bmHeight*bmp.bmWidth*nChannels,pBuffer);
memcpy(img->imageData,pBuffer,bmp.bmHeight*bmp.bmWidth*nChannels);
delete pBuffer;
IplImage *dst = cvCreateImage(cvGetSize(img),img->depth,3);
cvCvtColor(img,dst,CV_BGRA2BGR);
cvReleaseImage(&img);
return dst;
}
int main(int argc, _TCHAR* argv[])
{
POINT pt;
GetCursorPos(&pt);//得到当前鼠标所点的应用程序
HWND hpWnd=NULL, hcWnd=NULL;
hpWnd = ::WindowFromPoint( pt );
if( IsWindow(hpWnd) )
hcWnd = ::ChildWindowFromPointEx( hpWnd, pt, CWP_ALL );
if( IsWindow( hcWnd ) == FALSE )
hcWnd = hpWnd;
HDC hDc,memDc;
hDc = GetDC(hcWnd);//取得桌面设备 hdc
memDc = CreateCompatibleDC(hDc);//创建与HDC(桌面)兼容的设备memDc
HBITMAP hBitMap;
//HWND hWnd = GetDesktopWindow();//得到桌面句柄
RECT rect;
GetWindowRect(hcWnd,&rect);//桌面的矩形
int width = rect.right-rect.left;//桌面的宽
int hight = rect.bottom-rect.top;//桌面的高
while(waitKey(50)!=27&&IsWindow(hcWnd))//当按下ESC或者应用窗口退出时退出程序
{
hBitMap = CreateCompatibleBitmap(hDc,width,hight);//创建与桌面兼容的画布
SelectObject(memDc,hBitMap);//关联设备和画布
BitBlt(memDc,0,0,width,hight,hDc,0,0,SRCCOPY);//吧桌面往兼容设备里复制
IplImage* src = hBitmapToIpl(hBitMap);
cvNamedWindow("aa");
cvShowImage("aa",src);
cvReleaseImage(&src);
DeleteObject(hBitMap);
}
return 0;
}
程序效果如图
左边为同步后的程序,右边为记事本应用程序