OpenCV code use very high CPU at C++ more than Python

Viewed 310

I'm working on python and C++, but I'm still inexperienced. I capture a window image in both languages and convert it to hsv format.The results are as follows:

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

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

task manager

I'm making a mistake somewhere, that's for sure. If I don't convert my results to hsv format I get the almost the same fps and cpu usage but still at C++ more.

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)

I would be glad if you help.

0 Answers
Related