diff --git a/example/deal.cpp b/example/deal.cpp index 688d569..7f6b26f 100644 --- a/example/deal.cpp +++ b/example/deal.cpp @@ -197,6 +197,10 @@ deal::deal() m_nProductReadThreadNum = READ_PRODUCT_THREAD_NUM; m_nProductQueueMaxSize = PRODUCT_QUEUE_MAX_SIZE; m_nCheckNotifiedCount = 0; + + // ✅ 图片尺寸一致性保护 + m_nExpectedImgWidth = -1; + m_nExpectedImgHeight = -1; } deal::~deal() @@ -1486,6 +1490,10 @@ int deal::CheckProduct(const LoadProductImages &product) m_ProductFailedList.clear(); m_nCheckNotifiedCount = 0; + // ✅ 重置图片期望尺寸(每个产品批次独立) + m_nExpectedImgWidth = -1; + m_nExpectedImgHeight = -1; + // ✅ 修复内存泄漏:清理上一个产品累积的原始图像数据(cv::Mat) // 注意:m_DetResult 是统计结果(轻量字符串/数字),不清除,跨产品累积 m_ImageInfoList.clear(); @@ -1667,6 +1675,42 @@ int deal::SendCheckImg(const CheckImgNotifyInfo &info, IN_IMG_Status_ status, bo m_nProductSuccessCount.fetch_add(1); + // ✅ 图片尺寸一致性保护:获取当前图片的有效图像 + cv::Mat curImg; + if (!info.left_img.empty()) + { + curImg = info.left_img; + } + else if (!info.right_img.empty()) + { + curImg = info.right_img; + } + + if (!curImg.empty()) + { + // 首次设置期望尺寸 + if (m_nExpectedImgWidth < 0 || m_nExpectedImgHeight < 0) + { + m_nExpectedImgWidth = curImg.cols; + m_nExpectedImgHeight = curImg.rows; + printf("[尺寸基准] 产品 %s 首张图片尺寸: %dx%d (channel: %s)\n", + info.productId.c_str(), m_nExpectedImgWidth, m_nExpectedImgHeight, + info.channelName.c_str()); + } + // 尺寸不一致时,resize 到期望尺寸 + else if (curImg.cols != m_nExpectedImgWidth || curImg.rows != m_nExpectedImgHeight) + { + printf("[尺寸修正] 产品 %s 图片尺寸不一致: 期望 %dx%d, 实际 %dx%d (channel: %s),正在resize...\n", + info.productId.c_str(), + m_nExpectedImgWidth, m_nExpectedImgHeight, + curImg.cols, curImg.rows, + info.channelName.c_str()); + cv::Mat resizedImg; + cv::resize(curImg, resizedImg, cv::Size(m_nExpectedImgWidth, m_nExpectedImgHeight)); + curImg = resizedImg; + } + } + std::shared_ptr tem = std::make_shared(); tem->strChannel = info.channelName; @@ -1701,13 +1745,9 @@ int deal::SendCheckImg(const CheckImgNotifyInfo &info, IN_IMG_Status_ status, bo { - if (!info.left_img.empty()) - { - tem->img = info.left_img; - } - else if (!info.right_img.empty()) + if (!curImg.empty()) { - tem->img = info.right_img; + tem->img = curImg; } } diff --git a/example/deal.h b/example/deal.h index b4fe27b..25f8606 100644 --- a/example/deal.h +++ b/example/deal.h @@ -803,6 +803,10 @@ public: std::condition_variable m_productResultCV; // 产品结果保存通知CV(每次save后notify) std::set m_productIdList; // 产品ID列表 + + // ============ 图片尺寸一致性保护 ============ + int m_nExpectedImgWidth; // 当前产品批次期望的图片宽度(-1 表示未设置) + int m_nExpectedImgHeight; // 当前产品批次期望的图片高度(-1 表示未设置) }; #endif //_CORELOGICFACTORY_HPP_ \ No newline at end of file