mirror of
https://github.com/Karllzy/cotton_color.git
synced 2025-11-09 03:03:53 +00:00
修复了图像损毁bug
This commit is contained in:
parent
f9261d2fbb
commit
55b20ed64d
@ -40,16 +40,16 @@ void lab_process_raw(const MIL_ID& inputImage, MIL_ID& outputImageLab, const std
|
||||
MbufChildColor(MilLabImage, 2, &MilBChannel);
|
||||
|
||||
// Allocate output image as 1-bit image
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &outputImageLab);
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &outputImageLab);
|
||||
MbufClear(outputImageLab, 0); // Initialize to 0
|
||||
|
||||
// Pre-allocate binary buffers as 1-bit images
|
||||
MIL_ID MilBinaryL = M_NULL, MilBinaryA = M_NULL, MilBinaryB = M_NULL, MilResultLab = M_NULL;
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &MilBinaryL);
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &MilBinaryA);
|
||||
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);
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &MilBinaryL);
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &MilBinaryA);
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &MilBinaryB);
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &MilResultLab);
|
||||
MbufAlloc2d(MilSystem, SizeX, SizeY, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &lab_result);
|
||||
|
||||
|
||||
// Iterate over colors
|
||||
@ -155,13 +155,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), 1 + M_UNSIGNED,
|
||||
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + 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), 1 + M_UNSIGNED,
|
||||
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + 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), 1 + M_UNSIGNED,
|
||||
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED,
|
||||
M_IMAGE + M_PROC + M_DISP, &outputImageHSV);
|
||||
|
||||
// 对 S 通道进行阈值分割
|
||||
@ -188,7 +188,7 @@ void high_sat_detect(const MIL_ID& inputImage, MIL_ID& outputImage, const std::m
|
||||
lab_process(inputImage, output_lab, params);
|
||||
|
||||
MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL),
|
||||
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED,
|
||||
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED,
|
||||
M_IMAGE + M_PROC, &outputImage);
|
||||
|
||||
// 合并 Lab 和 HSV 的结果(取“或”运算)
|
||||
|
||||
@ -17,8 +17,8 @@ void pre_process(const MIL_ID& inputImage, MIL_ID& outputImageSuspect, const map
|
||||
MimArith(outputImageSuspect, M_NULL, outputImageSuspect, M_NOT);
|
||||
}
|
||||
|
||||
TemplateMatcher::TemplateMatcher(MIL_ID& system, MIL_ID& display, std::map<std::string, int>& param)
|
||||
: MilSystem(system), MilDisplay(display), isInitialized(false), param(param)
|
||||
TemplateMatcher::TemplateMatcher(std::map<std::string, int>& param)
|
||||
:isInitialized(false), param(param)
|
||||
{
|
||||
}
|
||||
|
||||
@ -63,7 +63,8 @@ void TemplateMatcher::loadTemplates(const std::vector<std::string>& template_pat
|
||||
MIL_ID template_temporary;
|
||||
MgraClear(M_DEFAULT, GraphicList);
|
||||
MbufRestore(convert_to_wstring(ModelImgPaths[i]).c_str(), MilSystem, &template_temporary);
|
||||
MIL_ID template_temporary_uint8 = convert_to_uint8(template_temporary);
|
||||
MIL_ID template_temporary_uint8;
|
||||
convert_to_uint8(template_temporary, template_temporary_uint8);
|
||||
if (this->param["isdisplay"] == 1)
|
||||
{
|
||||
MdispSelect(MilDisplay, template_temporary_uint8);
|
||||
@ -115,9 +116,8 @@ void TemplateMatcher::findModels(const MIL_ID& inputImage,MIL_ID& outputImage)
|
||||
std::cerr << "Templates are not loaded. Please load templates before searching.\n";
|
||||
return;
|
||||
}
|
||||
MIL_ID input_image_uint8 = convert_to_uint8(inputImage);
|
||||
if(this->param["isdisplay"] == 1) {
|
||||
MdispSelect(MilDisplay, input_image_uint8);
|
||||
MdispSelect(MilDisplay, inputImage);
|
||||
}
|
||||
|
||||
// Clear previous annotations
|
||||
@ -126,7 +126,7 @@ void TemplateMatcher::findModels(const MIL_ID& inputImage,MIL_ID& outputImage)
|
||||
// Find models
|
||||
MIL_DOUBLE Time = 0.0;
|
||||
MappTimer(M_DEFAULT, M_TIMER_RESET + M_SYNCHRONOUS, M_NULL);
|
||||
MmodFind(MilSearchContext, input_image_uint8, MilResult);
|
||||
MmodFind(MilSearchContext, inputImage, MilResult);
|
||||
MappTimer(M_DEFAULT, M_TIMER_READ + M_SYNCHRONOUS, &Time);
|
||||
|
||||
// Get results
|
||||
@ -147,7 +147,7 @@ void TemplateMatcher::findModels(const MIL_ID& inputImage,MIL_ID& outputImage)
|
||||
|
||||
// Create a binary image buffer
|
||||
MbufAlloc2d(MilSystem, MbufInquire(inputImage, M_SIZE_X, M_NULL),
|
||||
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED,
|
||||
MbufInquire(inputImage, M_SIZE_Y, M_NULL), 8 + M_UNSIGNED,
|
||||
M_IMAGE + M_PROC, &outputImage);
|
||||
// Initialize the binary image to black
|
||||
MbufClear(outputImage, 0);
|
||||
@ -170,10 +170,9 @@ void TemplateMatcher::findModels(const MIL_ID& inputImage,MIL_ID& outputImage)
|
||||
} else {
|
||||
std::cout << "No models found.\n";
|
||||
}
|
||||
MbufFree(input_image_uint8);
|
||||
}
|
||||
|
||||
std::vector<std::string> splitString(const std::string& str, char delimiter) {
|
||||
std::vector<std::string> splitString(const std::string& str, const char delimiter) {
|
||||
std::vector<std::string> tokens;
|
||||
std::stringstream ss(str);
|
||||
std::string item;
|
||||
@ -246,21 +245,32 @@ void TemplateMatcher::FindTemplates( const MIL_ID& inputImage, MIL_ID& outputIma
|
||||
this -> findModels(inputImage,outputImage);
|
||||
}
|
||||
|
||||
void TemplateMatcher::predict(const MIL_ID &inputImage, MIL_ID &outputImage, const std::map<std::string, int> ¶ms) {
|
||||
MIL_ID detection_result, detection_resize, output_image_unresize;
|
||||
void TemplateMatcher::predict(const MIL_ID &inputImage, MIL_ID &outputImage, const std::map<std::string, int> ¶ms,
|
||||
const bool do_resize=false) {
|
||||
MIL_ID detection_result;
|
||||
pre_process(inputImage, detection_result, params);
|
||||
MbufAlloc2d(MilSystem, MbufInquire(detection_result, M_SIZE_X, M_NULL)/2,
|
||||
if (do_resize) {
|
||||
MIL_ID detection_resize, output_image_unresize;
|
||||
MbufAlloc2d(MilSystem, MbufInquire(detection_result, M_SIZE_X, M_NULL)/2,
|
||||
MbufInquire(detection_result, M_SIZE_Y, M_NULL)/2, 1 + M_UNSIGNED,
|
||||
M_IMAGE + M_PROC, &detection_resize);
|
||||
MimResize(detection_result,detection_resize,0.5,0.5,M_DEFAULT);
|
||||
this->FindTemplates(detection_resize,output_image_unresize, params);
|
||||
MbufAlloc2d(MilSystem, MbufInquire(detection_result, M_SIZE_X, M_NULL),
|
||||
MbufInquire(detection_result, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED,
|
||||
M_IMAGE + M_PROC, &outputImage);
|
||||
MimResize(output_image_unresize,outputImage,2,2,M_DEFAULT);
|
||||
MbufFree(detection_result);
|
||||
MbufFree(detection_resize);
|
||||
MbufFree(output_image_unresize);
|
||||
MimResize(detection_result,detection_resize,0.5,0.5,M_DEFAULT);
|
||||
this->FindTemplates(detection_resize,output_image_unresize, params);
|
||||
MbufAlloc2d(MilSystem, MbufInquire(detection_result, M_SIZE_X, M_NULL),
|
||||
MbufInquire(detection_result, M_SIZE_Y, M_NULL), 1 + M_UNSIGNED,
|
||||
M_IMAGE + M_PROC, &outputImage);
|
||||
MimResize(output_image_unresize,outputImage,2,2,M_DEFAULT);
|
||||
MbufFree(detection_result);
|
||||
MbufFree(detection_resize);
|
||||
MbufFree(output_image_unresize);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->FindTemplates(detection_result,outputImage, params);
|
||||
MbufFree(detection_result);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -15,12 +15,9 @@ extern std::vector<MIL_DOUBLE> drawColor;
|
||||
|
||||
|
||||
class TemplateMatcher {
|
||||
private:
|
||||
MIL_ID MilSystem;
|
||||
MIL_ID MilDisplay;
|
||||
MIL_ID MilSearchContext;
|
||||
MIL_ID MilResult;
|
||||
MIL_ID GraphicList;
|
||||
MIL_ID MilSearchContext{};
|
||||
MIL_ID MilResult{};
|
||||
MIL_ID GraphicList{};
|
||||
|
||||
std::vector<std::string> ModelImgPaths;
|
||||
std::vector<MIL_INT> ModelsOffsetX;
|
||||
@ -35,7 +32,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
TemplateMatcher(MIL_ID& system, MIL_ID& display, std::map<std::string, int>& param);
|
||||
explicit TemplateMatcher(std::map<std::string, int>& param);
|
||||
|
||||
void LoadConfig(const std::string& config_path);
|
||||
|
||||
@ -49,10 +46,9 @@ public:
|
||||
void findModels(const MIL_ID& inputImage,MIL_ID& outputImage);
|
||||
|
||||
|
||||
|
||||
void FindTemplates(const MIL_ID &inputImage, MIL_ID &outputImage,const std::map<std::string, int> ¶ms);
|
||||
|
||||
void predict(const MIL_ID& inputImage, MIL_ID& outputImage, const std::map<std::string, int> ¶ms);
|
||||
void predict(const MIL_ID &inputImage, MIL_ID &outputImage, const std::map<std::string, int> ¶ms, bool do_resize);
|
||||
|
||||
static void loadConfig(const std::string& filename,
|
||||
std::vector<std::string>& template_paths,
|
||||
|
||||
@ -57,9 +57,7 @@ vector<int> opencvLabToPsLab(const vector<int>& lab_cv) {
|
||||
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);
|
||||
void convert_to_uint8(const MIL_ID& input_img, MIL_ID& output_img) {
|
||||
MIL_INT size_x = MbufInquire(input_img, M_SIZE_X, M_NULL);
|
||||
MIL_INT size_y = MbufInquire(input_img, M_SIZE_Y, M_NULL);
|
||||
MIL_INT channel_num = MbufInquire(input_img, M_SIZE_BAND, M_NULL);
|
||||
@ -74,7 +72,6 @@ MIL_ID convert_to_uint8(MIL_ID input_img) {
|
||||
} else {
|
||||
cout << "Unsupported channel number!" << endl;
|
||||
}
|
||||
return output_img;
|
||||
}
|
||||
|
||||
|
||||
@ -182,7 +179,7 @@ Mat mil2mat(const MIL_ID mil_img) {
|
||||
Mat grayImage(height, width, CV_8UC1);
|
||||
if (bitDepth == 1) {
|
||||
MIL_ID temp_img;
|
||||
temp_img = convert_to_uint8(mil_img);
|
||||
convert_to_uint8(mil_img, temp_img);
|
||||
MbufGet(temp_img, grayImage.data);
|
||||
MbufFree(temp_img);
|
||||
} else {
|
||||
|
||||
@ -35,12 +35,13 @@ 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);
|
||||
void convert_to_uint8(const MIL_ID& input_img, MIL_ID& output_img);
|
||||
|
||||
std::wstring convert_to_wstring(const std::string& str);
|
||||
|
||||
void read_params_from_file(const std::string& filename, std::map<std::string, int>& params) ;
|
||||
|
||||
cv::Mat mil2mat(MIL_ID mil_img);
|
||||
void processImage(cv::Mat& img);
|
||||
|
||||
std::unordered_map<std::string, int> loadConfig(const std::string& filename);
|
||||
|
||||
|
||||
@ -17,7 +17,7 @@ MIL_ID MilApplication = M_NULL, MilSystem = M_NULL, MilDisplay = M_NULL;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
||||
void LoadImagesFromFolder(const std::string& folderPath, MIL_ID MilSystem)
|
||||
void LoadImagesFromFolder(const std::string& folderPath)
|
||||
{
|
||||
// 遍历文件夹中的所有文件
|
||||
for (const auto& entry : fs::directory_iterator(folderPath))
|
||||
@ -46,10 +46,10 @@ void LoadImagesFromFolder(const std::string& folderPath, MIL_ID MilSystem)
|
||||
MIL_ID detection_resize = M_NULL;
|
||||
cv::Mat template_result;
|
||||
MIL_ID output_Image= M_NULL;
|
||||
TemplateMatcher matcher(MilSystem, MilDisplay, params);
|
||||
TemplateMatcher matcher(params);
|
||||
matcher.LoadConfig("C:\\Users\\zjc\\Desktop\\config\\template_config.txt");
|
||||
// Measure execution time
|
||||
for (int i = 0; i <1; i++) {
|
||||
for (int i = 0; i <12; i++) {
|
||||
measure_execution_time([&]()
|
||||
{
|
||||
pre_process(MilImage, detection_result, params);
|
||||
@ -88,7 +88,7 @@ int main() {
|
||||
|
||||
std::string folderPath ="C:/Users/zjc/Desktop/cotton_image_new"; // 请替换为你自己的文件夹路径
|
||||
// 加载文件夹中的所有图片
|
||||
LoadImagesFromFolder(folderPath, MilSystem);
|
||||
LoadImagesFromFolder(folderPath);
|
||||
|
||||
// Display result
|
||||
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
#define IMAGE_DIR MIL_TEXT(".\\test_imgs\\cotton_image_new") // 文件夹路径
|
||||
|
||||
#define run_high_sat true;
|
||||
#define run_templating true;
|
||||
#define run_templating false;
|
||||
#define run_deep_learning true;
|
||||
|
||||
MIL_ID MilApplication, MilSystem, MilDisplay;
|
||||
@ -36,7 +36,7 @@ cv::Mat overlayResultOnInput(const cv::Mat& cv_input, const cv::Mat& total_resul
|
||||
// 2. 确保 cv_input 是三通道图像(如果是灰度图像,则转换为 BGR)
|
||||
cv::Mat cv_input_rgb;
|
||||
if (cv_input.channels() == 1) {
|
||||
cv::cvtColor(cv_input, cv_input_rgb, cv::COLOR_GRAY2BGR);
|
||||
cv::cvtColor(cv_input, cv_input_rgb, cv::COLORMAP_WINTER);
|
||||
} else {
|
||||
cv_input_rgb = cv_input.clone(); // 保证不修改原始图像
|
||||
}
|
||||
@ -62,7 +62,7 @@ int main() {
|
||||
read_params_from_file("..\\config\\template_color_config.txt", params);
|
||||
|
||||
#if run_templating
|
||||
TemplateMatcher matcher(MilSystem, MilDisplay, params);
|
||||
TemplateMatcher matcher(params);
|
||||
matcher.LoadConfig("..\\config\\template_config.txt");
|
||||
#endif
|
||||
|
||||
@ -92,8 +92,6 @@ int main() {
|
||||
// 艳丽色彩检测
|
||||
#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");
|
||||
#else
|
||||
@ -102,7 +100,7 @@ int main() {
|
||||
|
||||
#if run_templating
|
||||
// 模板匹配检测
|
||||
matcher.predict(MilImage, MilTemplateMatchingResult, params);
|
||||
matcher.predict(MilImage, MilTemplateMatchingResult, params, false);
|
||||
template_result = mil2mat(MilTemplateMatchingResult);
|
||||
timer1.printElapsedTime("Template Matching finished");
|
||||
#else
|
||||
@ -145,10 +143,6 @@ int main() {
|
||||
MbufFree(MilTemplateMatchingResult);
|
||||
}
|
||||
}
|
||||
|
||||
#if run_templating
|
||||
matcher.~TemplateMatcher();
|
||||
#endif
|
||||
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user