功能:读取USB视频,鼠标点击点(x,y)保存在共享内存
发射端
/* 1包含文件 */ //1.1 系统 必选 #include<iostream> #include<Windows.h> //1.2 opencv 可选 #include <opencv2/opencv.hpp> using namespace cv; using namespace std; /* 2自定数据区 */ //2.1 要存储的数据 typedef struct { int x; int y; /*int zx; int zy;*/ int width; int height; int flag; }TrackBox; //目标检测的上下顶点; TrackBox BOX; /*3 共享内存*/ //3.1内存变量 HANDLE hMapFile; LPCTSTR pBuf; TCHAR szName[] = TEXT("Local\FHY_SYSTEM_0"); //指向同一块共享内存的名字 #define FRAME_SIZE 1920*1080 // 要存的数据大小(图像) #define BUF_SIZE FRAME_SIZE*10 //分配10倍大 #define BOX_DATA (char*)pBuf+FRAME_SIZE*0 //数据存储的起始位置 //3.2 初始化 int intShareroom() { hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) BUF_SIZE, // maximum object size (low-order DWORD) szName); // name of mapping object if (hMapFile == NULL) { printf(TEXT("Could not create file mapping object (%d). "), GetLastError()); return 1; } pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, 0, BUF_SIZE); if (pBuf == NULL) { printf(TEXT("Could not map view of file (%d). "), GetLastError()); CloseHandle(hMapFile); return 1; } } /* 4 测试 鼠标点击输出 x y 存入共享内存 */ //4.1 鼠标事件 void onMouse(int event, int x, int y, int flags, void* param) { //cout << "flag =" << flag << endl; Mat *im = reinterpret_cast<Mat*>(param); switch (event) { case CV_EVENT_LBUTTONDOWN: //鼠标左键按下响应:返回坐标和灰度 BOX.x = x; BOX.y = y; BOX.flag = flags; cout << "at(" << BOX.x << "," << BOX.y << ")" << "flag =" << BOX.flag << endl; //cout << "flag =" << BOX.flag << endl; //memcpy(&BOX, BOX_DATA, sizeof(TrackBox)); memcpy(BOX_DATA, &BOX, sizeof(TrackBox)); //memcpy(&flag, TRANSFER_FLAG, sizeof(int)); //memcpy(&point, DECTION_BOX_DATA, sizeof(CvPoint)); break; } } // 4.2 读取视频 int imge_test() { // 构造一个VideoWriter //VideoWriter video("test.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(1920, 1080)); VideoCapture capture(0); if (!capture.isOpened()) { return -1; } Mat frame; capture.set(CV_CAP_PROP_FRAME_WIDTH, 1920); capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1080); bool stop = false; while (1) { //flag = 0; capture >> frame; cvNamedWindow("当前视频", 0); resize(frame, frame, Size(1920, 1080)); //Sleep(10); cvSetMouseCallback("当前视频", onMouse, &frame); imshow("当前视频", frame); waitKey(1); } } int main() { //3 共享内存初始化 intShareroom(); //4 图像鼠标点击测试 imge_test(); //3 共享内存释放 UnmapViewOfFile(pBuf); //释放; CloseHandle(hMapFile); return 0; }
接收端
#include <windows.h> #include<iostream> using namespace std; // 1.1 定义共享内存 #define FRAME_SIZE 1920*1080 // 1单个大小 #define BUF_SIZE FRAME_SIZE*10 // 2设置单个成倍数 #define recBOX_DATA (char*)pBuffer+FRAME_SIZE*0 // 3在共享内存中的存储的开始地址 // 1.2 TCHAR szName[] = TEXT("Local\FHY_SYSTEM_0"); //4指向同一块共享内存的名字 HANDLE hMapFile; //创建句柄 LPCTSTR pBuffer; // 2 要发送的数据格式 typedef struct { int x; int y; int width; int height; int flag; }TrackBox; //目标检测的上下顶点; TrackBox recBOX; // 3初始化 void initShareMemory() { hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) BUF_SIZE, // maximum object size (low-order DWORD) szName); // name of mapping object if (hMapFile == NULL) { cout << "Could not create file mapping object" << GetLastError() << endl; } pBuffer = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, 0, BUF_SIZE); if (pBuffer == NULL) { printf("Could not map view of file (%d). ", GetLastError()); } } void main() { initShareMemory(); while (true) { // 4 主函数中 使用 memcpy(&recBOX, recBOX_DATA, sizeof(TrackBox));// 取共享内存值 int itarget_x, itarget_y, itarget_h, itarget_w; if (recBOX.flag != 0) // 标志位判断数据是否有效 { itarget_x = recBOX.x;// 中心 itarget_y = recBOX.y; itarget_h = recBOX.height; itarget_w = recBOX.width; //qDebug("target_x:%d target_y:%d", recBOX.x, recBOX.y); } else { itarget_x = 1920 / 2;// 中心 itarget_y = 1080 / 2; //qDebug("target_x:%d target_y:%d", itarget_x, itarget_y); } cout << "x " << itarget_x << " y " << itarget_y << " h " << itarget_h << " w " << itarget_w << endl; } }
封装成类
类定义
ShareMemray.h
#pragma once #ifndef ShareMemray #define ShareMemray #include<iostream> #include<Windows.h> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; #define FRAME_SIZE 1920*1080 // 要存的数据大小(图像) #define BUF_SIZE FRAME_SIZE*10 //分配10倍大 #define BOX_DATA (char*)pBuf+FRAME_SIZE*0 //数据存储的起始位置 存BOX_DATA pBuf+FRAME_SIZE*0 - pBuf+FRAME_SIZE*0+sizeof(TrackBox) #define MAT_DATA (char*)pBuf+FRAME_SIZE*1 // 存MAT_DATA pBuf+FRAME_SIZE*1 - pBuf+FRAME_SIZE*1+sizeof(Mat) typedef struct { int x; int y; int width; int height; int flag; }TrackBox; //目标检测的上下顶点; class SHAREDMEMORY { private: TrackBox BOX; HANDLE hMapFile; LPCTSTR pBuf; TCHAR szName[30] = TEXT("Local\FHY_SYSTEM_0"); //指向同一块共享内存的名字 public: //1 初始化 int intShareroom(); void SendBox(TrackBox BOX); void RecBox(TrackBox BOX); void stop(); }; #endif //SHAREDMEMORY_HPP
文件定义
ShareMemray.cpp
#include "ShareMemray.h" //3.2 初始化 int SHAREDMEMORY::intShareroom() { hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file NULL, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) BUF_SIZE, // maximum object size (low-order DWORD) szName); // name of mapping object if (hMapFile == NULL) { printf(TEXT("Could not create file mapping object (%d). "), GetLastError()); return 1; } pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, 0, BUF_SIZE); if (pBuf == NULL) { printf(TEXT("Could not map view of file (%d). "), GetLastError()); CloseHandle(hMapFile); return 1; } } void SHAREDMEMORY::SendBox(TrackBox BOX) { memcpy(BOX_DATA, &BOX, sizeof(TrackBox)); } void SHAREDMEMORY::RecBox(TrackBox BOX) { memcpy(&BOX, BOX_DATA, sizeof(TrackBox)); } void SHAREDMEMORY::stop() { UnmapViewOfFile(pBuf); //释放; CloseHandle(hMapFile); }
发送端
#pragma once #ifndef MAIN #define MAIN /* 1包含文件 */ //1.1 系统 必选 #include<iostream> #include<Windows.h> #include "../Include/ShareMemray.h" //1.2 opencv 可选 #include <opencv2/opencv.hpp> using namespace cv; using namespace std; TrackBox BOX1; /* 4 测试 鼠标点击输出 x y 存入共享内存 */ //4.1 鼠标事件 void onMouse(int event, int x, int y, int flags, void* param) { //cout << "flag =" << flag << endl; Mat *im = reinterpret_cast<Mat*>(param); switch (event) { case CV_EVENT_LBUTTONDOWN: //鼠标左键按下响应:返回坐标和灰度 BOX1.x = x; BOX1.y = y; BOX1.flag = flags; cout << "at(" << BOX1.x << "," << BOX1.y << ")" << "flag =" << BOX1.flag << endl; break; } } // 4.2 读取视频 int imge_test(SHAREDMEMORY sharesend) { // 构造一个VideoWriter //VideoWriter video("test.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(1920, 1080)); VideoCapture capture(0); if (!capture.isOpened()) { return -1; } Mat frame; capture.set(CV_CAP_PROP_FRAME_WIDTH, 1920); capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1080); bool stop = false; while (1) { //flag = 0; capture >> frame; cvNamedWindow("当前视频", 0); resize(frame, frame, Size(1920, 1080)); //Sleep(10); cvSetMouseCallback("当前视频", onMouse, &frame); imshow("当前视频", frame); waitKey(1); if (BOX1.flag == 1) { sharesend.SendBox(BOX1); BOX1.flag = 0; } } } int main() { //共享内存初始化 SHAREDMEMORY sharesend; sharesend.intShareroom(); //共享内存发送信息 imge_test(sharesend); //共享内存发送消息 sharesend.stop(); return 0; } #endif
接收端
#include <windows.h> #include<iostream> #include "../Include/ShareMemray.h" using namespace std; TrackBox recBOX; void main() { SHAREDMEMORY sharerec; sharerec.intShareroom(); while (true) { sharerec.RecBox(recBOX); int itarget_x, itarget_y, itarget_h, itarget_w; if (recBOX.flag != 0) // 标志位判断数据是否有效 { itarget_x = recBOX.x;// 中心 itarget_y = recBOX.y; itarget_h = recBOX.height; itarget_w = recBOX.width; //qDebug("target_x:%d target_y:%d", recBOX.x, recBOX.y); } else { itarget_x = 1920 / 2;// 中心 itarget_y = 1080 / 2; //qDebug("target_x:%d target_y:%d", itarget_x, itarget_y); } cout << "x " << itarget_x << " y " << itarget_y << " h " << itarget_h << " w " << itarget_w << endl; } sharerec.stop(); }
如何发送一个新的自定义结构体
1在共享内存头文件构造这个结构体
typedef struct { int x; int y; int width; int height; int flag; }TrackBox; //目标检测的上下顶点;
2 在发送类里面加入这个结构体声明
3 确定在开辟的贡献内存里,分哪一块用来存
3-1 确认共享内存总的区间,足以存下
总空间名字
总空间大小
3-2 在我我们开辟的共享内存里,分那一块用来存
4 实际存入