From bc9a5f9fc71a9731a688ebfdf9808584d095d738 Mon Sep 17 00:00:00 2001
From: zhenye
Date: Mon, 16 Dec 2024 13:13:38 +0800
Subject: [PATCH] pad columns feature added
---
camera.cpp | 58 ++++++++++++++++++++++++++++++++++++++----------------
camera.h | 12 +++++++----
2 files changed, 49 insertions(+), 21 deletions(-)
diff --git a/camera.cpp b/camera.cpp
index fe7416c..1c3dcaa 100644
--- a/camera.cpp
+++ b/camera.cpp
@@ -46,11 +46,15 @@ ONNXRunner runner;
std::map params;
-int widthBlocks = 22;
+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;
static std::vector> tail_0(0);
static std::vector> tail_1(0);
@@ -187,7 +191,7 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
// timer1.restart();
- auto [mask_1, newTail] = generateMaskWithTail(detection_result0, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange,ignoreSide);
+ auto [mask_1, newTail] = generateMaskWithTail(detection_result0, tail_1, widthBlocks, heightBlocks, sizeThreshold, rowRange, skipLeftCols, skipRightCols);
tail_1 = newTail;
@@ -197,6 +201,7 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
// std::vector> mask_Color1 = generateMaskFromImage(detection_result1, widthBlocks, heightBlocks, thresholds);
+ PadColumns(mask_1,padLeft,padRight,0);
std::vector> mask_Total = expandArray(mask_1,64);
// timer1.printElapsedTime("expand finished");
@@ -216,7 +221,7 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
// }
// }
// save masks
- vector> mask = generateMask(detection_result0, widthBlocks, heightBlocks, sizeThreshold,ignoreSide);
+ vector> mask = generateMask(detection_result0, widthBlocks, heightBlocks, sizeThreshold,skipLeftCols, skipRightCols);
#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_ignored" + std::to_string(FuncCount1) + ".bmp");
@@ -1176,13 +1181,13 @@ void VectorToImg(const std::vector > &array, const std::str
imwrite(image_path, image);
}
-
vector> generateMask(
const MIL_ID& inputImg,
int outputWidth,
int outputHeight,
int sizeThreshold,
- int ignoreSide=2
+ int skipLeftCols,
+ int skipRightCols
) {
cv::Mat image = mil2mat(inputImg);
@@ -1192,27 +1197,27 @@ vector> generateMask(
int imageWidth = image.cols;
int imageHeight = image.rows;
- int blockWidth = imageWidth / outputWidth;
+ // Adjust image width by excluding skipLeftCols and skipRightCols
+ int effectiveWidth = imageWidth - skipLeftCols - skipRightCols;
+ if (effectiveWidth <= 0) {
+ throw std::invalid_argument("Invalid column skip values. Effective width is less than or equal to zero.");
+ }
+
+ int blockWidth = effectiveWidth / outputWidth;
int blockHeight = imageHeight / outputHeight;
vector> mask(outputHeight, vector(outputWidth, 0));
for (int i = 0; i < outputHeight; ++i) {
for (int j = 0; j < outputWidth; ++j) {
- if((j<=ignoreSide) ||((outputHeight-j)<= ignoreSide) )
- {
- mask[i][j] = 0;
- continue;
- }
- int x_start = j * blockWidth;
+ int x_start = skipLeftCols + j * blockWidth;
int y_start = i * blockHeight;
- int x_end = (j == outputWidth - 1) ? imageWidth : (j + 1) * blockWidth;
+ int x_end = (j == outputWidth - 1) ? (imageWidth - skipRightCols) : (skipLeftCols + (j + 1) * blockWidth);
int y_end = (i == outputHeight - 1) ? imageHeight : (i + 1) * blockHeight;
cv::Mat block = image(cv::Rect(x_start, y_start, x_end - x_start, y_end - y_start));
int whitePixelCount = cv::countNonZero(block);
-
if (whitePixelCount > sizeThreshold) {
mask[i][j] = 1;
}
@@ -1222,6 +1227,7 @@ vector> generateMask(
return mask;
}
+
pair>, vector>> applyRowRangeDelay(
const vector>& mask,
const vector>& tail,
@@ -1280,11 +1286,12 @@ pair>, vector>> generateMaskWithTail(
int outputWidth,
int outputHeight,
int sizeThreshold = 10,
- int rowRange = 50,
- int ignoreSide=2
+ int rowRange=1,
+ int skipLeftCols=10,
+ int skipRightCols=10
) {
// Generate the mask from the image
- vector> mask = generateMask(inputImg, outputWidth, outputHeight, sizeThreshold,ignoreSide);
+ vector> mask = generateMask(inputImg, outputWidth, outputHeight, sizeThreshold, skipLeftCols,skipRightCols);
// Apply rowRange delay
return applyRowRangeDelay(mask, tail, rowRange);
@@ -1292,6 +1299,23 @@ pair>, vector>> generateMaskWithTail(
+void PadColumns(std::vector>& data, int pad_left, int pad_right, uint8_t fill_value = 0)
+{
+ // 如果不需要填充,直接返回
+ if (pad_left <= 0 && pad_right <= 0) {
+ return;
+ }
+
+ for (auto& row : data) {
+ // 在左侧插入pad_left个fill_value
+ row.insert(row.begin(), pad_left, fill_value);
+ // 在右侧插入pad_right个fill_value
+ row.insert(row.end(), pad_right, fill_value);
+ }
+}
+
+
+
//onnx_Mask
diff --git a/camera.h b/camera.h
index 1d9f5d2..35e1556 100644
--- a/camera.h
+++ b/camera.h
@@ -26,6 +26,8 @@
std::vector> generateMaskFromImage(const MIL_ID& inputImage, int widthBlocks, int heightBlocks, int thresholds);
std::vector> generateMaskFromImage2(const cv::Mat& image, int widthBlocks, int heightBlocks, int thresholds);
+
+void PadColumns(std::vector>& data, int pad_left, int pad_right, uint8_t fill_value );
std::vector> expandArray(const std::vector>& array, int newCols) ;
void VectorToImg(const std::vector>& array, const std::string& image_path);
@@ -33,13 +35,14 @@ void VectorToImg(const std::vector>& array, const std::stri
cv::Mat mil2mat(MIL_ID mil_img);
void convert_to_uint8(const MIL_ID& input_img, MIL_ID& output_img);
-std::vector> generateMask(
+std::vector> generateMask(
const MIL_ID& inputImg,
int outputWidth,
int outputHeight,
int sizeThreshold,
- int ignoreSide
- );
+ int skipLeftCols,
+ int skipRightCols
+ ) ;
using namespace std;
using namespace cv;
@@ -97,7 +100,8 @@ pair>, vector>> generateMaskWithTail(
int outputHeight,
int sizeThreshold,
int rowRange,
- int ignoreSide
+ int skipLeftCols,
+ int skipRightCols
);