diff --git a/CMakeLists.txt b/CMakeLists.txt index 389c023..78cd882 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(cotton_color) set(CMAKE_CXX_STANDARD 17) add_definitions(-DUNICODE -D_UNICODE) +add_compile_options(/utf-8) # 设置 OpenCV 路径 if(DEFINED ENV{OpenCV_DIR}) @@ -42,40 +43,8 @@ file(GLOB MIL_LIBS E:/QTexamble/matrox/LIB/*.lib) add_subdirectory(src) add_subdirectory(tests) -## 添加可执行文件 cotton_color -#add_executable(cotton_color cotton_color.cpp) -## 链接 OpenCV 和 Qt 库 -#target_link_libraries(cotton_color Qt6::Widgets ${OpenCV_LIBS} comdlg32) -# -## 添加可执行文件 cotton_color -#add_executable(cotton_range src/Matrox/color_range.cpp -# src/Matrox/color_range.h src/Matrox/utils.h src/Matrox/utils.cpp) -## 链接 OpenCV 和 Qt 库 -#target_link_libraries(cotton_range Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) -# -## 添加可执行文件 cotton_color2 -#add_executable(cotton_color2 cotton_color2.cpp) -## 链接 OpenCV 和 Qt 库 -#target_link_libraries(cotton_color2 Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) -# -#add_executable(template_matching src/Matrox/template_matching.cpp -# src/Matrox/color_range.cpp src/Matrox/color_range.h -# src/Matrox/utils.cpp src/Matrox/utils.h) -#target_link_libraries(template_matching Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) -# -# -#add_executable(ui src/Matrox/ui.cpp) -#target_link_libraries(ui Qt6::Widgets) -# -# -#add_executable(onnx src/Matrox/onnx_running.cpp) -#target_link_libraries(onnx Qt6::Widgets ${MIL_LIBS}) -# + add_executable(opencv_onnx opencv_onnx.cpp) # 链接 OpenCV 和 Qt 库 target_link_libraries(opencv_onnx Qt6::Widgets ${OpenCV_LIBS} comdlg32) -# -# -#add_executable(create_mask src/Matrox/mask.cpp) -#target_link_libraries(create_mask Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS}) -#add_executable(test_color_range tests/test_color_range.cpp) + diff --git a/src/Matrox/utils.cpp b/src/Matrox/utils.cpp index 80b8856..30636db 100644 --- a/src/Matrox/utils.cpp +++ b/src/Matrox/utils.cpp @@ -11,7 +11,13 @@ #include #include "Mil.h" #include + +#include +#include +#include +#include using namespace std; +using namespace cv; /** * Convert Lab values from Photoshop range to OpenCV range. @@ -121,11 +127,6 @@ void read_params_from_file(const std::string& filename, std::map -#include -#include -#include - // 函数:从配置文件中读取参数 @@ -167,61 +168,52 @@ std::unordered_map loadConfig(const std::string& filename){ // 图片转换函数,输入4096*1024*3的图片,输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1 -#include "mil.h" // 包含 MIL 库的头文件 +Mat mil2mat(const MIL_ID mil_img) { + // 获取 MIL 图像的宽度、高度和通道数 + MIL_INT width, height, channels; + MbufInquire(mil_img, M_SIZE_X, &width); + MbufInquire(mil_img, M_SIZE_Y, &height); + MbufInquire(mil_img, M_SIZE_BAND, &channels); -// cv::Mat milToMat(MIL_ID milImage, MIL_ID MilSystem) { -// // 获取图像的宽度、高度和像素格式 -// int width, height, channels; -// MbufInquire(milImage, M_SIZE_X, &width); // 获取图像宽度 -// MbufInquire(milImage, M_SIZE_Y, &height); // 获取图像高度 -// MbufInquire(milImage, M_SIZE_BAND, &channels); // 获取图像通道数 -// -// // 为图像数据分配内存 -// byte* buf = new byte[width * height](); // 初始化内存 -// -// // 如果是彩色图像,则转换为灰度图 -// if (channels > 1) { -// MIL_ID grayscaleImage; -// // 分配一个新的灰度图像缓冲区 -// MbufAlloc2d(MilSystem, width, height, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &grayscaleImage); -// // 将彩色图像转换为灰度图 -// MimConvert(milImage, grayscaleImage, M_RGB_TO_L); -// // 获取转换后的灰度图像数据 -// MbufGet(grayscaleImage, buf); -// MbufFree(grayscaleImage); // 释放灰度图像缓冲区 -// } else { -// // 如果本身是灰度图像,直接获取数据 -// MbufGet(milImage, buf); -// } -// -// // 将数据封装成OpenCV Mat对象 -// cv::Mat cvMat(height, width, CV_8UC1, buf); -// return cvMat; -// } -cv::Mat milToMat(MIL_ID milImage) { - // 获取图像的宽度、高度和像素格式 - int width, height; - MIL_INT pixelFormat; - MbufInquire(milImage, M_SIZE_X, &width); // 获取图像宽度 - MbufInquire(milImage, M_SIZE_Y, &height); // 获取图像高度 - MbufInquire(milImage, M_TYPE, &pixelFormat); // 获取图像像素格式 - - // 判断像素格式并选择适当的数据类型 - // MIL_INT 和 M_UINT8 是典型的像素格式,OpenCV 处理不同的类型会有不同的实现 - cv::Mat matImage; - if (pixelFormat == M_RGB) - { - // 如果是RGB图像 - matImage = cv::Mat(height, width, CV_8UC3); + if (channels == 1) { + // 单通道图像,直接读取整个缓冲区 + Mat grayImage(height, width, CV_8UC1); + MbufGet(mil_img, grayImage.data); + return grayImage; } - else - {// 如果是其他类型的图像,你需要根据实际格式进行调整 - std::cerr << "Unsupported MIL image format!" << std::endl; - return matImage; + if (channels == 3) { + // 多通道图像,分通道读取 + MIL_ID redChannel, greenChannel, blueChannel; + MbufAlloc2d(M_DEFAULT, width, height, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &redChannel); + MbufAlloc2d(M_DEFAULT, width, height, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &greenChannel); + MbufAlloc2d(M_DEFAULT, width, height, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &blueChannel); + + // 将 MIL 图像的各个通道复制到单通道缓冲区 + MbufCopyColor(mil_img, redChannel, M_RED); + MbufCopyColor(mil_img, greenChannel, M_GREEN); + MbufCopyColor(mil_img, blueChannel, M_BLUE); + + // 分别读取每个通道的数据 + Mat redMat(height, width, CV_8UC1); + Mat greenMat(height, width, CV_8UC1); + Mat blueMat(height, width, CV_8UC1); + MbufGet(redChannel, redMat.data); + MbufGet(greenChannel, greenMat.data); + MbufGet(blueChannel, blueMat.data); + // 释放通道缓冲区 + MbufFree(redChannel); + MbufFree(greenChannel); + MbufFree(blueChannel); + + // 合并通道 + std::vector bgrChannels = {blueMat, greenMat, redMat}; + Mat colorImage; + cv::merge(bgrChannels, colorImage); + + return colorImage; } - - // 获取图像数据并复制到 OpenCV Mat - MbufGet(milImage, matImage.data); // 获取 MIL 图像数据并拷贝到 Mat 对象 - - return matImage; + // 不支持的通道数 + std::cerr << "[Error] Unsupported number of channels: " << channels << std::endl; + return Mat(); } + diff --git a/src/Matrox/utils.h b/src/Matrox/utils.h index 9e6cc8d..5ca75cb 100644 --- a/src/Matrox/utils.h +++ b/src/Matrox/utils.h @@ -39,7 +39,7 @@ MIL_ID convert_to_uint8(MIL_ID input_img); std::wstring convert_to_wstring(const std::string& str); void read_params_from_file(const std::string& filename, std::map& params) ; -cv::Mat milToMat(MIL_ID MilImage); +cv::Mat mil2mat(MIL_ID mil_img); void processImage(cv::Mat& img); std::unordered_map loadConfig(const std::string& filename); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b7361ed..4c23ee4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,7 +14,6 @@ add_executable(test_template_matching target_link_libraries(test_template_matching Matrox ${OpenCV_LIBS} ${MIL_LIBS}) - #测试用例 3:test_mask add_executable(test_mask ${CMAKE_CURRENT_SOURCE_DIR}/test_mask.cpp) @@ -27,11 +26,11 @@ add_executable(test_onnx # 链接 Matrox 模块和依赖库 target_link_libraries(test_onnx CVDL ${OpenCV_LIBS} ${MIL_LIBS}) -#测试用例 4:test_onnx +#测试用例 4:test_MIltoMat add_executable(test_MILtoMat ${CMAKE_CURRENT_SOURCE_DIR}/test_MILtoMat.cpp) # 链接 Matrox 模块和依赖库 -target_link_libraries(test_MILtoMat CVDL ${OpenCV_LIBS} ${MIL_LIBS}) +target_link_libraries(test_MILtoMat Matrox CVDL ${OpenCV_LIBS} ${MIL_LIBS}) #测试用例 4:test_onnx add_executable(test_total diff --git a/tests/test_MILtoMat.cpp b/tests/test_MILtoMat.cpp index b5a532c..f7028a3 100644 --- a/tests/test_MILtoMat.cpp +++ b/tests/test_MILtoMat.cpp @@ -1,82 +1,63 @@ #include +#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") +#define IMAGE_PATH MIL_TEXT(".\\test_imgs\\test_gray.png") +#define IMAGE_PATH_COLOR MIL_TEXT(".\\test_imgs\\8.bmp") +#define SAVE_PATH MIL_TEXT(".\\runs\\test_MILtoMat.png") + +using namespace std; + +void show_img_info(const MIL_ID mil_img) { + // 获取图像尺寸和通道数 + MIL_INT width, height, channels; + MbufInquire(mil_img, M_SIZE_X, &width); // 获取图像宽度 + MbufInquire(mil_img, M_SIZE_Y, &height); // 获取图像高度 + MbufInquire(mil_img, M_SIZE_BAND, &channels); // 获取图像通道数 + cout << "Gray MIL Image loaded successfully!" << endl; + cout << "Width: " << width << endl; + cout << "Height: " << height << endl; + cout << "Channels: " << channels << endl; +} 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; - } - // 加载图像 + // 测试用例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 显示图像 - + show_img_info(MilImage); + cv::Mat cvImg = mil2mat(MilImage); + cout << "MIL Image converted successfully!" << endl; + cv::imshow("MIL Image", cvImg); // 使用 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; + string savepath = ".\\runs\\test_MILtoMat_gray.png"; + if (!cv::imwrite(savepath, cvImg)) { + cerr << "Failed to save image!" << endl; } - // 释放资源 - free(m_AvsBuffer); // 释放缓冲区 - MbufFree(MilImage); // 释放 MIL 图像资源 - MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); // 释放 MIL 系统资源 + //测试用例2:彩色图测试 + MIL_ID MilImage2 = M_NULL; + MbufRestore(IMAGE_PATH_COLOR, MilSystem, &MilImage2); + // show_img_info(MilImage2); + cv::Mat cvImgColor = mil2mat(MilImage2); + cout << "MIL Image converted successfully!" << endl; + cv::imshow("MIL Image COLOR", cvImgColor); // 使用 OpenCV 显示图像 + // 等待按键并关闭窗口 + cv::waitKey(0); + // 保存图像 + savepath = ".\\runs\\test_MILtoMat_color.png"; + if (!imwrite(savepath, cvImgColor)) { + cerr << "Failed to save image!" << endl; + } + MbufFree(MilImage); // 释放 MIL 图像资源 + MbufFree(MilImage2); + MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL); // 释放 MIL 系统资源 return 0; } diff --git a/tests/test_imgs/8.bmp b/tests/test_imgs/8.bmp new file mode 100644 index 0000000..fe1041e Binary files /dev/null and b/tests/test_imgs/8.bmp differ diff --git a/tests/test_imgs/test_gray.png b/tests/test_imgs/test_gray.png new file mode 100644 index 0000000..4969c15 Binary files /dev/null and b/tests/test_imgs/test_gray.png differ diff --git a/tests/test_onnx.cpp b/tests/test_onnx.cpp index 9654906..5964892 100644 --- a/tests/test_onnx.cpp +++ b/tests/test_onnx.cpp @@ -1,69 +1,3 @@ -// #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" @@ -99,7 +33,7 @@ int main() { // Create and show the detection mask cv::Mat detectionMask = createDetectionMask(image, finalDetections, scale, padTop, padLeft); - cv::imshow("Detection Mask", detectionMask); + // cv::imshow("Detection Mask", detectionMask); // Save the result