统一template——matching的接口,添加mask的测试函数,在src中新建CVDL库

This commit is contained in:
zjc-zjc-123 2024-11-26 11:09:14 +08:00
parent fc50c3b440
commit f83d435ddc
12 changed files with 145 additions and 45 deletions

View File

@ -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<int> indices;
std::vector<cv::Rect> 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);

View File

@ -3,6 +3,7 @@ add_library(Matrox
Matrox/color_range.cpp
Matrox/utils.cpp
Matrox/template_matching.cpp
Matrox/mask.cpp
)
#

5
src/CVDL/OnnxRunner.cpp Normal file
View File

@ -0,0 +1,5 @@
//
// Created by zjc on 24-11-26.
//
#include "OnnxRunner.h"

16
src/CVDL/OnnxRunner.h Normal file
View File

@ -0,0 +1,16 @@
//
// Created by zjc on 24-11-26.
//
#ifndef ONNXRUNNER_H
#define ONNXRUNNER_H
class OnnxRunner {
};
#endif //ONNXRUNNER_H

View File

@ -1,7 +1,5 @@
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
#include "mask.h"
// 读取二值化的单通道一位图片并生成掩膜
std::vector<std::vector<bool>> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold = 10) {
// 读取图像
@ -52,25 +50,4 @@ std::vector<std::vector<bool>> 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<std::vector<bool>> 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;
}

16
src/Matrox/mask.h Normal file
View File

@ -0,0 +1,16 @@
//
// Created by zjc on 24-11-26.
//
#ifndef MASK_H
#define MASK_H
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
std::vector<std::vector<bool>> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold);
#endif //MASK_H

View File

@ -237,7 +237,7 @@ void TemplateMatcher::loadConfig(const std::string& filename,
file.close();
}
void TemplateMatcher::LoadTemplate(TemplateMatcher& matcher, std::map<std::string, int>& params)
void TemplateMatcher::LoadTemplate(std::map<std::string, int>& params)
{
std::vector<std::string> template_paths;
std::vector<MIL_INT> offsetX, offsetY, sizeX, sizeY;
@ -245,26 +245,39 @@ void TemplateMatcher::LoadTemplate(TemplateMatcher& matcher, std::map<std::strin
// 调用 loadConfig 并加载配置
loadConfig("C:\\Users\\zjc\\Desktop\\config\\template_config.txt",
template_paths, offsetX, offsetY, sizeX, sizeY, drawColor);
template_paths, offsetX, offsetY, sizeX, sizeY, drawColor);
// 调用 matcher 的 loadTemplates 方法
matcher.loadTemplates(template_paths, offsetX, offsetY, sizeX, sizeY, drawColor);
this->loadTemplates(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<std::string, int> &params)
{
// 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: 完善相应部分的手册 已

View File

@ -47,13 +47,13 @@ public:
const std::vector<MIL_DOUBLE>& 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<std::string, int> &params);
void LoadTemplate(std::map<std::string, int> &params);
void FindTemplates(const MIL_ID &inputImage, MIL_ID &outputImage, TemplateMatcher &matcher);
void FindTemplates(const MIL_ID &inputImage, MIL_ID &outputImage,const std::map<std::string, int> &params);
void loadConfig(const std::string& filename,
void loadConfig(const std::string& filename,
std::vector<std::string>& template_paths,
std::vector<MIL_INT>& offsetX,
std::vector<MIL_INT>& offsetY,

View File

@ -8,6 +8,7 @@
#include <iostream>
#include <vector>
#include <cmath>
#include <opencv2/core/mat.hpp>
using namespace std;
@ -120,3 +121,4 @@ void read_params_from_file(const std::string& filename, std::map<std::string, in
}
// 图片转换函数输入4096*1024*3的图片输出为(4096 / n_valves) * (1024 / n_merge_vertical) * 1
// Mat Mil2cvImage(MIL_ID &input_image,Mat) {}

View File

@ -13,4 +13,12 @@ add_executable(test_template_matching
)
# Matrox
target_link_libraries(test_template_matching Matrox ${OpenCV_LIBS} ${MIL_LIBS})
target_link_libraries(test_template_matching Matrox ${OpenCV_LIBS} ${MIL_LIBS})
add_executable(test_mask
${CMAKE_CURRENT_SOURCE_DIR}/test_mask.cpp
)
# Matrox
target_link_libraries(test_mask Matrox ${OpenCV_LIBS} ${MIL_LIBS})

30
tests/test_mask.cpp Normal file
View File

@ -0,0 +1,30 @@
//
// Created by zjc on 24-11-26.
//
#include "vector"
#include"iostream"
#include"string"
#include"Matrox/mask.h"
int main() {
// 指定图像路径
std::string imagePath = "C:\\Users\\zjc\\Desktop\\diguandai.png";
// 设置分块数量和白色像素点阈值
int widthBlocks = 24;
int heightBlocks = 24;
int threshold = 20;
// 生成掩膜
std::vector<std::vector<bool>> 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;
}

View File

@ -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