增加两侧吹气,修改了延时时间参数

This commit is contained in:
zhenye 2024-12-17 23:22:36 +08:00
parent d36012cc9c
commit dd2eb4df74
6 changed files with 4868 additions and 134 deletions

View File

@ -5,8 +5,8 @@
// Debug Options // Debug Options
#define GlobalDebug 1 // 全局是否允许打印Debug信息打印会拖慢处理时间 #define GlobalDebug 1 // 全局是否允许打印Debug信息打印会拖慢处理时间
#define DebugDetection 0 // 注意开启这个编译选项会导致图片存储, 处理时间会很慢 #define DebugDetection 0 // 注意开启这个编译选项会导致图片存储, 处理时间会很慢
#define DebugDetectionTime 1 // 是否打印处理时间 #define DebugDetectionTime 0 // 是否打印处理时间
#define DebugLowerMacCOM 1 // 是否打印和下位机通讯的相关信息 #define DebugLowerMacCOM 0 // 是否打印和下位机通讯的相关信息
camera::camera() {} camera::camera() {}
@ -49,6 +49,7 @@ std::map<std::string, int> params;
int widthBlocks = 20; // 输出的喷阀通道数 int widthBlocks = 20; // 输出的喷阀通道数
int heightBlocks = 512; // 输出的Mask高度 int heightBlocks = 512; // 输出的Mask高度
int sizeThreshold = 20; // 转化为喷阀的每块要求像素个数 int sizeThreshold = 20; // 转化为喷阀的每块要求像素个数
int expansionRaidus = 1; // 获取mask后进行左右位置的扩展
int padLeft = 1; // 左侧在结果Mask上补0 int padLeft = 1; // 左侧在结果Mask上补0
int padRight = 1; // 右侧在结果Mask上补0 int padRight = 1; // 右侧在结果Mask上补0
int rowRange = 1; // 结果维持行数至少为1 int rowRange = 1; // 结果维持行数至少为1
@ -66,6 +67,9 @@ extern int file_delay;
extern int file_encoder; extern int file_encoder;
extern int file_valve; extern int file_valve;
Timer CallBackTimer0;
Timer CallBackTimer1;
// ONNXRunner runner; // ONNXRunner runner;
bool iniCamera() bool iniCamera()
{ {
@ -133,6 +137,10 @@ bool iniCamera()
MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr) MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
{ {
// CallBackTimer0.printElapsedTime("Call Back 0 reach");
// CallBackTimer0.restart();
#if(GlobalDebug && DebugDetectionTime) #if(GlobalDebug && DebugDetectionTime)
Timer timer_detection_time; Timer timer_detection_time;
timer_detection_time.restart(); timer_detection_time.restart();
@ -154,23 +162,123 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
qDebug()<<"回调1"; qDebug()<<"回调1";
#endif #endif
//拷贝艳丽色检测图像 //拷贝艳丽色检测图像
MbufCopy(ModifiedBufferId0,MilImage_Color0); // MbufCopy(ModifiedBufferId0,MilImage_Color0);
// // //拷贝onnx检测图像
// // MbufCopy(ModifiedBufferId1,MilImage_Onnx1);
// MIL_UNIQUE_BUF_ID MimResizedestination = MbufAllocColor(MilSystem, 3, 2048, 512, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, M_UNIQUE_ID);
// MbufClear(MimResizedestination, M_COLOR_BLACK);
// MimResize(MilImage_Color0, MimResizedestination, 0.5, 1 , M_DEFAULT);
// MIL_UNIQUE_BUF_ID MimFlipDedtination = MbufClone(MimResizedestination, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_UNIQUE_ID);
// MbufClear(MimFlipDedtination, M_COLOR_BLACK);
// MimFlip(MimResizedestination, MimFlipDedtination, M_FLIP_HORIZONTAL, M_DEFAULT);
// #if(GlobalDebug && DebugDetectionTime)
// Timer timer2;
// #endif
// //艳丽检测mask
// high_sat_detect(MimFlipDedtination, detection_result0, params);
// #if(GlobalDebug && DebugDetectionTime)
// timer2.printElapsedTime("Algorithm Spent: ");
// #endif
// #if(GlobalDebug && DebugDetection)
// MbufSave(SAVE_PATH_flip,MimFlipDedtination);
// // MbufSave(SAVE_PATH_resize,MimResizedestination);
// MbufSave(SAVE_PATH_raw,MilImage_Color0);
// MbufSave(SAVE_PATH_result,detection_result0);
// #endif
// //Onnx检测mask
// // cv::Mat image = mil2mat(MilImage_Onnx1);
// // cv::Mat Img_Onnx;
// // std::vector<Detection> result = runner.predict(image);
// // Img_Onnx = runner.postProcess(result, image);
// // std::vector<std::vector<uint8_t>> mask_Onnx1 = generateMaskFromImage2(Img_Onnx, widthBlocks, heightBlocks, sizeThreshold);
// // timer1.printElapsedTime("onnx sat finished");
// auto [mask_1, newTail] = generateMaskWithTail(detection_result0, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange, skipLeftCols, skipRightCols);
// tail_1 = newTail;
// PadColumns(mask_1,padLeft,padRight,0);
// std::vector<std::vector<uint8_t>> mask_Total = expandArray(mask_1,64);
// // save masks
// #if(GlobalDebug && DebugDetection)
// VectorToImg(mask_1,"C:/Users/Pc/Desktop/img/mask" + std::to_string(FuncCount1) + ".bmp");
// VectorToImg(mask_1,"C:/Users/Pc/Desktop/img/mask_ignored" + std::to_string(FuncCount1) + ".bmp");
// VectorToImg(mask_Total,"C:/Users/Pc/Desktop/img/mask_expended" + std::to_string(FuncCount1) + ".bmp");
// #endif
// bool result_Low = get_valve_data(mask_Total);
// if(!result_Low)
// {
// qWarning()<<"下位机发送失败";
// }
// #if(GlobalDebug && DebugDetectionTime)
// timer_detection_time.printElapsedTime("Time of Processing From Get into CallBack to Sent to Lower Mac");
// #endif
return 0;
}
Timer timer2;
MIL_INT ProcessingFunction1(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
{
// CallBackTimer1.printElapsedTime("Call Back 1 reached");
// CallBackTimer1.restart();
#if(GlobalDebug && DebugDetectionTime)
Timer timer_detection_time;
timer_detection_time.restart();
#endif
// FuncCount1++;
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &ModifiedBufferId1);
if(SaveImg_Flag)
{
//拷贝存图数据
MbufCopy(ModifiedBufferId1,MilImage1);
//Mil保存路径
QString MilImgPath = QString("C:/Users/Pc/Desktop/cotton_double2/Img2/%1.bmp").arg(FuncCount1);
MIL_STRING MilImagePath = MilImgPath.toStdWString();
MbufExport(MilImagePath,M_BMP,MilImage1);
qDebug()<<"回调2成功存储照片:"<<FuncCount1<<"";
FuncCount1++;
}
#if (GlobalDebug)
qDebug()<<"回调2";
#endif
//拷贝艳丽色检测图像
MbufCopy(ModifiedBufferId1,MilImage_Color1);
// //拷贝onnx检测图像 // //拷贝onnx检测图像
// MbufCopy(ModifiedBufferId1,MilImage_Onnx1); // MbufCopy(ModifiedBufferId1,MilImage_Onnx1);
MIL_UNIQUE_BUF_ID MimResizedestination = MbufAllocColor(MilSystem, 3, 2048, 512, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, M_UNIQUE_ID); MIL_UNIQUE_BUF_ID MimResizedestination = MbufAllocColor(MilSystem, 3, 2048, 512, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, M_UNIQUE_ID);
MbufClear(MimResizedestination, M_COLOR_BLACK); MbufClear(MimResizedestination, M_COLOR_BLACK);
MimResize(MilImage_Color0, MimResizedestination, 0.5, 1 , M_DEFAULT); MimResize(MilImage_Color1, MimResizedestination, 0.5, 1 , M_DEFAULT);
MIL_UNIQUE_BUF_ID MimFlipDedtination = MbufClone(MimResizedestination, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_UNIQUE_ID); // MIL_UNIQUE_BUF_ID MimFlipDedtination = MbufClone(MimResizedestination, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_UNIQUE_ID);
MbufClear(MimFlipDedtination, M_COLOR_BLACK); // MbufClear(MimFlipDedtination, M_COLOR_BLACK);
MimFlip(MimResizedestination, MimFlipDedtination, M_FLIP_HORIZONTAL, M_DEFAULT); // MimFlip(MimResizedestination, MimFlipDedtination, M_FLIP_HORIZONTAL, M_DEFAULT);
#if(GlobalDebug && DebugDetectionTime) #if(GlobalDebug && DebugDetectionTime)
Timer timer2; Timer timer2;
#endif #endif
//艳丽检测mask //艳丽检测mask
high_sat_detect(MimFlipDedtination, detection_result0, params); high_sat_detect(MimResizedestination, detection_result1, params);
#if(GlobalDebug && DebugDetectionTime) #if(GlobalDebug && DebugDetectionTime)
timer2.printElapsedTime("Algorithm Spent: "); timer2.printElapsedTime("Algorithm Spent: ");
@ -192,12 +300,13 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
// std::vector<std::vector<uint8_t>> mask_Onnx1 = generateMaskFromImage2(Img_Onnx, widthBlocks, heightBlocks, sizeThreshold); // std::vector<std::vector<uint8_t>> mask_Onnx1 = generateMaskFromImage2(Img_Onnx, widthBlocks, heightBlocks, sizeThreshold);
// timer1.printElapsedTime("onnx sat finished"); // timer1.printElapsedTime("onnx sat finished");
auto [mask_1, newTail] = generateMaskWithTail(detection_result0, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange, skipLeftCols, skipRightCols); auto [mask_1, newTail] = generateMaskWithTail(detection_result1, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange, skipLeftCols, skipRightCols);
tail_1 = newTail; tail_1 = newTail;
auto mask_expaned = expandMaskHorizontally(mask_1, expansionRaidus);
PadColumns(mask_1,padLeft,padRight,0); PadColumns(mask_expaned,padLeft,padRight,0);
std::vector<std::vector<uint8_t>> mask_Total = expandArray(mask_1,64); std::vector<std::vector<uint8_t>> mask_Total = expandArray(mask_expaned,64);
// save masks // save masks
#if(GlobalDebug && DebugDetection) #if(GlobalDebug && DebugDetection)
@ -213,120 +322,9 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
#if(GlobalDebug && DebugDetectionTime) #if(GlobalDebug && DebugDetectionTime)
timer_detection_time.printElapsedTime("Time of Processing From Get into CallBack to Sent to Lower Mac"); timer_detection_time.printElapsedTime("Time of Processing From Get into Ca"
"..0llBack to Sent to Lower Mac");
#endif #endif
return 0;
}
Timer timer2;
MIL_INT ProcessingFunction1(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
{
FuncCount1++;
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &ModifiedBufferId1);
if(SaveImg_Flag)
{
//拷贝存图数据
MbufCopy(ModifiedBufferId1,MilImage1);
//Mil保存路径
QString MilImgPath = QString("C:/Users/Pc/Desktop/cotton_double2/Img2/%1.bmp").arg(FuncCount1);
MIL_STRING MilImagePath = MilImgPath.toStdWString();
MbufExport(MilImagePath,M_BMP,MilImage1);
qDebug()<<"回调2成功存储照片:"<<FuncCount1<<"";
FuncCount1++;
}
#if (GlobalDebug)
qDebug()<<"回调2";
#endif
// Timer timer1;
// timer1.restart();
//拷贝艳丽色检测图像
MbufCopy(ModifiedBufferId1,MilImage_Color1);
// //拷贝onnx检测图像
// MbufCopy(ModifiedBufferId1,MilImage_Onnx1);
// timer1.printElapsedTime("copy finished");
// timer1.restart();
// MIL_UNIQUE_BUF_ID MimResizedestination = MbufAllocColor(MilSystem, 3, 2048, 512, 8+M_UNSIGNED, M_IMAGE+M_PROC+M_DISP, M_UNIQUE_ID);
// MbufClear(MimResizedestination, M_COLOR_BLACK);
// MimResize(MilImage_Color1, MimResizedestination, 0.5, 0.5, M_DEFAULT);
// //艳丽检测mask
// high_sat_detect(MimResizedestination, detection_result1, params);
// // MbufSave(SAVE_PATH,detection_result1);
// //Onnx检测mask
// // cv::Mat image = mil2mat(MilImage_Onnx1);
// // cv::Mat Img_Onnx;
// // std::vector<Detection> result = runner.predict(image);
// // Img_Onnx = runner.postProcess(result, image);
// // std::vector<std::vector<uint8_t>> mask_Onnx1 = generateMaskFromImage2(Img_Onnx, widthBlocks, heightBlocks, sizeThreshold);
// // timer1.printElapsedTime("onnx sat finished");
// // timer1.restart();
// auto [mask_1, newTail] = generateMaskWithTail(detection_result1, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange);
// tail_1 = newTail;
// // timer1.printElapsedTime("tail finished");
// // timer1.restart();
// // std::vector<std::vector<uint8_t>> mask_Color1 = generateMaskFromImage(detection_result1, widthBlocks, heightBlocks, thresholds);
// std::vector<std::vector<uint8_t>> mask_Total = expandArray(mask_1,64);
// // timer1.printElapsedTime("expand finished");
// // for(int m = 0 ; m < 512 ; m ++)
// // {
// // for(int n = 0; n < 22 ; n++)
// // {
// // if(mask_Color1[m][n] == 1)
// // {
// // for(int k = 0; k < 50; k++)
// // {
// // mask_Total[m + k][n] = 1;
// // }
// // }
// // }
// // }
// // VectorToImg(mask_Total,"C:/Users/admin/Desktop/maskImg/mask" + std::to_string(FuncCount1) + ".bmp");
// bool result_Low = get_valve_data(mask_Total);
// if(!result_Low)
// {
// qDebug()<<"下位机发送失败";
// }
// if(FuncCount1 == 2)
// {
// timer2.restart();
// }
// if(FuncCount1 == 3)
// {
// timer2.printElapsedTime("send interval");
// }
return 0; return 0;
} }
@ -859,10 +857,7 @@ bool iniLowMac()
return 0 ; return 0 ;
} }
// 硬编码参数值
int file_delay = 1530; // 延迟时间(毫秒)
int file_encoder = 12000; // 编码器值
int file_valve = 200; // 阀门通道
// 计算 Y = 100000000 / X // 计算 Y = 100000000 / X
int divide_camera = (file_encoder != 0) ? 100000000 / file_encoder : 0; // 防止除以零的情况 int divide_camera = (file_encoder != 0) ? 100000000 / file_encoder : 0; // 防止除以零的情况
@ -1260,7 +1255,34 @@ void PadColumns(std::vector<std::vector<uint8_t>>& data, int pad_left, int pad_r
} }
std::vector<std::vector<uint8_t>> expandMaskHorizontally(
const std::vector<std::vector<uint8_t>>& mask,
int expansionRadius)
{
std::vector<std::vector<uint8_t>> expanded_mask = mask;
int rows = (int)mask.size();
if (rows == 0) return expanded_mask;
int cols = (int)mask[0].size();
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
if (mask[y][x] == 1)
{
for (int i = 1; i <= expansionRadius; i++)
{
if (x - i >= 0)
expanded_mask[y][x - i] = 1;
if (x + i < cols)
expanded_mask[y][x + i] = 1;
}
}
}
}
return expanded_mask;
}
//onnx_Mask //onnx_Mask

View File

@ -32,7 +32,7 @@ std::vector<std::vector<uint8_t>> expandArray(const std::vector<std::vector<uint
void VectorToImg(const std::vector<std::vector<uint8_t>>& array, const std::string& image_path); void VectorToImg(const std::vector<std::vector<uint8_t>>& array, const std::string& image_path);
cv::Mat mil2mat(MIL_ID mil_img); cv::Mat mil2mat(const MIL_ID mil_img);
void convert_to_uint8(const MIL_ID& input_img, MIL_ID& output_img); void convert_to_uint8(const MIL_ID& input_img, MIL_ID& output_img);
std::vector<std::vector<uint8_t>> generateMask( std::vector<std::vector<uint8_t>> generateMask(
@ -104,6 +104,10 @@ pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> generateMaskWithTail(
int skipRightCols int skipRightCols
); );
std::vector<std::vector<uint8_t>> expandMaskHorizontally(
const std::vector<std::vector<uint8_t>>& mask,
int expansionRadius);
//下位机通讯 //下位机通讯
extern QTcpServer* server_to_lowermachine; extern QTcpServer* server_to_lowermachine;

2356
config/1.dcf Normal file

File diff suppressed because it is too large Load Diff

2352
config/2.dcf Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 15.0.0, 2024-12-14T12:49:17. --> <!-- Written by QtCreator 15.0.0, 2024-12-17T00:13:56. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

View File

@ -16,10 +16,10 @@
using namespace std; using namespace std;
int file_delay; // 硬编码参数值
int file_encoder; int file_delay = 1270; // 延迟时间(毫秒)
int file_valve; int file_encoder = 12000; // 编码器值
int file_valve = 200; // 阀门通道
Widget::Widget(QWidget *parent) Widget::Widget(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, ui(new Ui::Widget) , ui(new Ui::Widget)