opencv读取电脑桌面实时屏幕画面-----------------------opencv学习笔记

.h头文件
#pragma once

#include <Windows.h>

#include

// 记住包含opencv目录
#include<opencv2\opencv.hpp>

// opencv_4.5.1 and contrib
#pragma comment(lib,“opencv_world454.lib”)
#pragma comment(lib,“opencv_world454d.lib”)

namespace _wss
{
/**
* 获取指定窗口句柄的mat
* @Param : h
*/
cv::Mat get_window_bits(HWND h);

/**
* 直接根据窗口进行截图
* @Param : name
* @Param : h
*/
cv::Mat screen_shot_by_window(std::string name);
cv::Mat screen_shot_by_window(HWND h);

}

.C源文件
#include “win32_screen_shots.h”

namespace _wss
{
cv::Mat get_window_bits(HWND h)
{
cv::Mat result;
HBITMAP bit = nullptr;
RECT rect{ 0 };
int width = 0, height = 0;
HDC hdc = nullptr, hdc_compatible = nullptr;
BITMAP info{ 0 };
int channels = 0;

	if (h == nullptr)
		return result;

	do
	{
		if (GetWindowRect(h, &rect) == FALSE)
			break;

		if (rect.left < 0)
			rect.left = 0;

		if (rect.top < 0)
			rect.top = 0;

		width = rect.right - rect.left;
		height = rect.bottom - rect.top;

		hdc = GetWindowDC(h);
		if (hdc == nullptr)
			break;

		hdc_compatible = CreateCompatibleDC(hdc);
		if (hdc_compatible == nullptr)
			break;

		bit = CreateCompatibleBitmap(hdc, width, height);
		if (bit == nullptr)
			break;

		SelectObject(hdc_compatible, bit);

		if (BitBlt(hdc_compatible, 0, 0, width, height, hdc, 0, 0, SRCCOPY) == FALSE)
			break;

		if (GetObjectA(bit, sizeof(BITMAP), &info) == 0)
			break;

		channels = info.bmBitsPixel == 1 ? 1 : info.bmBitsPixel / 8;
		result.create(cv::Size(info.bmWidth, info.bmHeight), CV_MAKETYPE(CV_8U, channels));
		if (GetBitmapBits(bit, info.bmHeight * info.bmWidth * channels, result.data) == 0)
			break;
	} while (false);

	if (hdc)
		ReleaseDC(h, hdc);

	if (hdc_compatible)
		DeleteDC(hdc_compatible);

	if (bit)
		DeleteObject(bit);

	return result;
}

cv::Mat screen_shot_by_window(HWND h)
{
	while (1)
	{
		return get_window_bits(h);
	}
	
}

cv::Mat screen_shot_by_window(std::string name)
{
	while (1)
	{
		return get_window_bits(FindWindowA(0, name.c_str()));
	}
	
}

}

测试文件

while (1)
{
	cv::Mat src = _wss::screen_shot_by_window(GetDesktopWindow());
	cv::waitKey(1);
	imwrite("D:/opencv/tu/小人.png", src);
	Mat dst = imread("D:/opencv/tu/小人.png");
	imshow("result", dst);
}
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
安装方法可以通过以下步骤进行: 1. 首先,你可以尝试从清华大学的镜像站点下载opencv-python和opencv-contrib-python。你可以使用以下链接进行下载:opencv-python https://pypi.tuna.tsinghua.edu.cn/simple/opencv-python/ opencv-python-contrib https://pypi.tuna.tsinghua.edu.cn/simple/opencv-contrib-python/ \[1\] 2. 如果以上方法不可行,你可以访问UCI的网站,找到OpenCV模块,并根据你的Python版本和CPU位数选择相应的安装包进行下载。例如,如果你的Python版本是3.9,CPU是64位,你可以选择下载opencv_python-4.5.5-cp39-cp39-win_amd64.whl文件。下载完成后,将该压缩包剪切到你的Scripts文件夹中。然后,在命令提示符中进入Python安装路径的Scripts文件夹,并使用pip命令进行安装。例如,在Python安装路径的Scripts下输入以下命令:pip3.9.exe install D:\python\Scripts\opencv_python-4.5.5-cp39-cp39-win_amd64.whl \[2\] 3. 安装完成后,你可以使用import cv2和import numpy等语句导入opencv和numpy库,并使用相应的函数进行图像处理。例如,你可以使用cv2.imread函数读取图像,并使用img.shape打印图像的形状。\[3\] #### 引用[.reference_title] - *1* *3* [python 38 安装 opencv-python 4.5.5和opencv-python-contrib 4.5.5.62](https://blog.csdn.net/weixin_42888638/article/details/123289881)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [最新openCV-Python安装教程(python:3.9||opencv-python:4.5.5)](https://blog.csdn.net/weixin_43349916/article/details/123232335)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泰7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值