实时截屏转为opencv格式处理

实时截屏 转为 openCV 格式处理

(2011-09-21 14:11:08)
标签:

it

分类: OpenCV

// abs_image.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"


//截屏图片参数
int image_width;
int image_height;
int image_depth;
int image_nchannels;

IplImage* screemImage;
int flag=0;

//截屏函数
void CopyScreenToBitmap()
{
int right=GetSystemMetrics(SM_CXSCREEN),left=0,top=0,bottom=GetSystemMetrics(SM_CYSCREEN);//定义截屏范围此处设为全屏

int nWidth, nHeight;

HDC hSrcDC = NULL, hMemDC = NULL;
HBITMAP hBitmap = NULL, hOldBitmap = NULL;

hSrcDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hSrcDC);

nWidth = right - left;
nHeight = bottom - top;

hBitmap = CreateCompatibleBitmap(hSrcDC, nWidth, nHeight);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);

BitBlt(hMemDC, 0, 0, nWidth, nHeight, hSrcDC, left, top , SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);

BITMAP bmp;
int nChannels,depth;
BYTE *pBuffer;

GetObject(hBitmap,sizeof(BITMAP),&bmp);

image_nchannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ;
image_depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
image_width=bmp.bmWidth;
image_height=bmp.bmHeight;

if(flag==0)
{
screemImage=cvCreateImage(cvSize(image_width,image_height),image_depth,image_nchannels);
flag=1;
}

pBuffer = new BYTE[image_width*image_height*image_nchannels];
GetBitmapBits(hBitmap,image_height*image_width*image_nchannels,pBuffer);
memcpy(screemImage->imageData,pBuffer,image_height*image_width*image_nchannels);

delete pBuffer;

SelectObject(hMemDC,hOldBitmap);
DeleteObject(hOldBitmap);
DeleteDC(hMemDC);

SelectObject(hSrcDC,hBitmap);
DeleteDC(hMemDC);
DeleteObject(hBitmap);
}

int _tmain(int argc, _TCHAR* argv[])
{

IplImage* screenRGB=0;
IplImage* screen_resize=0;

while(1)
{

CopyScreenToBitmap(); //得到的图片为RGBA格式,即4通道。

if(!screen_resize)screen_resize=cvCreateImage(cvSize(320,240),image_depth,image_nchannels);
cvResize(screemImage,screen_resize,CV_INTER_LINEAR);

if(!screenRGB)screenRGB=cvCreateImage(cvSize(320,240),IPL_DEPTH_8U,3);
cvCvtColor(screen_resize,screenRGB,CV_RGBA2RGB);

cvShowImage("s_laplace",screenRGB);

cvWaitKey(10);
}
cvDestroyAllWindows();
return 0;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值