修改了debug的打印方式,修复了resize导致的mask问题。

This commit is contained in:
zhenye 2024-12-16 20:50:44 +08:00
parent bc9a5f9fc7
commit d36012cc9c

View File

@ -3,9 +3,10 @@
#include <QTimer>
// Debug Options
#define GlobalDebug 1
#define DebugDetection 1
#define DebugLowerMacCOM 0
#define GlobalDebug 1 // 全局是否允许打印Debug信息打印会拖慢处理时间
#define DebugDetection 0 // 注意开启这个编译选项会导致图片存储, 处理时间会很慢
#define DebugDetectionTime 1 // 是否打印处理时间
#define DebugLowerMacCOM 1 // 是否打印和下位机通讯的相关信息
camera::camera() {}
@ -13,7 +14,6 @@ QTcpServer* server_to_lowermachine = nullptr;
QTcpSocket* lower_machine = nullptr;
bool volatile is_running = false;
//编号0 RGB相机 编号1 BV相机
static MIL_ID MilApplication;
static MIL_ID MilSystem;
@ -46,15 +46,15 @@ ONNXRunner runner;
std::map<std::string, int> params;
int widthBlocks = 20;
int heightBlocks = 512;
int sizeThreshold = 20;
int padLeft = 1;
int padRight = 1;
int rowRange = 0;
int ignoreSide=2;
int skipLeftCols=10;
int skipRightCols=10;
int widthBlocks = 20; // 输出的喷阀通道数
int heightBlocks = 512; // 输出的Mask高度
int sizeThreshold = 20; // 转化为喷阀的每块要求像素个数
int padLeft = 1; // 左侧在结果Mask上补0
int padRight = 1; // 右侧在结果Mask上补0
int rowRange = 1; // 结果维持行数至少为1
int ignoreSide = 0; // 左右两侧的忽略的喷阀通道数量
int skipLeftCols = 0; // 生成mask时跳过左侧的像素数量
int skipRightCols = 0; // 生成mask时跳过右侧的像素数量
static std::vector<std::vector<uint8_t>> tail_0(0);
static std::vector<std::vector<uint8_t>> tail_1(0);
@ -117,16 +117,15 @@ bool iniCamera()
break;
}
}
#if(GlobalDebug)
qDebug()<<"ready";
#endif
return 1;
}
#if(GlobalDebug && DebugDetection)
#define SAVE_PATH_resize MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//resize.png")
// #define SAVE_PATH_resize MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//resize.png")
#define SAVE_PATH_flip MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//flip.png")
#define SAVE_PATH_raw MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//raw.png")
#define SAVE_PATH_result MIL_TEXT ("C:\\Users\\Pc\\Desktop\\cotton_double2//result.png")
@ -134,6 +133,10 @@ bool iniCamera()
MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
{
#if(GlobalDebug && DebugDetectionTime)
Timer timer_detection_time;
timer_detection_time.restart();
#endif
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &ModifiedBufferId0);
if(SaveImg_Flag)
@ -147,35 +150,35 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
qDebug()<<"回调1成功存储照片:"<<FuncCount0<<"";
FuncCount0++;
}
#if(GlobalDebug)
qDebug()<<"回调1";
#endif
//拷贝艳丽色检测图像
MbufCopy(ModifiedBufferId0,MilImage_Color0);
// //拷贝onnx检测图像
// MbufCopy(ModifiedBufferId1,MilImage_Onnx1);
// timer1.printElapsedTime("copy finished");
// timer1.restart();
MIL_UNIQUE_BUF_ID MimResizedestination = MbufAllocColor(MilSystem, 3, 2048, 256, 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);
MimResize(MilImage_Color0, MimResizedestination, 0.5, 0.5, M_DEFAULT);
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_resize,MimResizedestination);
MbufSave(SAVE_PATH_raw,MilImage_Color0);
MbufSave(SAVE_PATH_result,detection_result0);
#endif
@ -189,50 +192,29 @@ 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);
// timer1.printElapsedTime("onnx sat finished");
// timer1.restart();
auto [mask_1, newTail] = generateMaskWithTail(detection_result0, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange, skipLeftCols, skipRightCols);
tail_1 = newTail;
// timer1.printElapsedTime("tail finished");
// timer1.restart();
// std::vector<std::vector<uint8_t>> mask_Color1 = generateMaskFromImage(detection_result1, widthBlocks, heightBlocks, thresholds);
PadColumns(mask_1,padLeft,padRight,0);
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_Total[m][n] == 1)
// {
// for(int k = 0; k < 50; k++)
// {
// mask_Total[m + k][n] = 1;
// }
// }
// }
// }
// save masks
vector<vector<uint8_t>> mask = generateMask(detection_result0, widthBlocks, heightBlocks, sizeThreshold,skipLeftCols, skipRightCols);
// save masks
#if(GlobalDebug && DebugDetection)
VectorToImg(mask,"C:/Users/Pc/Desktop/img/mask" + std::to_string(FuncCount1) + ".bmp");
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)
{
qDebug()<<"下位机发送失败";
qWarning()<<"下位机发送失败";
}
#if(GlobalDebug && DebugDetectionTime)
timer_detection_time.printElapsedTime("Time of Processing From Get into CallBack to Sent to Lower Mac");
#endif
return 0;
}
@ -242,9 +224,6 @@ 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)
@ -848,73 +827,38 @@ bool iniLowMac()
{
/*--- server socket to faban ---*/
server_to_lowermachine = new QTcpServer();
qDebug()<<"1";
#if(GlobalDebug && DebugLowerMacCOM)
qDebug()<<"1/4. Build Tcp Server";
#endif
server_to_lowermachine->listen(QHostAddress::Any, 13452);
qDebug()<<"2";
#if(GlobalDebug && DebugLowerMacCOM)
qDebug()<<"2/4. Bind to Port 13452";
#endif
bool is_timeout;
server_to_lowermachine->waitForNewConnection(5000,&is_timeout);
qDebug()<<"3";
#if(GlobalDebug && DebugLowerMacCOM)
qDebug()<<"3/4. Try to Establish connect";
#endif
if(is_timeout)
{
return 0;
}
qDebug()<<"4";
#if(GlobalDebug && DebugLowerMacCOM)
qDebug()<<"4/4. Connection Established no timeout";
#endif
lower_machine = server_to_lowermachine->nextPendingConnection();
qDebug()<<"连接成功";
#if(GlobalDebug && DebugLowerMacCOM)
qDebug()<<"Lower Machine Connection Suecced";
#endif
if (lower_machine == nullptr || !lower_machine->isWritable()) {
cout << "Error: Lower machine is not available or writable." << endl;
return 0 ;
}
// // 创建 TCP 服务器对象
// server_to_lowermachine = new QTcpServer();
// // 启动监听
// if (!server_to_lowermachine->listen(QHostAddress::Any, 13452)) {
// qDebug() << "Error: Server failed to start.";
// return false;
// }
// qDebug() << "Server is listening on port 13452.";
// // 等待新连接
// if (!server_to_lowermachine->waitForNewConnection(5000)) {
// qDebug() << "Timeout or no connection.";
// return false;
// }
// // 获取连接
// lower_machine = server_to_lowermachine->nextPendingConnection();
// if (lower_machine) {
// qDebug() << "New client connected.";
// // 连接数据接收信号
// QObject::connect(lower_machine, &QTcpSocket::readyRead, []() {
// QByteArray data = lower_machine->readAll();
// qDebug() << "Received data:" << data;
// // 向下位机发送响应
// QByteArray response = "Acknowledged.";
// lower_machine->write(response);
// });
// // 连接断开信号
// QObject::connect(lower_machine, &QTcpSocket::disconnected, []() {
// qDebug() << "Client disconnected.";
// lower_machine->deleteLater();
// });
// return true;
// } else {
// qDebug() << "Error: No client connected.";
// return false;
// }
// 硬编码参数值
int file_delay = 1530; // 延迟时间(毫秒)
int file_encoder = 12000; // 编码器值
@ -925,11 +869,6 @@ bool iniLowMac()
int divide_valve = (file_valve != 0) ? 100000000 / file_valve : 0; // 防止除以零的情况
// 将参数转换为长度为8的字符串前面补0
QString delay_time = QString("%1").arg(file_delay, 8, 10, QChar('0'));
QString divide_parameter = QString("%1").arg(divide_camera, 8, 10, QChar('0'));
@ -1232,7 +1171,13 @@ pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> applyRowRangeDelay(
const vector<vector<uint8_t>>& mask,
const vector<vector<uint8_t>>& tail,
int rowRange
) {
)
{
if(rowRange <= 0)
{
rowRange = 1;
qDebug() << "Maintain Row Range Error, forced to 1!!!!!!!!";
}
int outputHeight = (int)mask.size();
int outputWidth = (int)mask[0].size();