问题描述
我正在研究 python 和 C++
,但我仍然缺乏经验。我捕获了两种语言的窗口图像并将其转换为hsv format
。结果如下:
Python: %5-9 cpu / 67-72 FPS
C++: %45-50 cpu / 28-35 FPS
我在某个地方犯了一个错误,这是肯定的。如果我不将结果转换为 hsv format
,我会得到几乎相同的 fps
和 cpu
用法,但仍然多 C++
。
C++
:
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <Windows.h>
using namespace std;
using namespace cv;
Mat hwnd2mat(HWND hwnd) {
HDC hwindowDC,hwindowCompatibleDC;
int height,width,imgheight,imgwidth;
HBITMAP hbwindow;
Mat img;
BITMAPINFOHEADER bi;
hwindowDC = GetDC(hwnd);
hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
SetStretchBltMode(hwindowCompatibleDC,COLORONCOLOR);
RECT windowsize;
GetClientRect(hwnd,&windowsize);
imgheight = windowsize.bottom;
imgwidth = windowsize.right;
height = windowsize.bottom / 1;
width = windowsize.right / 1;
img.create(height,CV_8UC4);
hbwindow = CreateCompatibleBitmap(hwindowDC,height);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
SelectObject(hwindowCompatibleDC,hbwindow);
StretchBlt(hwindowCompatibleDC,height,hwindowDC,imgwidth,SRCcopY); //change imgcopY to NOTimgcopY for wacky colors !
GetDIBits(hwindowCompatibleDC,hbwindow,img.data,(BITMAPINFO*)&bi,DIB_RGB_COLORS); //copy from hwindowCompatibleDC to hbwindow
DeleteObject(hbwindow);
DeleteDC(hwindowCompatibleDC);
ReleaseDC(hwnd,hwindowDC);
return img;
}
int main() {
Mat imgBGR,imgHSV;
HWND hwnd = FindWindowA(NULL,"Dragon Age: Origins");
while (1) {
Mat img = hwnd2mat(hwnd);
cvtColor(img,imgHSV,COLOR_BGR2HSV);
imshow("Capture Window C++",imgHSV);
waitKey(1);
}
}
Python
:
import win32gui,win32ui,win32con
import cv2 as cv
import numpy as np
def captureWindow(window_name):
hwnd = win32gui.FindWindow(None,window_name)
window_rect = win32gui.GetwindowRect(hwnd)
w = window_rect[2] - window_rect[0]
h = window_rect[3] - window_rect[1]
border_pixels = 3
titlebar_pixels = 28
w = w - (border_pixels * 2)
h = h - titlebar_pixels - border_pixels
cropped_x = border_pixels
cropped_y = titlebar_pixels
wDC = win32gui.GetwindowDC(hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj,w,h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0),(w,h),dcObj,(cropped_x,cropped_y),win32con.SRCcopY)
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray,dtype='uint8')
img.shape = (h,4)
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(hwnd,wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
return img
while (1):
img = captureWindow("Dragon Age: Origins")
imgBGR = cv.cvtColor(img,cv.COLOR_BGRA2BGR)
imgHSV = cv.cvtColor(imgBGR,cv.COLOR_BGR2HSV)
cv.imshow("Capture Window Python",imgHSV)
cv.waitKey(1)
如果你能帮忙我会很高兴。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)