修改了显示函数,但是多次预测的bug无法修复。

This commit is contained in:
zjc-zjc-123 2024-11-28 00:19:30 +08:00
parent 21b2a539f7
commit f9261d2fbb
2 changed files with 105 additions and 65 deletions

View File

@ -227,16 +227,19 @@ Mat mil2mat(const MIL_ID mil_img) {
}
#include <opencv2/opencv.hpp>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <windows.h>
void displayCombinedResults(const std::vector<cv::Mat>& images, const std::string& windowName) {
// Get the screen resolution (you can adjust this if needed, this is for a typical screen)
int screen_width = round(GetSystemMetrics(SM_CXSCREEN) * 0.4);
int screen_height = round(GetSystemMetrics(SM_CYSCREEN) * 0.4);
// Get the screen resolution
int screen_width = GetSystemMetrics(SM_CXSCREEN); // Screen width
int screen_height = GetSystemMetrics(SM_CYSCREEN); // Screen height
// First, we need to find the maximum width and height among the input images
// Find the maximum width and height among the input images
int max_width = 0;
int max_height = 0;
@ -248,12 +251,16 @@ void displayCombinedResults(const std::vector<cv::Mat>& images, const std::strin
}
}
// Resize all images to a reasonable scale to fit the screen
float scale_factor = min((float)screen_width / max_width, (float)screen_height / max_height);
// Calculate the scaling factor based on the maximum image width and screen width
float scale_factor = 1.0f; // Default scale factor is 1 (no scaling)
// If the images are already too large, reduce the scale factor
if (scale_factor > 1.0) scale_factor = 1.0;
// If images are larger than the screen width, scale them down
if (max_width > screen_width) {
scale_factor = (float)screen_width / max_width;
}
// Resize all images based on the calculated scale factor
std::vector<cv::Mat> resized_images;
for (const auto& img : images) {
if (!img.empty()) {
@ -296,7 +303,7 @@ void displayCombinedResults(const std::vector<cv::Mat>& images, const std::strin
// Set the window properties to allow resizing
cv::namedWindow(windowName, cv::WINDOW_NORMAL);
cv::resizeWindow(windowName, screen_width, screen_height); // Resize window to screen size
cv::resizeWindow(windowName, final_result.cols, final_result.rows); // Resize window based on the final image size
// Display the combined result
cv::imshow(windowName, final_result);

View File

@ -10,11 +10,12 @@
#include "Matrox/utils.h"
#include "Matrox/color_range.h"
#include "Matrox/template_matching.h"
#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_PATH MIL_TEXT(".\\test_imgs\\dimo.bmp")
#define IMAGE_DIR MIL_TEXT(".\\test_imgs\\cotton_image_new") // 文件夹路径
#define run_high_sat true;
#define run_templating true;
@ -23,20 +24,46 @@
MIL_ID MilApplication, MilSystem, MilDisplay;
std::map<std::string, int> params;
namespace fs = std::filesystem; // 使用命名空间 fs 来方便调用
using namespace std;
cv::Mat overlayResultOnInput(const cv::Mat& cv_input, const cv::Mat& total_result, double alpha = 0.5, int colormap = cv::COLORMAP_JET) {
// 1. 将 total_result 转换为伪彩色图像
cv::Mat total_result_color;
cv::applyColorMap(total_result, total_result_color, colormap); // 使用 JET 色图
// 2. 确保 cv_input 是三通道图像(如果是灰度图像,则转换为 BGR
cv::Mat cv_input_rgb;
if (cv_input.channels() == 1) {
cv::cvtColor(cv_input, cv_input_rgb, cv::COLOR_GRAY2BGR);
} else {
cv_input_rgb = cv_input.clone(); // 保证不修改原始图像
}
// 3. 设置叠加透明度alpha: 0.0-1.0
double beta = 1.0 - alpha;
// 4. 使用加权和将 total_result_color 叠加到 cv_input_rgb 上
cv::Mat overlay;
cv::addWeighted(cv_input_rgb, alpha, total_result_color, beta, 0.0, overlay);
// 5. 返回叠加后的图像
return overlay;
}
int main() {
// 初始化 MIL 应用
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL) ;
// 读取图片
MIL_ID MilImage=M_NULL, MilHighSatResult = M_NULL, MilTemplateMatchingResult = M_NULL;
MbufRestore(IMAGE_PATH, MilSystem, &MilImage);
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
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);
#if run_templating
TemplateMatcher matcher(MilSystem, MilDisplay, params);
matcher.LoadConfig("..\\config\\template_config.txt");
TemplateMatcher matcher(MilSystem, MilDisplay, params);
matcher.LoadConfig("..\\config\\template_config.txt");
#endif
#if run_deep_learning
@ -47,75 +74,81 @@ int main() {
timer1.printElapsedTime("Load config and templates and models");
cout << "Sequence running start:" << endl;
for(int i = 0; i < 10; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cout << i << endl;
timer1.restart();
timer2.restart();
cv::Mat deep_result, high_sat_result, template_result, total_result;
// 艳丽色彩检测
// 遍历文件夹中的所有图片文件
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);
MIL_ID MilHighSatUint8 = convert_to_uint8(MilHighSatResult);
MdispSelect(MilDisplay, MilHighSatUint8);
high_sat_result = mil2mat(MilHighSatResult);
timer1.printElapsedTime("High Sat finished");
high_sat_detect(MilImage, MilHighSatResult, params);
MIL_ID MilHighSatUint8 = convert_to_uint8(MilHighSatResult);
// MdispSelect(MilDisplay, MilHighSatUint8);
high_sat_result = mil2mat(MilHighSatResult);
timer1.printElapsedTime("High Sat finished");
#else
high_sat_result = cv::Mat::zeros(1024,4096, CV_8UC1);
high_sat_result = cv::Mat::zeros(1024, 4096, CV_8UC1);
#endif
#if run_templating
// 模板匹配检测
matcher.predict(MilImage, MilTemplateMatchingResult, params);
template_result = mil2mat(MilTemplateMatchingResult);
timer1.printElapsedTime("Template Matching finished");
// 模板匹配检测
matcher.predict(MilImage, MilTemplateMatchingResult, params);
template_result = mil2mat(MilTemplateMatchingResult);
timer1.printElapsedTime("Template Matching finished");
#else
template_result= cv::Mat::zeros(1024,4096, CV_8UC1);
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);
// 640x640 的全零矩阵
// 深度学习检测
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);
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;
}
timer1.printElapsedTime("Deep Learning finished");
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);
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;
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);
}
timer2.printElapsedTime("Prediction finished Total");
cv::imwrite("./runs/deep_result"+std::to_string(i)+".png", deep_result);
cv::imwrite("./runs/high_sat_result"+std::to_string(i)+".png", high_sat_result);
cv::imwrite("./runs/template_result"+std::to_string(i)+".png", template_result);
cv::imwrite("./runs/total_result"+std::to_string(i)+".png", total_result);
std::vector<cv::Mat> images = {deep_result, high_sat_result, template_result, total_result};
displayCombinedResults(images, "Combined Results");
}
// 释放资源
MbufFree(MilImage);
MbufFree(MilHighSatResult);
MbufFree(MilTemplateMatchingResult);
#if run_templating
matcher.~TemplateMatcher();
#endif
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
std::cout << "所有模块检测已完成!按 <Enter> 退出。" << std::endl;
getchar();
return 0;
}