mirror of
https://github.com/Karllzy/cotton_color.git
synced 2025-11-09 11:13:54 +00:00
两线程
This commit is contained in:
parent
3f21a63291
commit
80e6aca858
@ -13,13 +13,13 @@
|
||||
#include <filesystem> // For directory traversal
|
||||
|
||||
// 宏定义
|
||||
#define SAVE_PATH3 MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect.png")
|
||||
#define SAVE_PATH4 MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect2.png")
|
||||
#define IMAGE_DIR MIL_TEXT(".\\test_imgs\\cotton_image_new") // 文件夹路径
|
||||
// #define SAVE_PATH3 MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect.png")
|
||||
// #define SAVE_PATH4 MIL_TEXT("C:\\Users\\zjc\\Desktop\\suspect2.png")
|
||||
#define IMAGE_DIR MIL_TEXT("C:\\Users\\ZLSDKJ\\Desktop\\iamge\\1.bmp") // 文件夹路径
|
||||
|
||||
#define run_high_sat true;
|
||||
#define run_templating true;
|
||||
#define run_deep_learning true;
|
||||
#define run_templating false;
|
||||
#define run_deep_learning false;
|
||||
|
||||
MIL_ID MilApplication, MilSystem, MilDisplay;
|
||||
std::map<std::string, int> params;
|
||||
@ -51,6 +51,14 @@ cv::Mat overlayResultOnInput(const cv::Mat& cv_input, const cv::Mat& total_resul
|
||||
// 5. 返回叠加后的图像
|
||||
return overlay;
|
||||
}
|
||||
void process_iterations(MIL_ID MilImage, MIL_ID MilHighSatResult, const std::map<std::string, int>& params, int iterations, const std::string& thread_name) {
|
||||
Timer timer;
|
||||
for(int i = 0; i < iterations; ++i) {
|
||||
timer.restart();
|
||||
high_sat_detect(MilImage, MilHighSatResult, params);
|
||||
timer.printElapsedTime(thread_name + " - Iteration " + std::to_string(i+1) + " finished");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 初始化 MIL 应用
|
||||
@ -58,91 +66,20 @@ int main() {
|
||||
|
||||
Timer timer1, timer2;
|
||||
std::map<std::string, int> params;
|
||||
read_params_from_file("..\\config\\color_range_config.txt", params);
|
||||
read_params_from_file("..\\config\\template_color_config.txt", params);
|
||||
read_params_from_file("C:\\Users\\ZLSDKJ\\Desktop\\color_range_config.txt", params);
|
||||
// read_params_from_file("..\\config\\template_color_config.txt", params);
|
||||
MIL_ID MilImage = M_NULL, MilHighSatResult = M_NULL, MilTemplateMatchingResult = M_NULL;
|
||||
MbufRestore(IMAGE_DIR, MilSystem, &MilImage);
|
||||
const int iterations_per_thread = 500;
|
||||
|
||||
#if run_templating
|
||||
TemplateMatcher matcher(params);
|
||||
matcher.LoadConfig("..\\config\\template_config.txt");
|
||||
#endif
|
||||
// 创建两个线程
|
||||
std::thread thread1(process_iterations, MilImage, MilHighSatResult, std::cref(params), iterations_per_thread, "Thread 1");
|
||||
std::thread thread2(process_iterations, MilImage, MilHighSatResult, std::cref(params), iterations_per_thread, "Thread 2");
|
||||
|
||||
#if run_deep_learning
|
||||
ONNXRunner runner;
|
||||
runner.load("C:\\Users\\zjc\\Desktop\\dimo_11.14.onnx");
|
||||
#endif
|
||||
// 等待两个线程完成
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
timer1.printElapsedTime("Load config and templates and models");
|
||||
|
||||
cout << "Sequence running start:" << endl;
|
||||
|
||||
// 遍历文件夹中的所有图片文件
|
||||
for (const auto& entry : fs::directory_iterator(IMAGE_DIR)) {
|
||||
if (entry.is_regular_file()) {
|
||||
string image_path = entry.path().string();
|
||||
cout << "Processing image: " << image_path << endl;
|
||||
|
||||
// 读取当前图片
|
||||
MIL_ID MilImage = M_NULL, MilHighSatResult = M_NULL, MilTemplateMatchingResult = M_NULL;
|
||||
MbufRestore(convert_to_wstring(image_path), MilSystem, &MilImage);
|
||||
|
||||
timer1.restart();
|
||||
timer2.restart();
|
||||
cv::Mat deep_result, high_sat_result, template_result, total_result;
|
||||
|
||||
// 艳丽色彩检测
|
||||
#if run_high_sat
|
||||
high_sat_detect(MilImage, MilHighSatResult, params);
|
||||
high_sat_result = mil2mat(MilHighSatResult);
|
||||
timer1.printElapsedTime("High Sat finished");
|
||||
#else
|
||||
high_sat_result = cv::Mat::zeros(1024, 4096, CV_8UC1);
|
||||
#endif
|
||||
|
||||
#if run_templating
|
||||
// 模板匹配检测
|
||||
matcher.predict(MilImage, MilTemplateMatchingResult, params, false);
|
||||
template_result = mil2mat(MilTemplateMatchingResult);
|
||||
timer1.printElapsedTime("Template Matching finished");
|
||||
#else
|
||||
template_result = cv::Mat::zeros(1024, 4096, CV_8UC1);
|
||||
#endif
|
||||
|
||||
#if run_deep_learning
|
||||
// 深度学习检测
|
||||
cv::Mat cv_input = mil2mat(MilImage);
|
||||
std::vector<Detection> result = runner.predict(cv_input);
|
||||
deep_result = runner.postProcess(result, cv_input);
|
||||
#else
|
||||
deep_result = cv::Mat::zeros(1024, 4096, CV_8UC1);
|
||||
#endif
|
||||
|
||||
timer1.printElapsedTime("Deep Learning finished");
|
||||
|
||||
if (!deep_result.empty() && !high_sat_result.empty() && !template_result.empty()) {
|
||||
cv::bitwise_or(deep_result, high_sat_result, total_result);
|
||||
cv::bitwise_or(total_result, template_result, total_result);
|
||||
} else {
|
||||
cerr << "Error: One or more detection results are empty!" << endl;
|
||||
}
|
||||
|
||||
timer2.printElapsedTime("Prediction finished Total");
|
||||
|
||||
// 保存结果
|
||||
cv::imwrite("./runs/deep_result_" + entry.path().filename().string() + ".png", deep_result);
|
||||
cv::imwrite("./runs/high_sat_result_" + entry.path().filename().string() + ".png", high_sat_result);
|
||||
cv::imwrite("./runs/template_result_" + entry.path().filename().string() + ".png", template_result);
|
||||
cv::imwrite("./runs/total_result_" + entry.path().filename().string() + ".png", total_result);
|
||||
|
||||
std::vector<cv::Mat> images = { deep_result, high_sat_result, template_result, total_result };
|
||||
displayCombinedResults(images, "Combined Results");
|
||||
images = { cv_input, overlayResultOnInput(cv_input, total_result, 0.1, cv::COLORMAP_OCEAN)};
|
||||
displayCombinedResults(images, "Combined Results");
|
||||
// 释放当前图片资源
|
||||
MbufFree(MilImage);
|
||||
MbufFree(MilHighSatResult);
|
||||
MbufFree(MilTemplateMatchingResult);
|
||||
}
|
||||
}
|
||||
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user