From f83d435ddcfcaddcaaff0e402b0be83523143a58 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Tue, 26 Nov 2024 11:09:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80template=E2=80=94=E2=80=94mat?= =?UTF-8?q?ching=E7=9A=84=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=B7=BB=E5=8A=A0mask?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E5=87=BD=E6=95=B0=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?src=E4=B8=AD=E6=96=B0=E5=BB=BACVDL=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- opencv_onnx.cpp | 40 ++++++++++++++++++++++++++++---- src/CMakeLists.txt | 1 + src/CVDL/OnnxRunner.cpp | 5 ++++ src/CVDL/OnnxRunner.h | 16 +++++++++++++ src/Matrox/mask.cpp | 25 +------------------- src/Matrox/mask.h | 16 +++++++++++++ src/Matrox/template_matching.cpp | 31 ++++++++++++++++++------- src/Matrox/template_matching.h | 8 +++---- src/Matrox/utils.cpp | 2 ++ tests/CMakeLists.txt | 10 +++++++- tests/test_mask.cpp | 30 ++++++++++++++++++++++++ tests/test_template_matching.cpp | 6 ++--- 12 files changed, 145 insertions(+), 45 deletions(-) create mode 100644 src/CVDL/OnnxRunner.cpp create mode 100644 src/CVDL/OnnxRunner.h create mode 100644 src/Matrox/mask.h create mode 100644 tests/test_mask.cpp diff --git a/opencv_onnx.cpp b/opencv_onnx.cpp index d82fe57..fd7769c 100644 --- a/opencv_onnx.cpp +++ b/opencv_onnx.cpp @@ -112,7 +112,7 @@ int main() { timer1.printElapsedTime("Time to preprocessing"); timer1.restart(); - for(int j = 0; j <30; j++) { + for(int j = 0; j <1; j++) { // 推理模型 cv::Mat output = net.forward(); @@ -153,9 +153,6 @@ int main() { } } - - - // 非极大值抑制 std::vector indices; std::vector boxes; @@ -192,6 +189,41 @@ int main() { drawDetections(image, finalDetections); timer1.printElapsedTime("Time to run inference"); } + int depth = inputImage.depth(); // 图像数据类型 + int channels = inputImage.channels(); // 通道数 + + // 判断图像深度和通道数,打印类型 + std::string depthStr; + switch (depth) { + case CV_8U: + depthStr = "8-bit unsigned integer"; + break; + case CV_8S: + depthStr = "8-bit signed integer"; + break; + case CV_16U: + depthStr = "16-bit unsigned integer"; + break; + case CV_16S: + depthStr = "16-bit signed integer"; + break; + case CV_32S: + depthStr = "32-bit signed integer"; + break; + case CV_32F: + depthStr = "32-bit floating point"; + break; + case CV_64F: + depthStr = "64-bit floating point"; + break; + default: + depthStr = "Unknown depth"; + break; + } + + std::cout << "Image Depth: " << depthStr << std::endl; + std::cout << "Number of Channels: " << channels << std::endl; + cv::imshow("Detections", inputImage); cv::waitKey(0); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8859a18..1d252e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(Matrox Matrox/color_range.cpp Matrox/utils.cpp Matrox/template_matching.cpp + Matrox/mask.cpp ) # 头文件路径 diff --git a/src/CVDL/OnnxRunner.cpp b/src/CVDL/OnnxRunner.cpp new file mode 100644 index 0000000..fa89f0f --- /dev/null +++ b/src/CVDL/OnnxRunner.cpp @@ -0,0 +1,5 @@ +// +// Created by zjc on 24-11-26. +// + +#include "OnnxRunner.h" diff --git a/src/CVDL/OnnxRunner.h b/src/CVDL/OnnxRunner.h new file mode 100644 index 0000000..57b2534 --- /dev/null +++ b/src/CVDL/OnnxRunner.h @@ -0,0 +1,16 @@ +// +// Created by zjc on 24-11-26. +// + +#ifndef ONNXRUNNER_H +#define ONNXRUNNER_H + + + +class OnnxRunner { + +}; + + + +#endif //ONNXRUNNER_H diff --git a/src/Matrox/mask.cpp b/src/Matrox/mask.cpp index cf0ae1b..e564bfa 100644 --- a/src/Matrox/mask.cpp +++ b/src/Matrox/mask.cpp @@ -1,7 +1,5 @@ -#include -#include -#include +#include "mask.h" // 读取二值化的单通道一位图片并生成掩膜 std::vector> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold = 10) { // 读取图像 @@ -52,25 +50,4 @@ std::vector> generateMaskFromImage(const std::string& imagePat return mask; } -int main() { - // 指定图像路径 - std::string imagePath = "C:\\Users\\zjc\\Desktop\\diguandai.png"; - // 设置分块数量和白色像素点阈值 - int widthBlocks = 24; - int heightBlocks = 24; - int threshold = 20; - - // 生成掩膜 - std::vector> mask = generateMaskFromImage(imagePath, widthBlocks, heightBlocks, threshold); - - // 打印掩膜结果 - for (int i = 0; i < heightBlocks; ++i) { - for (int j = 0; j < widthBlocks; ++j) { - std::cout << (mask[i][j] ? "1 " : "0 "); - } - std::cout << std::endl; - } - - return 0; -} diff --git a/src/Matrox/mask.h b/src/Matrox/mask.h new file mode 100644 index 0000000..fee2b3e --- /dev/null +++ b/src/Matrox/mask.h @@ -0,0 +1,16 @@ +// +// Created by zjc on 24-11-26. +// + + +#ifndef MASK_H +#define MASK_H + +#include +#include +#include + +std::vector> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold); + + +#endif //MASK_H diff --git a/src/Matrox/template_matching.cpp b/src/Matrox/template_matching.cpp index b39bff9..bf6132b 100644 --- a/src/Matrox/template_matching.cpp +++ b/src/Matrox/template_matching.cpp @@ -237,7 +237,7 @@ void TemplateMatcher::loadConfig(const std::string& filename, file.close(); } -void TemplateMatcher::LoadTemplate(TemplateMatcher& matcher, std::map& params) +void TemplateMatcher::LoadTemplate(std::map& params) { std::vector template_paths; std::vector offsetX, offsetY, sizeX, sizeY; @@ -245,26 +245,39 @@ void TemplateMatcher::LoadTemplate(TemplateMatcher& matcher, std::maploadTemplates(template_paths, offsetX, offsetY, sizeX, sizeY, drawColor); } -void TemplateMatcher::FindTemplates( const MIL_ID& inputImage, MIL_ID& outputImage,TemplateMatcher& matcher) +void TemplateMatcher::FindTemplates( const MIL_ID& inputImage, MIL_ID& outputImage,const std::map ¶ms) { // Perform template matching - matcher.findModels(inputImage,outputImage); + this -> findModels(inputImage,outputImage); // Notify user that matching is complete cout << "Template matching completed.\n"; } -//TODO: 1加入加载多个模板的功能 已 + 加入配置文件 已 -//TODO: 5制作标准结构的函数,例如:matcher.findModels(MIL_ID inputImage, MIL_ID output_image, map); -////未实现,因为加载和寻找分开后,要对加载和寻找函数传入类成员,无法统一,其余可用到的参数统一,加一个类成员即可。 -//TODO: 6完善相应部分的手册 已 + +// TODO: Opencv ONNX runner, +// 1. 构建相应的模型加载和模型运行函数 +// 2. 在src里头添加另一个cvdl库,专用于视觉深度学习 +// 3. 添加一个类OnnxRunner + +// TODO: 完善config文件,确保能够读取mask转换的相关参数 + +// TODO: Opencv和matrox图像的转换函数,添加到Matrox/utils.cpp + +// TODO:构建统一的图像检测器类,可以一键加载,一键开启多进程快速预测 + +// TODO:计算统一预测框架的预测时间 + +// TODO: 完善模板和参数,添加陈棉模块,陈棉模块可通过配置进行启用和关闭。 + +//TODO: 完善相应部分的手册 已 diff --git a/src/Matrox/template_matching.h b/src/Matrox/template_matching.h index 83a6dc5..00f8e88 100644 --- a/src/Matrox/template_matching.h +++ b/src/Matrox/template_matching.h @@ -47,13 +47,13 @@ public: const std::vector& drawColor); // Search for models in the input image -void findModels(const MIL_ID& inputImage,MIL_ID& outputImage); + void findModels(const MIL_ID& inputImage,MIL_ID& outputImage); - void LoadTemplate(TemplateMatcher &matcher, std::map ¶ms); + void LoadTemplate(std::map ¶ms); - void FindTemplates(const MIL_ID &inputImage, MIL_ID &outputImage, TemplateMatcher &matcher); + void FindTemplates(const MIL_ID &inputImage, MIL_ID &outputImage,const std::map ¶ms); -void loadConfig(const std::string& filename, + void loadConfig(const std::string& filename, std::vector& template_paths, std::vector& offsetX, std::vector& offsetY, diff --git a/src/Matrox/utils.cpp b/src/Matrox/utils.cpp index 31b3f0e..fdde47a 100644 --- a/src/Matrox/utils.cpp +++ b/src/Matrox/utils.cpp @@ -8,6 +8,7 @@ #include #include #include +#include using namespace std; @@ -120,3 +121,4 @@ void read_params_from_file(const std::string& filename, std::map> mask = generateMaskFromImage(imagePath, widthBlocks, heightBlocks, threshold); + + // 打印掩膜结果 + for (int i = 0; i < heightBlocks; ++i) { + for (int j = 0; j < widthBlocks; ++j) + { + std::cout << (mask[i][j] ? "1 " : "0 "); + } + std::cout << std::endl; + } + + return 0; +} \ No newline at end of file diff --git a/tests/test_template_matching.cpp b/tests/test_template_matching.cpp index 4ab409e..fb2e568 100644 --- a/tests/test_template_matching.cpp +++ b/tests/test_template_matching.cpp @@ -49,9 +49,9 @@ int main() { M_IMAGE + M_PROC, &detection_resize); MimResize(detection_result,detection_resize,0.5,0.5,M_DEFAULT); - matcher.LoadTemplate(matcher,params); - matcher.FindTemplates(detection_resize,output_Image,matcher); - //最后的释放问题应该出在寻找模板里面 + matcher.LoadTemplate(params); + matcher.FindTemplates(detection_resize,output_Image,params); + }); MbufSave(SAVE_PATH, detection_result); // Display result