模板匹配模块完成

This commit is contained in:
zjc-zjc-123 2024-11-20 15:56:19 +08:00
parent 4cc10387b0
commit a2f24318ed
20 changed files with 576 additions and 376 deletions

View File

@ -37,39 +37,45 @@ include_directories(E:/QTexamble/matrox/Include)
link_directories(E:/QTexamble/matrox/LIB)
file(GLOB MIL_LIBS E:/QTexamble/matrox/LIB/*.lib)
# 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 Matrox/color_range.cpp
Matrox/color_range.h Matrox/utils.h Matrox/utils.cpp)
# OpenCV Qt
target_link_libraries(cotton_range Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS})
#
add_subdirectory(src)
add_subdirectory(tests)
# cotton_color2
add_executable(cotton_color2 cotton_color2.cpp)
# OpenCV Qt
target_link_libraries(cotton_color2 Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS})
add_executable(color_matching Matrox/template_matching.cpp
Matrox/utils.cpp
Matrox/utils.h)
target_link_libraries(color_matching Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS})
add_executable(ui Matrox/ui.cpp)
target_link_libraries(ui Qt6::Widgets)
add_executable(onnx 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 Matrox/mask.cpp)
target_link_libraries(create_mask Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS})
## 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)

View File

@ -1,222 +0,0 @@
#include <mil.h>
//#include <milim.h> // 添加此行
#include <iostream>
/* Example functions declarations. */
void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay);
/*****************************************************************************/
/* Main.
******************************************************************************/
int MosMain(void)
{
MIL_ID MilApplication, /* Application identifier. */
MilSystem, /* System Identifier. */
MilDisplay; /* Display identifier. */
/* Allocate defaults. */
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
/* Run single model example. */
SingleModelExample(MilSystem, MilDisplay);
/* Free defaults. */
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
/*****************************************************************************/
/* Single model example. */
/* Source MIL image file specifications. */
#define SINGLE_MODEL_IMAGE MIL_TEXT("C:\\Users\\zjc\\Desktop\\diguandai2.png") // 替换为您的模板RGB图像文件路径
/* Target MIL image file specifications. */
#define SINGLE_MODEL_TARGET_IMAGE MIL_TEXT ("C:\\Users\\zjc\\Desktop\\diguandai.png") // 替换为您的待检测RGB图像文件路径
/* Search speed: M_VERY_HIGH for faster search, M_MEDIUM for precision and robustness. */
#define SINGLE_MODEL_SEARCH_SPEED M_LOW
/* Model specifications. */
#define MODEL_OFFSETX 3200L // 根据您的模板图像调整
#define MODEL_OFFSETY 550L // 根据您的模板图像调整
#define MODEL_SIZEX 200L // 根据您的模板图像调整
#define MODEL_SIZEY 200L // 根据您的模板图像调整
#define MODEL_MAX_OCCURRENCES 6L
void SingleModelExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
clock_t start_time = clock();
MIL_ID MilColorImage, /* 彩色图像缓冲区标识符。*/
MilImage, /* 灰度图像缓冲区标识符。*/
GraphicList; /* 图形列表标识符。*/
MIL_ID MilSearchContext, /* 搜索上下文。*/
MilResult; /* 结果标识符。*/
MIL_DOUBLE ModelDrawColor = M_COLOR_RED; /* 模板绘制颜色。*/
MIL_INT Model[MODEL_MAX_OCCURRENCES], /* 模板索引。*/
NumResults = 0L; /* 找到的结果数量。*/
MIL_DOUBLE Score[MODEL_MAX_OCCURRENCES], /* 模板匹配得分。*/
XPosition[MODEL_MAX_OCCURRENCES], /* 模板X位置。*/
YPosition[MODEL_MAX_OCCURRENCES], /* 模板Y位置。*/
Angle[MODEL_MAX_OCCURRENCES], /* 模板角度。*/
Scale[MODEL_MAX_OCCURRENCES], /* 模板缩放。*/
Time = 0.0; /* 计时变量。*/
int i; /* 循环变量。*/
/* 加载RGB模板图像。 */
MbufRestore(SINGLE_MODEL_IMAGE, MilSystem, &MilColorImage);
/* 获取图像尺寸。 */
MIL_INT Width = MbufInquire(MilColorImage, M_SIZE_X, M_NULL);
MIL_INT Height = MbufInquire(MilColorImage, M_SIZE_Y, M_NULL);
/* 分配灰度图像缓冲区。 */
MbufAlloc2d(MilSystem, Width, Height, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilImage);
/* 将RGB图像转换为灰度图像。 */
MimConvert(MilColorImage, MilImage, M_RGB_TO_L);
/* 选择灰度图像进行显示。 */
MdispSelect(MilDisplay, MilImage);
/* 释放彩色图像缓冲区。 */
MbufFree(MilColorImage);
/* Allocate a graphic list to hold the subpixel annotations to draw. */
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
/* Associate the graphic list to the display for annotations. */
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
/* Allocate a Geometric Model Finder context. */
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext);
/* Allocate a result buffer. */
MmodAllocResult(MilSystem, M_DEFAULT, &MilResult);
/* Define the model. */
MmodDefine(MilSearchContext, M_IMAGE, MilImage,
MODEL_OFFSETX, MODEL_OFFSETY, MODEL_SIZEX, MODEL_SIZEY);
/* Set the search speed. */
MmodControl(MilSearchContext, M_CONTEXT, M_SPEED, SINGLE_MODEL_SEARCH_SPEED);
/* Preprocess the search context. */
MmodPreprocess(MilSearchContext, M_DEFAULT);
/* Draw box and position it in the source image to show the model. */
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, MilSearchContext, GraphicList,
M_DRAW_BOX + M_DRAW_POSITION, 0, M_ORIGINAL);
clock_t end_time = clock();
std::cout << "The run time is: " << (double)(end_time - start_time) / CLOCKS_PER_SEC << "s";
/* Pause to show the model. */
MosPrintf(MIL_TEXT("\nGEOMETRIC MODEL FINDER:\n"));
MosPrintf(MIL_TEXT("-----------------------\n\n"));
MosPrintf(MIL_TEXT("A model context was defined with "));
MosPrintf(MIL_TEXT("the model in the displayed image.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
/* Clear annotations. */
MgraClear(M_DEFAULT, GraphicList);
/* 加载RGB待检测图像。 */
MbufRestore(SINGLE_MODEL_TARGET_IMAGE, MilSystem, &MilColorImage);
/* 确保待检测图像的尺寸与模板图像一致。 */
MIL_INT TargetWidth = MbufInquire(MilColorImage, M_SIZE_X, M_NULL);
MIL_INT TargetHeight = MbufInquire(MilColorImage, M_SIZE_Y, M_NULL);
/* 如果尺寸不同,需要重新分配灰度图像缓冲区。 */
if (TargetWidth != Width || TargetHeight != Height)
{
/* 释放之前的灰度图像缓冲区。 */
MbufFree(MilImage);
/* 分配新的灰度图像缓冲区。 */
MbufAlloc2d(MilSystem, TargetWidth, TargetHeight, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &MilImage);
/* 更新宽度和高度。 */
Width = TargetWidth;
Height = TargetHeight;
}
/* 将RGB待检测图像转换为灰度图像。 */
MimConvert(MilColorImage, MilImage, M_RGB_TO_L);
/* 释放彩色图像缓冲区。 */
MbufFree(MilColorImage);
/* 显示灰度待检测图像。 */
MdispSelect(MilDisplay, MilImage);
/* Dummy first call for bench measure purpose only (bench stabilization,
cache effect, etc...). This first call is NOT required by the application. */
MmodFind(MilSearchContext, MilImage, MilResult);
/* Reset the timer. */
MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL);
/* Find the model. */
MmodFind(MilSearchContext, MilImage, MilResult);
/* Read the find time. */
MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time);
/* Get the number of models found. */
MmodGetResult(MilResult, M_DEFAULT, M_NUMBER + M_TYPE_MIL_INT, &NumResults);
/* If a model was found above the acceptance threshold. */
if ((NumResults >= 1) && (NumResults <= MODEL_MAX_OCCURRENCES))
{
/* Get the results of the single model. */
MmodGetResult(MilResult, M_DEFAULT, M_INDEX + M_TYPE_MIL_INT, Model);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition);
MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle);
MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale);
MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score);
/* Print the results for each model found. */
MosPrintf(MIL_TEXT("The model was found in the target image:\n\n"));
MosPrintf(MIL_TEXT("Result Model X Position Y Position ")
MIL_TEXT("Angle Scale Score\n\n"));
for (i = 0; i < NumResults; i++)
{
MosPrintf(MIL_TEXT("%-9d%-8d%-13.2f%-13.2f%-8.2f%-8.2f%-5.2f%%\n"),
i, (int)Model[i], XPosition[i], YPosition[i],
Angle[i], Scale[i], Score[i]);
}
MosPrintf(MIL_TEXT("\nThe search time is %.1f ms\n\n"), Time * 1000.0);
/* Draw edges, position and box over the occurrences that were found. */
for (i = 0; i < NumResults; i++)
{
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, MilResult, GraphicList,
M_DRAW_EDGES + M_DRAW_BOX + M_DRAW_POSITION, i, M_DEFAULT);
}
}
else
{
MosPrintf(MIL_TEXT("The model was not found or the number of models ")
MIL_TEXT("found is greater than\n"));
MosPrintf(MIL_TEXT("the specified maximum number of occurrence !\n\n"));
}
/* Wait for a key to be pressed. */
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
/* Free MIL objects. */
MgraFree(GraphicList);
MbufFree(MilImage);
MmodFree(MilSearchContext);
MmodFree(MilResult);
}

View File

@ -1,8 +0,0 @@
//
// Created by zjc on 24-11-12.
//
#ifndef TEMPLATE_MATCHING_H
#define TEMPLATE_MATCHING_H
#endif //TEMPLATE_MATCHING_H

View File

@ -1,9 +0,0 @@
//
// Created by zjc on 24-11-18.
//
#include "utils.h"
// 图片转换函数输入4096*1024*3的图片输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1

12
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
# Matrox
add_library(Matrox
Matrox/color_range.cpp
Matrox/utils.cpp
Matrox/template_matching.cpp
)
#
target_include_directories(Matrox PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
#
target_link_libraries(Matrox PUBLIC Qt6::Widgets ${OpenCV_LIBS} ${MIL_LIBS})

View File

@ -72,3 +72,15 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma
| lab_denoising = 1 | | | ![diguandai](./README.assets/diguandai.png) |
| lab_denoising = 2 | | | |
这些是经过实验后的推荐参数:
| | L_min | L_max | a_min | a_max | b_min | b_max |
| ------ | ----- | ----- | ----- | ----- | ----- | ----- |
| green | 27 | 49 | -27 | -8 | 2 | 12 |
| blue | 18 | 26 | 2 | 17 | -33 | -23 |
| orange | 65 | 75 | 7 | 14 | 32 | 46 |
| black | 0 | 8 | -1 | 5 | -2 | 6 |
| red | 28 | 38 | 15 | 25 | -95 | 26 |
| purple | 67 | 77 | 3 | 13 | -20 | -5 |

View File

@ -4,15 +4,10 @@
#include <string>
#include "utils.h"
#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton_image\\174.bmp")
#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\diguandai.png")
// Global variables
MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
// Optimized LabProcess function
void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map<std::string, int>& params)
void lab_process_raw(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map<std::string, int>& params,
const std::vector<std::string>& color_vector)
{
MIL_ID MilLabImage = M_NULL, MilLChannel = M_NULL, MilAChannel = M_NULL, MilBChannel = M_NULL;
MIL_ID lab_result=M_NULL;
@ -56,11 +51,11 @@ void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::ma
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &MilBinaryB);
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &MilResultLab);
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &lab_result);
const std::vector<std::string> colors = {"green", "blue", "orange", "black", "red", "purple"};
// Iterate over colors
// 遍历颜色
for (const auto& color : colors) {
for (const auto& color : color_vector) {
// 构建参数键
std::string L_min_key = color + "_L_min";
std::string L_max_key = color + "_L_max";
@ -76,6 +71,18 @@ void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::ma
int a_max = params.at(a_max_key);
int b_min = params.at(b_min_key);
int b_max = params.at(b_max_key);
std::vector<int> lab_min_ps = {L_min, a_min, b_min};
std::vector<int> lab_max_ps = {L_max, a_max, b_max};
std::vector<int> lab_min_cv = psLabToOpenCVLab(lab_min_ps);
std::vector<int> lab_max_cv = psLabToOpenCVLab(lab_max_ps);
L_min = lab_min_cv[0];
L_max = lab_max_cv[0];
a_min = lab_min_cv[1];
a_max = lab_max_cv[1];
b_min = lab_min_cv[2];
b_max = lab_max_cv[2];
// 对每个通道进行二值化
MimBinarize(MilLChannel, MilBinaryL, M_IN_RANGE, L_min, L_max);
@ -107,6 +114,13 @@ void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::ma
MbufFree(lab_result);
}
void lab_process(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map<std::string, int>& params) {
const std::vector<std::string> colors = {"green", "blue", "orange", "black", "red", "purple"};
lab_process_raw(inputImage, outputImageLab, params, colors);
}
void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::map<std::string, int>& params)
{
MIL_ID MilHSVImage = M_NULL, MilHChannel = M_NULL, MilSChannel = M_NULL, MilVChannel = M_NULL;
@ -142,13 +156,13 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma
// 分配输出图像缓冲区
MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL),
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED,
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED,
M_IMAGE + M_PROC + M_DISP, &hsv_result);
MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL),
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED,
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED,
M_IMAGE + M_PROC + M_DISP, &hsv_denoising);
MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL),
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED,
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED,
M_IMAGE + M_PROC + M_DISP, &outputImageHSV);
// 对 S 通道进行阈值分割
@ -167,6 +181,7 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma
MbufFree(hsv_denoising);
}
void high_sat_detect(const MIL_ID& inputImage, MIL_ID& outputImage, const std::map<std::string, int>& params) {
MIL_ID output_hsv=M_NULL, output_lab=M_NULL;
@ -184,81 +199,4 @@ void high_sat_detect(const MIL_ID& inputImage, MIL_ID& outputImage, const std::m
MbufFree(output_hsv);
}
int main()
{
// Initialize MIL application
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL,
M_NULL);
// Load input image
MIL_ID MilImage = M_NULL;
MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
// Define color ranges
std::map<std::string, int> params;
params["green_L_min"] = 68;
params["green_L_max"] = 125;
params["green_a_min"] = 101;
params["green_a_max"] = 120;
params["green_b_min"] = 130;
params["green_b_max"] = 140;
params["blue_L_min"] = 45;
params["blue_L_max"] = 66;
params["blue_a_min"] = 130;
params["blue_a_max"] = 145;
params["blue_b_min"] = 95;
params["blue_b_max"] = 105;
params["orange_L_min"] = 166;
params["orange_L_max"] = 191;
params["orange_a_min"] = 135;
params["orange_a_max"] = 142;
params["orange_b_min"] = 160;
params["orange_b_max"] = 174;
params["black_L_min"] = 0;
params["black_L_max"] = 21;
params["black_a_min"] = 127;
params["black_a_max"] = 133;
params["black_b_min"] = 126;
params["black_b_max"] = 134;
params["red_L_min"] = 71;
params["red_L_max"] = 97;
params["red_a_min"] = 143;
params["red_a_max"] = 153;
params["red_b_min"] = 33;
params["red_b_max"] = 154;
params["purple_L_min"] = 171;
params["purple_L_max"] = 197;
params["purple_a_min"] = 131;
params["purple_a_max"] = 141;
params["purple_b_min"] = 108;
params["purple_b_max"] = 123;
params["lab_denoising"] = 1;
params["saturation_threshold"] = 150;
params["saturation_denoising"] = 1;
// Initialize combined result
MIL_ID detection_result = M_NULL;
// Measure execution time
measure_execution_time([&]() {
high_sat_detect(MilImage, detection_result, params);
});
MbufSave(SAVE_PATH, detection_result);
// Display result
std::cout << "所有颜色检测已完成并合并。按 <Enter> 退出。" << std::endl;
getchar();
// Free resources
MbufFree(detection_result);
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}

View File

@ -60,7 +60,10 @@ void hsv_process(const MIL_ID& inputImage, MIL_ID& outputImageHSV, const std::ma
// params["purple_b_max"] = 123;
//
void lab_process_raw(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std::map<std::string, int>& params,
const std::vector<std::string>& color_vector);
// 新增了可自定义的色彩矩阵
void high_sat_detect(const MIL_ID& inputImage, MIL_ID& outputImage, const std::map<std::string, int>& params);
#endif //COLOR_RANGE_H

View File

@ -13,9 +13,9 @@
#include <vector>
// Path definitions.
#define EXAMPLE_ONNX_MODEL_PATH MIL_TEXT("C:\\Users\\zjc\\source\\repos\\cotton_color\\Matrox\\models\\2024_11_12_imgsz640_batch1.onnx")
#define TARGET_IMAGE_DIR_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo2.mim")
#define IMAGE_FILE MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo2.bmp")
#define EXAMPLE_ONNX_MODEL_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\WeChat Files\\wxid_ipl8u0ctajtn22\\FileStorage\\File\\2024-11\\2024_11_12_imgsz640_batch1(1).onnx")
#define TARGET_IMAGE_DIR_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo4.mim")
int MosMain(void)
{
@ -31,14 +31,6 @@ int MosMain(void)
MsysAlloc(M_DEFAULT, M_SYSTEM_DEFAULT, M_DEFAULT, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay);
MIL_UNIQUE_BUF_ID dimo2;
MbufImport(IMAGE_FILE, M_DEFAULT, M_RESTORE+M_NO_GRAB+M_NO_COMPRESS, MilSystem, &dimo2);
//MIL_UNIQUE_BUF_ID MimArithdestination = MbufClone(dimo2, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_UNIQUE_ID);
MIL_UNIQUE_BUF_ID MimArithDestination = MbufAllocColor(MilSystem, 3, 640, 640, 32 + M_FLOAT, M_IMAGE + M_PROC, M_UNIQUE_ID);
// Post-Alloc Block for MimArith's destination
MbufClear(MimArithDestination, M_COLOR_BLACK);
MimArith(dimo2, 255.0, MimArithDestination, M_DIV_CONST);
// Load the image into memory.
if (MbufRestore(TARGET_IMAGE_DIR_PATH, MilSystem, &MilImage) != M_NULL)
@ -51,7 +43,6 @@ int MosMain(void)
return 1; // Exit if the image loading failed
}
MdispSelect(MilDisplay, MimArithDestination);
// MbufInquire(MilImage, , NULL);
@ -70,7 +61,7 @@ int MosMain(void)
// Perform object detection on the image using MclassPredict.
MclassPredict(DetectCtx, MimArithDestination, DetectRes, M_DEFAULT);
MclassPredict(DetectCtx, MilImage, DetectRes, M_DEFAULT);
MosPrintf(MIL_TEXT("Object detection completed.\n"));

View File

@ -0,0 +1,219 @@
#include <execution>
#include <mil.h>
#include <iostream>
#include "color_range.h"
#include "utils.h"
void pre_process(const MIL_ID& inputImage, MIL_ID& outputImageSuspect, const std::map<std::string, int>& params) {
// 异色检测, 检测出不同与棉花颜色的物体作为模板匹配的对象
const std::vector<std::string> colors = {"cotton", "background"};
std::map <std::string, int> param_temp = params;
param_temp["lab_denoising"] = param_temp["cotton_denoising"];
param_temp["saturation_threshold"] = param_temp["cotton_saturation_threshold"];
param_temp["saturation_denoising"] = param_temp["cotton_saturation_denoising"];
lab_process_raw(inputImage, outputImageSuspect, param_temp, colors);
MimArith(outputImageSuspect, M_NULL, outputImageSuspect, M_NOT);
}
/* Number of models. */
#define NUMBER_OF_MODELS 3L
#define MODELS_MAX_OCCURRENCES 16L
/* Model 1 specifications. */
#define MODEL0_OFFSETX 34L
#define MODEL0_OFFSETY 93L
#define MODEL0_SIZEX 214L
#define MODEL0_SIZEY 76L
#define MODEL0_DRAW_COLOR M_COLOR_RED
/* Model 2 specifications. */
#define MODEL1_OFFSETX 73L
#define MODEL1_OFFSETY 232L
#define MODEL1_SIZEX 150L
#define MODEL1_SIZEY 154L
#define MODEL1_REFERENCEX 23L
#define MODEL1_REFERENCEY 127L
#define MODEL1_DRAW_COLOR M_COLOR_GREEN
/* Model 3 specifications. */
#define MODEL2_OFFSETX 308L
#define MODEL2_OFFSETY 39L
#define MODEL2_SIZEX 175L
#define MODEL2_SIZEY 357L
#define MODEL2_REFERENCEX 62L
#define MODEL2_REFERENCEY 150L
#define MODEL2_DRAW_COLOR M_COLOR_BLUE
/* Search speed: M_VERY_HIGH for faster search, M_MEDIUM for precision and robustness.*/
#define MULTI_MODELS_SEARCH_SPEED M_VERY_HIGH
/* Models array specifications. */
#define MODELS_ARRAY_SIZE 3L
#define MODELS_OFFSETX {MODEL0_OFFSETX, MODEL1_OFFSETX, MODEL2_OFFSETX}
#define MODELS_OFFSETY {MODEL0_OFFSETY, MODEL1_OFFSETY, MODEL2_OFFSETY}
#define MODELS_SIZEX {MODEL0_SIZEX, MODEL1_SIZEX, MODEL2_SIZEX}
#define MODELS_SIZEY {MODEL0_SIZEY, MODEL1_SIZEY, MODEL2_SIZEY}
#define MODELS_DRAW_COLOR {MODEL0_DRAW_COLOR, MODEL1_DRAW_COLOR, MODEL2_DRAW_COLOR}
void template_matching(const MIL_ID& inputImage, MIL_ID& outputImage, const std::map<std::string, int>& params) {
MIL_ID MilImage, /* Image buffer identifier. */
GraphicList; /* Graphic list identifier. */
MIL_ID MilSearchContext, /* Search context */
MilResult; /* Result identifier. */
MIL_INT Models[MODELS_MAX_OCCURRENCES], /* Model indices. */
ModelsOffsetX[MODELS_ARRAY_SIZE] = MODELS_OFFSETX,/* Model X offsets array. */
ModelsOffsetY[MODELS_ARRAY_SIZE] = MODELS_OFFSETY,/* Model Y offsets array. */
ModelsSizeX[MODELS_ARRAY_SIZE] = MODELS_SIZEX, /* Model X sizes array. */
ModelsSizeY[MODELS_ARRAY_SIZE] = MODELS_SIZEY; /* Model Y sizes array. */
MIL_DOUBLE ModelsDrawColor[MODELS_ARRAY_SIZE]=MODELS_DRAW_COLOR; /* Model drawing colors.*/
MIL_INT NumResults = 0L; /* Number of results found. */
MIL_DOUBLE Score[MODELS_MAX_OCCURRENCES], /* Model correlation scores.*/
XPosition[MODELS_MAX_OCCURRENCES], /* Model X positions. */
YPosition[MODELS_MAX_OCCURRENCES], /* Model Y positions. */
Angle[MODELS_MAX_OCCURRENCES], /* Model occurrence angles. */
Scale[MODELS_MAX_OCCURRENCES], /* Model occurrence scales. */
Time = 0.0; /* Time variable. */
int i; /* Loop variable */
MIL_ID inputImageSuspect = convert_to_uint8(inputImage);
/* Restore the model image and display it. */
MdispSelect(MilDisplay, inputImageSuspect);
// 用于显示的部分
/* Allocate a graphic list to hold the subpixel annotations to draw. */
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
/* Associate the graphic list to the display for annotations. */
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
/* Allocate a geometric model finder. */
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &MilSearchContext);
// 取出一个buffer用来存储结果
/* Allocate a result buffer. */
MmodAllocResult(MilSystem, M_DEFAULT, &MilResult);
/* Define the models. */
for (i=0; i<NUMBER_OF_MODELS; i++)
{
MmodDefine(MilSearchContext, M_IMAGE, inputImageSuspect,
(MIL_DOUBLE)ModelsOffsetX[i], (MIL_DOUBLE)ModelsOffsetY[i],
(MIL_DOUBLE)ModelsSizeX[i], (MIL_DOUBLE)ModelsSizeY[i]);
}
// 设置一些参数
/* Set the desired search speed. */
MmodControl(MilSearchContext, M_CONTEXT, M_SPEED, MULTI_MODELS_SEARCH_SPEED);
/* Increase the smoothness for the edge extraction in the search context. */
MmodControl(MilSearchContext, M_CONTEXT, M_SMOOTHNESS, 75);
/* Modify the acceptance and the certainty for all the models that were defined. */
MmodControl(MilSearchContext, M_DEFAULT, M_ACCEPTANCE, 40);
MmodControl(MilSearchContext, M_DEFAULT, M_CERTAINTY, 60);
/* Set the number of occurrences to 2 for all the models that were defined. */
MmodControl(MilSearchContext, M_DEFAULT, M_NUMBER, 2);
#if (NUMBER_OF_MODELS>1)
/* Change the reference point of the second model. */
MmodControl(MilSearchContext, 1, M_REFERENCE_X, MODEL1_REFERENCEX);
MmodControl(MilSearchContext, 1, M_REFERENCE_Y, MODEL1_REFERENCEY);
#if (NUMBER_OF_MODELS>2)
/* Change the reference point of the third model. */
MmodControl(MilSearchContext, 2, M_REFERENCE_X, MODEL2_REFERENCEX);
MmodControl(MilSearchContext, 2, M_REFERENCE_Y, MODEL2_REFERENCEY);
#endif
#endif
/* Preprocess the search context. */
MmodPreprocess(MilSearchContext, M_DEFAULT);
/* Draw boxes and positions in the source image to identify the models. */
for (i=0; i<NUMBER_OF_MODELS; i++)
{
MgraColor(M_DEFAULT, ModelsDrawColor[i]);
MmodDraw( M_DEFAULT, MilSearchContext, GraphicList,
M_DRAW_BOX+M_DRAW_POSITION, i, M_ORIGINAL);
}
/* Pause to show the models. */
MosPrintf(MIL_TEXT("A model context was defined with the ")
MIL_TEXT("models in the displayed image.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
/* Clear annotations. */
MgraClear(M_DEFAULT, GraphicList);
/* Dummy first call for bench measure purpose only (bench stabilization,
cache effect, etc...). This first call is NOT required by the application. */
// MmodFind(MilSearchContext, inputImageSuspect, MilResult);
/* Reset the timer. */
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
/* Find the models. */
MmodFind(MilSearchContext, inputImageSuspect, MilResult);
/* Read the find time. */
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
/* Get the number of models found. */
MmodGetResult(MilResult, M_DEFAULT, M_NUMBER+M_TYPE_MIL_INT, &NumResults);
/* If the models were found above the acceptance threshold. */
if( (NumResults >= 1) && (NumResults <= MODELS_MAX_OCCURRENCES) )
{
/* Get the results for each model. */
MmodGetResult(MilResult, M_DEFAULT, M_INDEX+M_TYPE_MIL_INT, Models);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_X, XPosition);
MmodGetResult(MilResult, M_DEFAULT, M_POSITION_Y, YPosition);
MmodGetResult(MilResult, M_DEFAULT, M_ANGLE, Angle);
MmodGetResult(MilResult, M_DEFAULT, M_SCALE, Scale);
MmodGetResult(MilResult, M_DEFAULT, M_SCORE, Score);
/* Print information about the target image. */
MosPrintf(MIL_TEXT("The models were found in the target "));
MosPrintf(MIL_TEXT("image although there is:\n "));
MosPrintf(MIL_TEXT("Full rotation\n Small scale change\n "));
MosPrintf(MIL_TEXT("Contrast variation\n Specular reflection\n "));
MosPrintf(MIL_TEXT("Occlusion\n Multiple models\n"));
MosPrintf(MIL_TEXT(" Multiple occurrences\n\n"));
/* Print the results for the found models. */
MosPrintf(MIL_TEXT("Result Model X Position Y Position ")
MIL_TEXT("Angle Scale Score\n\n"));
for (i=0; i<NumResults; i++)
{
MosPrintf(MIL_TEXT("%-9d%-8d%-13.2f%-13.2f%-8.2f%-8.2f%-5.2f%%\n"),
i, (int) Models[i], XPosition[i], YPosition[i],
Angle[i], Scale[i], Score[i]);
}
MosPrintf(MIL_TEXT("\nThe search time is %.1f ms\n\n"), Time*1000.0);
/* Draw edges and positions over the occurrences that were found. */
for (i=0; i < NumResults; i++)
{
MgraColor(M_DEFAULT, ModelsDrawColor[Models[i]]);
MmodDraw(M_DEFAULT, MilResult,GraphicList,
M_DRAW_EDGES+M_DRAW_POSITION, i, M_DEFAULT);
}
}
else
{
MosPrintf(MIL_TEXT("The models were not found or the number of ")
MIL_TEXT("models found is greater than\n"));
MosPrintf(MIL_TEXT("the defined value of maximum occurrences !\n\n"));
}
/* Wait for a key to be pressed. */
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
/* Free MIL objects. */
MgraFree(GraphicList);
// MbufFree(MilImage);
MmodFree(MilSearchContext);
MmodFree(MilResult);
}

View File

@ -0,0 +1,10 @@
//
// Created by zjc on 24-11-12.
//
#ifndef TEMPLATE_MATCHING_H
#define TEMPLATE_MATCHING_H
void pre_process(const MIL_ID& inputImage, MIL_ID& outputImageSuspect, const std::map<std::string, int>& params);
void template_matching(const MIL_ID& inputImageSuspect, MIL_ID& outputImage, const std::map<std::string, int>& params);
#endif //TEMPLATE_MATCHING_H

63
src/Matrox/utils.cpp Normal file
View File

@ -0,0 +1,63 @@
//
// Created by zjc on 24-11-18.
//
#include "utils.h"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
/**
* Convert Lab values from Photoshop range to OpenCV range.
*
* @param lab_ps A vector of Lab values in Photoshop range [L (0-100), a (-128 to 127), b (-128 to 127)].
* @return A vector of Lab values in OpenCV range [L (0-255), a (0-255), b (0-255)].
*/
vector<int> psLabToOpenCVLab(const vector<int>& lab_ps) {
int l_ps = lab_ps[0];
int a_ps = lab_ps[1];
int b_ps = lab_ps[2];
// Conversion formulas
int l_cv = round((l_ps / 100.0) * 255.0); // Scale L from 0-100 to 0-255
int a_cv = round(((a_ps + 128.0) / 255.0) * 255.0); // Shift and scale a
int b_cv = round(((b_ps + 128.0) / 255.0) * 255.0); // Shift and scale b
return {l_cv, a_cv, b_cv};
}
/**
* Convert Lab values from OpenCV range to Photoshop range.
*
* @param lab_cv A vector of Lab values in OpenCV range [L (0-255), a (0-255), b (0-255)].
* @return A vector of Lab values in Photoshop range [L (0-100), a (-128 to 127), b (-128 to 127)].
*/
vector<int> opencvLabToPsLab(const vector<int>& lab_cv) {
int l_cv = lab_cv[0];
int a_cv = lab_cv[1];
int b_cv = lab_cv[2];
// Conversion formulas
int l_ps = round((l_cv / 255.0) * 100.0); // Scale L from 0-255 to 0-100
int a_ps = round((a_cv / 255.0) * 255.0 - 128.0); // Scale and shift a
int b_ps = round((b_cv / 255.0) * 255.0 - 128.0); // Scale and shift b
return {l_ps, a_ps, b_ps};
}
MIL_ID convert_to_uint8(MIL_ID input_img) {
MIL_ID output_img;
MIL_ID MilSystem = MbufInquire(input_img, M_OWNER_SYSTEM, M_NULL);
MIL_INT size_x = MbufInquire(input_img, M_SIZE_X, M_NULL);
MIL_INT size_y = MbufInquire(input_img, M_SIZE_Y, M_NULL);
MbufAlloc2d(MilSystem, size_x, size_y, 8 + M_UNSIGNED, M_IMAGE + M_PROC + M_DISP, &output_img);
MimArith(output_img, input_img, output_img, M_ADD);
MimArith(output_img, 255.0, output_img, M_MULT_CONST);
return output_img;
}
// 图片转换函数输入4096*1024*3的图片输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1

View File

@ -7,6 +7,14 @@
#include <chrono>
#include <iostream>
#include <vector>
#include <Mil.h>
// 声明全局变量(注意:这里只是声明,不是定义)
extern __int64 MilApplication;
extern __int64 MilSystem;
extern __int64 MilDisplay;
template <typename Func>
// Time measurement function
void measure_execution_time(Func func) {
@ -18,4 +26,10 @@ void measure_execution_time(Func func) {
std::cout << "Function execution time: " << duration.count() << " milliseconds" << std::endl;
}
std::vector<int> psLabToOpenCVLab(const std::vector<int>& lab_ps);
std::vector<int> opencvLabToPsLab(const std::vector<int>& lab_cv);
MIL_ID convert_to_uint8(MIL_ID input_img);
#endif //UTILS_H

16
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
# 1: template_color_range
add_executable(test_color_range
${CMAKE_CURRENT_SOURCE_DIR}/test_color_range.cpp
)
# Matrox
target_link_libraries(test_color_range Matrox ${OpenCV_LIBS} ${MIL_LIBS})
# 2: template_template_matching
add_executable(test_template_matching
${CMAKE_CURRENT_SOURCE_DIR}/test_template_matching.cpp
)
# Matrox
target_link_libraries(test_template_matching Matrox ${OpenCV_LIBS} ${MIL_LIBS})

View File

@ -0,0 +1,91 @@
#include <iostream>
#include <map>
#include <mil.h>
#include <string>
#include "Matrox/utils.h"
#include "Matrox/color_range.h"
#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton_image\\174.bmp")
#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\diguandai.png")
// Global variables
MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
int main()
{
// Initialize MIL application
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL,
M_NULL);
// Load input image
MIL_ID MilImage = M_NULL;
MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
// Define color ranges
std::map<std::string, int> params;
params["green_L_min"] = 27;
params["green_L_max"] = 49;
params["green_a_min"] = -27;
params["green_a_max"] = -8;
params["green_b_min"] = 2;
params["green_b_max"] = 12;
params["blue_L_min"] = 18;
params["blue_L_max"] = 26;
params["blue_a_min"] = 2;
params["blue_a_max"] = 17;
params["blue_b_min"] = -33;
params["blue_b_max"] = -23;
params["orange_L_min"] = 65;
params["orange_L_max"] = 75;
params["orange_a_min"] = 7;
params["orange_a_max"] = 14;
params["orange_b_min"] = 32;
params["orange_b_max"] = 46;
params["black_L_min"] = 0;
params["black_L_max"] = 8;
params["black_a_min"] = -1;
params["black_a_max"] = 5;
params["black_b_min"] = -2;
params["black_b_max"] = 6;
params["red_L_min"] = 28;
params["red_L_max"] = 38;
params["red_a_min"] = 15;
params["red_a_max"] = 25;
params["red_b_min"] = -95;
params["red_b_max"] = 26;
params["purple_L_min"] = 67;
params["purple_L_max"] = 77;
params["purple_a_min"] = 3;
params["purple_a_max"] = 13;
params["purple_b_min"] = -20;
params["purple_b_max"] = -5;
params["lab_denoising"] = 1;
params["saturation_threshold"] = 150;
params["saturation_denoising"] = 1;
// Initialize combined result
MIL_ID detection_result = M_NULL;
// Measure execution time
measure_execution_time([&]() {
high_sat_detect(MilImage, detection_result, params);
});
MbufSave(SAVE_PATH, detection_result);
// Display result
std::cout << "所有颜色检测已完成并合并。按 <Enter> 退出。" << std::endl;
getchar();
// Free resources
MbufFree(detection_result);
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}

View File

@ -0,0 +1,64 @@
//
// Created by zjc on 24-11-20.
//
#include <iostream>
#include <map>
#include <mil.h>
#include <string>
#include "Matrox/utils.h"
#include "Matrox/template_matching.h"
#define IMAGE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\cotton2.bmp")
#define SAVE_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect.png")
// Global variables
MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
int main() {
using namespace std;
map<string, int> params;
params["cotton_L_min"] = 40;
params["cotton_L_max"] = 90;
params["cotton_a_min"] = -5;
params["cotton_a_max"] = 6;
params["cotton_b_min"] = -5;
params["cotton_b_max"] = 30;
params["background_L_min"] = 95;
params["background_L_max"] = 100;
params["background_a_min"] = -3;
params["background_a_max"] = 3;
params["background_b_min"] = -3;
params["background_b_max"] = 3;
params["cotton_denoising"] = 1;
// Initialize MIL application
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL,
M_NULL);
// Load input image
MIL_ID MilImage = M_NULL;
MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
// Initialize combined result
MIL_ID detection_result = M_NULL;
// Measure execution time
measure_execution_time([&]() {
pre_process(MilImage, detection_result, params);
template_matching(detection_result, detection_result, params);
});
MbufSave(SAVE_PATH, detection_result);
// Display result
std::cout << "所有颜色检测已完成并合并。按 <Enter> 退出。" << std::endl;
getchar();
// Free resources
MbufFree(detection_result);
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}