OpenCV代码使用C++的高CPU比Python高

2024-09-30 18:33:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究python和C++,但我仍然缺乏经验。我用两种语言捕获了一个窗口图像,并将其转换为hsv format。结果如下:

Python: %5-9 CPU / 67-72 FPS

C++: %45-50 CPU / 28-35 FPS

task manager

我肯定在某个地方出错了。如果我不将结果转换为hsv format,我会得到几乎相同的fpscpu用法,但仍然是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, width, CV_8UC4);

    hbwindow = CreateCompatibleBitmap(hwindowDC, width, 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, 0, 0, width, height, hwindowDC, 0, 0, imgwidth, imgheight,     SRCCOPY); //change imgCOPY to NOTimgCOPY for wacky colors !
    GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, 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, w, 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)

如果你能帮忙,我会很高兴的


Tags: rectimgwindowwidthcvheightbiwin32gui