How to do contour (zone) tracking Python/C++

31 views Asked by At

How to make a code that will monitor a contour (zone) and if something changes in this contour, it will click on the coordinates? (Python or C++). I need the speed to be a maximum of 5 milliseconds. Here are examples. ZONE --> enter image description here enter image description here

#include <iostream>
#include <opencv2/opencv.hpp>
#include <Windows.h>
#include "mouse.h"

bool EXIT;
using namespace mouse;
using namespace std;


void click(int x = NULL, int y = NULL) {
    Left_Click(true, x, y);
}

namespace Time
{
    long getMillis() {
        return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
    }
}

#include <iostream>
#include <Windows.h>

int getColor(int x, int y) {
    HDC hdc = GetDC(NULL);
    COLORREF color = GetPixel(hdc, x, y);
    ReleaseDC(NULL, hdc);
    return color;
}


double getContoursCount(int x, int y, int x1, int y1) {
    // Вычисление размера из двух координат
    int width = x1 - x;
    int height = y1 - y;
    auto pro_img = [](cv::Mat img_stan) -> cv::Mat {
    cv::cvtColor(img_stan, img_stan, cv::COLOR_BGRA2GRAY);
    cv::Canny(img_stan, img_stan, 330, 390);
    return img_stan;};
    HDC hScreenDC = GetDC(0);
    HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
    HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
    HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
    BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, x, y, SRCCOPY);
    BITMAPINFOHEADER bi;
    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;
    cv::Mat img(width, height, CV_8UC4);
    GetDIBits(hMemoryDC, hBitmap, 0, height, img.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
    SelectObject(hMemoryDC, hOldBitmap);
    DeleteObject(hBitmap);
    DeleteDC(hMemoryDC);
    ReleaseDC(0, hScreenDC);
    cv::Mat processed_img = pro_img(img);
    double mean = cv::mean(processed_img)[0];
    return mean;
}


POINT s1 = { 948,314 };
POINT s2 = { s1.x + 10, s1.y + 10 };
POINT buy = { 1232,314 };
POINT confirm = { 765,553 };
POINT es = { 384,803 };
POINT up = { 630,263 };


short slot_count = 6;
short slot_h = 77;
int esc = 0;
int buyc = 15919329;

void start() {
    POINT s3, s4;
    s3.x = s1.x; s4.x = s2.x;


    long H = Time::getMillis();
    while (!EXIT)
    {

        for (short i=0;i<slot_count*3;i++)
        {
            s3.y = s1.y + (slot_h/3 * i);
            s4.y = s2.y + (slot_h/3 * i);

            if (getContoursCount(s3.x, s3.y, s4.x, s4.y) != 0 && getColor(buy.x, buy.y + (slot_h/3 * i)) > 2500000)
            {
                click(buy.x, buy.y + (slot_h/3 * i)); 
                click(confirm.x, confirm.y); click(confirm.x, confirm.y); click(confirm.x, confirm.y);
                Sleep(100); click(es.x, es.y); Sleep(150);
                click(up.x, up.y);  Sleep(30);
                click(up.x, up.y); click(es.x, es.y);
                Sleep(250);
                H = Time::getMillis();
                EXIT = true;
                break;
            }
        }
        if (Time::getMillis() - H >= 5000) { click(up.x, up.y); Sleep(15); click(up.x, up.y); Sleep(50); H = Time::getMillis();}


    }


}

Here is my code that I made but it is slow.

0

There are 0 answers