多进程尝试

This commit is contained in:
zjc-zjc-123 2024-11-26 18:17:35 +08:00
parent e93b5fbff7
commit cf1dde8a62
6 changed files with 279 additions and 4 deletions

View File

@ -269,9 +269,9 @@ void TemplateMatcher::FindTemplates( const MIL_ID& inputImage, MIL_ID& outputIma
// 2. 在src里头添加另一个cvdl库专用于视觉深度学习
// 3. 添加一个类OnnxRunner
// TODO: 完善config文件确保能够读取mask转换的相关参数
// TODO: 完善config文件确保能够读取mask转换的相关参数
// TODO: Opencv和matrox图像的转换函数添加到Matrox/utils.cpp
// TODO: Opencv和matrox图像的转换函数添加到Matrox/utils.cpp 有问题
// TODO构建统一的图像检测器类可以一键加载一键开启多进程快速预测

View File

@ -25,4 +25,16 @@ target_link_libraries(test_mask Matrox ${OpenCV_LIBS} ${MIL_LIBS})
add_executable(test_onnx
${CMAKE_CURRENT_SOURCE_DIR}/test_onnx.cpp)
# Matrox
target_link_libraries(test_onnx CVDL ${OpenCV_LIBS} ${MIL_LIBS})
target_link_libraries(test_onnx CVDL ${OpenCV_LIBS} ${MIL_LIBS})
# 4test_onnx
add_executable(test_MILtoMat
${CMAKE_CURRENT_SOURCE_DIR}/test_MILtoMat.cpp)
# Matrox
target_link_libraries(test_MILtoMat CVDL ${OpenCV_LIBS} ${MIL_LIBS})
# 4test_onnx
add_executable(test_total
${CMAKE_CURRENT_SOURCE_DIR}/test_total.cpp)
# Matrox
target_link_libraries(test_total Matrox CVDL ${OpenCV_LIBS} ${MIL_LIBS})

82
tests/test_MILtoMat.cpp Normal file
View File

@ -0,0 +1,82 @@
#include <iostream>
#include <opencv2/opencv.hpp>
#include "Mil.h" // 引入 MIL 库
#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\8.bmp")
#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect.png")
int main() {
MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
MIL_ID MilImage = M_NULL;
// 初始化 MIL 应用程序、系统和显示
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
if (MilApplication == M_NULL || MilSystem == M_NULL || MilDisplay == M_NULL) {
std::cerr << "MIL Initialization failed!" << std::endl;
return -1;
}
// 加载图像
MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
if (MilImage == M_NULL) {
std::cerr << "Failed to load MIL image!" << std::endl;
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return -1;
}
// 获取图像尺寸和通道数
int width, height, channels;
MbufInquire(MilImage, M_SIZE_X, &width); // 获取图像宽度
MbufInquire(MilImage, M_SIZE_Y, &height); // 获取图像高度
MbufInquire(MilImage, M_SIZE_BAND, &channels); // 获取图像通道数
// 为图像数据分配缓冲区
unsigned char* m_AvsBuffer = (unsigned char*)malloc(width * height * channels);
if (m_AvsBuffer == NULL) {
std::cerr << "Memory allocation for image buffer failed!" << std::endl;
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return -1;
}
// 获取 MIL 图像数据
MbufGet(MilImage, m_AvsBuffer);
// 将 MIL 图像数据转换为 OpenCV 格式
cv::Mat cvImage;
if (channels == 1) {
// 灰度图像1 通道)
cvImage = cv::Mat(height, width, CV_8UC1, m_AvsBuffer);
} else if (channels == 3) {
// 彩色图像3 通道BGR
cvImage = cv::Mat(height, width, CV_8UC3, m_AvsBuffer);
} else {
std::cerr << "Unsupported number of channels: " << channels << std::endl;
free(m_AvsBuffer);
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return -1;
}
// 显示图像
MdispSelect(MilDisplay, MilImage); // 在 MIL 显示中显示图像
cv::imshow("MIL Image", cvImage); // 使用 OpenCV 显示图像
// 等待按键并关闭窗口
cv::waitKey(0);
// 保存图像
std::string savepath = "C:\\Users\\zjc\\Desktop\\suspect.png";
if (!cv::imwrite(savepath, cvImage)) {
std::cerr << "Failed to save image!" << std::endl;
}
// 释放资源
free(m_AvsBuffer); // 释放缓冲区
MbufFree(MilImage); // 释放 MIL 图像资源
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); // 释放 MIL 系统资源
return 0;
}

View File

@ -5,7 +5,7 @@
#include "Matrox/utils.h"
#include "Matrox/color_range.h"
#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton_image\\174.bmp")
#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\xixian.png")
#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\diguandai.png")
// Global variables

View File

@ -1,3 +1,70 @@
// #include "vector"
// #include"iostream"
// #include"string"
// #include"Matrox/utils.h"
// #include"opencv2/opencv.hpp"
//
// #define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\8.bmp")
// #define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect.png")
// int main() {
// MIL_ID MilImage = M_NULL;
// MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
//
// MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL,
// M_NULL);
// MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
// cv::Mat opencvImage = milToMat(MilImage);
// imshow("opencv", opencvImage);
// // MosGetch();
//
// return 0;
// }
// int main() {
// MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
// MIL_ID MilImage = M_NULL;
//
// // 初始化 MIL 应用程序、系统和显示
// MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
// if (MilApplication == M_NULL || MilSystem == M_NULL || MilDisplay == M_NULL) {
// std::cerr << "MIL Initialization failed!" << std::endl;
// return -1;
// }
//
// // 加载图像
// MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
// if (MilImage == M_NULL) {
// std::cerr << "Failed to load MIL image!" << std::endl;
// MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL,M_NULL);
// return -1;
// }
//
// // 转换并显示
// cv::Mat opencvImage = milToMat(MilImage);
// if (!opencvImage.empty()) {
// cv::imshow("opencv", opencvImage);
// cv::waitKey(0);
// }
// std:: string savepath="C:\\Users\\zjc\\Desktop\\suspect.png";
// cv::imwrite(savepath,opencvImage);
// // 释放资源
// MbufFree(MilImage);
// MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL,M_NULL); // 使用 MappFreeDefault 代替 MappFree
//
// return 0;
// }
// int main() {
// cv::Mat img = cv::imread("C:\\Users\\zjc\\Desktop\\suspect.png");
// if (img.empty()) {
// std::cout << "图像加载失败!" << std::endl;
// return -1;
// }
//
// // 处理图像
// processImage(img);
// }
#include "CVDL/OnnxRunner.h"
int main() {

114
tests/test_total.cpp Normal file
View File

@ -0,0 +1,114 @@
#include <windows.h>
#include <iostream>
#include <map>
#include <string>
#include "Mil.h"
#include "Matrox/utils.h"
#include "Matrox/color_range.h"
#include "Matrox/template_matching.h"
// 宏定义
#define SAVE_PATH3 MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect.png")
#define SAVE_PATH4 MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect2.png")
#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton_image_new\\357.bmp")
MIL_ID MilApplication, MilSystem, MilDisplay;
std::map<std::string, int> params;
void run_high_sat_detect(MIL_ID MilImage, std::map<std::string, int>& params) {
MIL_ID detection_result = M_NULL;
read_params_from_file("C:\\Users\\zjc\\Desktop\\config\\color_range_config.txt", params);
measure_execution_time([&]() {
high_sat_detect(MilImage, detection_result, params);
});
MbufSave(SAVE_PATH3, detection_result);
MbufFree(detection_result);
}
void run_pre_process(MIL_ID MilImage, std::map<std::string, int>& params) {
MIL_ID detection_result = M_NULL;
MIL_ID detection_resize = M_NULL;
MIL_ID output_Image = M_NULL;
read_params_from_file("C:\\Users\\zjc\\Desktop\\config\\template_color_config.txt", params);
TemplateMatcher matcher(MilSystem, MilDisplay, params);
measure_execution_time([&]() {
pre_process(MilImage, detection_result, params);
MbufAlloc2d(MilSystem, MbufInquire(detection_result, M_SIZE_X, M_NULL) / 2,
MbufInquire(detection_result, M_SIZE_Y, M_NULL) / 2, 1 + M_UNSIGNED,
M_IMAGE + M_PROC, &detection_resize);
MimResize(detection_result, detection_resize, 0.5, 0.5, M_DEFAULT);
matcher.LoadTemplate(params);
matcher.FindTemplates(detection_resize, output_Image, params);
});
MbufSave(SAVE_PATH4, detection_result);
MbufFree(detection_resize);
MbufFree(output_Image);
MbufFree(detection_result);
}
int main() {
// 初始化 MIL 应用
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL) ;
// 读取图片
MIL_ID MilImage = M_NULL;
MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
if (MilImage == M_NULL) {
std::cerr << "Error: Failed to restore image from " << IMAGE_PATH << std::endl;
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return -1;
}
// 加载参数
// 创建两个子进程
PROCESS_INFORMATION processInfo1, processInfo2;
STARTUPINFO startupInfo1, startupInfo2;
ZeroMemory(&startupInfo1, sizeof(startupInfo1));
ZeroMemory(&startupInfo2, sizeof(startupInfo2));
ZeroMemory(&processInfo1, sizeof(processInfo1));
ZeroMemory(&processInfo2, sizeof(processInfo2));
startupInfo1.cb = sizeof(startupInfo1);
startupInfo2.cb = sizeof(startupInfo2);
// 子进程1
if (!CreateProcess(NULL, LPWSTR((LPSTR) "ChildProcess1.exe"), NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo1, &processInfo1)) {
std::cerr << "Error: Failed to create process 1. Error code: " << GetLastError() << std::endl;
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return -1;
}
// 子进程2
if (!CreateProcess(NULL, LPWSTR((LPSTR) "ChildProcess2.exe"), NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo2, &processInfo2)) {
std::cerr << "Error: Failed to create process 2. Error code: " << GetLastError() << std::endl;
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return -1;
}
// 等待两个子进程完成
WaitForSingleObject(processInfo1.hProcess, INFINITE);
WaitForSingleObject(processInfo2.hProcess, INFINITE);
CloseHandle(processInfo1.hProcess);
CloseHandle(processInfo1.hThread);
CloseHandle(processInfo2.hProcess);
CloseHandle(processInfo2.hThread);
// 释放资源
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
std::cout << "所有模块检测已完成!按 <Enter> 退出。" << std::endl;
getchar();
return 0;
}