利用opencv 同步应用程序窗口

13 篇文章 0 订阅

如何利用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;  
}

程序效果如图

左边为同步后的程序,右边为记事本应用程序


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值