From cf1dde8a62919c79797c9bd1e6e23371e41104b4 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Tue, 26 Nov 2024 18:17:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E8=BF=9B=E7=A8=8B=E5=B0=9D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Matrox/template_matching.cpp | 4 +- tests/CMakeLists.txt | 14 +++- tests/test_MILtoMat.cpp | 82 ++++++++++++++++++++++ tests/test_color_range.cpp | 2 +- tests/test_onnx.cpp | 67 ++++++++++++++++++ tests/test_total.cpp | 114 +++++++++++++++++++++++++++++++ 6 files changed, 279 insertions(+), 4 deletions(-) create mode 100644 tests/test_MILtoMat.cpp create mode 100644 tests/test_total.cpp diff --git a/src/Matrox/template_matching.cpp b/src/Matrox/template_matching.cpp index 57a3c38..b279084 100644 --- a/src/Matrox/template_matching.cpp +++ b/src/Matrox/template_matching.cpp @@ -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:构建统一的图像检测器类,可以一键加载,一键开启多进程快速预测 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 989d4f6..b7361ed 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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}) \ No newline at end of file +target_link_libraries(test_onnx CVDL ${OpenCV_LIBS} ${MIL_LIBS}) + +#测试用例 4:test_onnx +add_executable(test_MILtoMat + ${CMAKE_CURRENT_SOURCE_DIR}/test_MILtoMat.cpp) +# 链接 Matrox 模块和依赖库 +target_link_libraries(test_MILtoMat CVDL ${OpenCV_LIBS} ${MIL_LIBS}) + +#测试用例 4:test_onnx +add_executable(test_total + ${CMAKE_CURRENT_SOURCE_DIR}/test_total.cpp) +# 链接 Matrox 模块和依赖库 +target_link_libraries(test_total Matrox CVDL ${OpenCV_LIBS} ${MIL_LIBS}) \ No newline at end of file diff --git a/tests/test_MILtoMat.cpp b/tests/test_MILtoMat.cpp new file mode 100644 index 0000000..b5a532c --- /dev/null +++ b/tests/test_MILtoMat.cpp @@ -0,0 +1,82 @@ +#include +#include +#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; +} diff --git a/tests/test_color_range.cpp b/tests/test_color_range.cpp index c57a590..d24d087 100644 --- a/tests/test_color_range.cpp +++ b/tests/test_color_range.cpp @@ -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 diff --git a/tests/test_onnx.cpp b/tests/test_onnx.cpp index 0d95653..9654906 100644 --- a/tests/test_onnx.cpp +++ b/tests/test_onnx.cpp @@ -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() { diff --git a/tests/test_total.cpp b/tests/test_total.cpp new file mode 100644 index 0000000..ff83f03 --- /dev/null +++ b/tests/test_total.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#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 params; + +void run_high_sat_detect(MIL_ID MilImage, std::map& 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& 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 << "所有模块检测已完成!按 退出。" << std::endl; + getchar(); + + return 0; +}