Hi, I have trouble getting my head around image processing and containers
I basically use threads to capture screen/process images(color/resize)
and use deque containers to stock images !
I basically dequeVar.pushfront and popback when the container exceed 25
and I just basically want to use cvtColor/resize
cvtColor(mat1_buffer.front(), mat1processed_buffer.front(), COLOR_BGRA2BGR);
and it just don’t work idk why !
Could anyone explain what is the best way to exchange resources between threads
and a way to turn off logging would be really great
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/core/utility.hpp>
#include <iostream>
#include <Windows.h>
#include <thread>
#include <deque>
#include <mutex>
#include <condition_variable>
#include <opencv2/core/utils/logger.hpp>
using namespace std;
using namespace cv;
// mat1 = background mat2 = target
Mat ore_mat = imread(R"(C:\Users\Ethernel\Desktop\iron_ore.png)", IMREAD_UNCHANGED);
deque<Mat> mat1_buffer, mat1processed_buffer;
// Global State Vars
mutex mutexVar;
condition_variable condVar;
bool isBuffer_ready = false;
bool isBuffer_main_ready = false;
cv::Mat getMat(HWND hWND){
HDC deviceContext = GetDC(hWND);
HDC memoryDeviceContext = CreateCompatibleDC(deviceContext);
RECT windowRect;
GetClientRect(hWND, &windowRect);
int height = windowRect.bottom;
int width = windowRect.right;
HBITMAP bitmap = CreateCompatibleBitmap(deviceContext, width, height);
SelectObject(memoryDeviceContext, bitmap);
BitBlt(memoryDeviceContext, 0, 0, width, height, deviceContext, 0, 0, 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; //because no compression
bi.biXPelsPerMeter = 1;
bi.biYPelsPerMeter = 2;
bi.biClrUsed = 3;
bi.biClrImportant = 4;
Mat mat = Mat(height, width, CV_8UC4); // 8 bit unsigned ints 4 Channels -> RGBA
//transform data and store into mat.data
GetDIBits(memoryDeviceContext, bitmap, 0, height, mat.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
//clean up!
DeleteObject(bitmap);
DeleteDC(memoryDeviceContext); //delete not release!
ReleaseDC(hWND, deviceContext);
return mat;
}
void getScreen(){
for(;;){
mat1_buffer.push_front(getMat((GetDesktopWindow())));
isBuffer_ready = true;
cvtColor(mat1_buffer.front(), mat1processed_buffer.front(), COLOR_BGRA2BGR);
}
}
void cvtColorMat(){
for(;;){
while(isBuffer_main_ready == true){
cvtColor(mat1_buffer.front(), mat1processed_buffer.front(), COLOR_BGRA2BGR);
}
}
}
void printDebug(){
for(;;){
Sleep(500);
cout << "[MAT1CONTAINER] Size: " << mat1_buffer.size() << endl;
cout << "[MAT1PROCESSEDCONTAINER] Size: " << mat1processed_buffer.size() << endl;
};
}
void clearBuffer(){
for(;;){
while(mat1_buffer.size() > 25){
mat1_buffer.pop_back();
}
}
}
int main(int argc, char** argv) {
thread getScreen_t = thread(getScreen);
Sleep(2000);
isBuffer_main_ready = true;
if(mat1_buffer.size() > 10){
thread printDebug_t = thread(printDebug);
thread clearBuffer_t = thread(clearBuffer);
}
}