pad columns feature added

This commit is contained in:
zhenye 2024-12-16 13:13:38 +08:00
parent 645f428d92
commit bc9a5f9fc7
2 changed files with 49 additions and 21 deletions

View File

@ -46,11 +46,15 @@ ONNXRunner runner;
std::map<std::string, int> 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<std::vector<uint8_t>> tail_0(0);
static std::vector<std::vector<uint8_t>> 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<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");
@ -216,7 +221,7 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
// }
// }
// save masks
vector<vector<uint8_t>> mask = generateMask(detection_result0, widthBlocks, heightBlocks, sizeThreshold,ignoreSide);
vector<vector<uint8_t>> 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<std::vector<uint8_t> > &array, const std::str
imwrite(image_path, image);
}
vector<vector<uint8_t>> 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<vector<uint8_t>> 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<vector<uint8_t>> mask(outputHeight, vector<uint8_t>(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<vector<uint8_t>> generateMask(
return mask;
}
pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> applyRowRangeDelay(
const vector<vector<uint8_t>>& mask,
const vector<vector<uint8_t>>& tail,
@ -1280,11 +1286,12 @@ pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> 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<vector<uint8_t>> mask = generateMask(inputImg, outputWidth, outputHeight, sizeThreshold,ignoreSide);
vector<vector<uint8_t>> mask = generateMask(inputImg, outputWidth, outputHeight, sizeThreshold, skipLeftCols,skipRightCols);
// Apply rowRange delay
return applyRowRangeDelay(mask, tail, rowRange);
@ -1292,6 +1299,23 @@ pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> generateMaskWithTail(
void PadColumns(std::vector<std::vector<uint8_t>>& 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

View File

@ -26,6 +26,8 @@
std::vector<std::vector<uint8_t>> generateMaskFromImage(const MIL_ID& inputImage, int widthBlocks, int heightBlocks, int thresholds);
std::vector<std::vector<uint8_t>> generateMaskFromImage2(const cv::Mat& image, int widthBlocks, int heightBlocks, int thresholds);
void PadColumns(std::vector<std::vector<uint8_t>>& data, int pad_left, int pad_right, uint8_t fill_value );
std::vector<std::vector<uint8_t>> expandArray(const std::vector<std::vector<uint8_t>>& array, int newCols) ;
void VectorToImg(const std::vector<std::vector<uint8_t>>& array, const std::string& image_path);
@ -38,7 +40,8 @@ std::vector<std::vector<uint8_t>> generateMask(
int outputWidth,
int outputHeight,
int sizeThreshold,
int ignoreSide
int skipLeftCols,
int skipRightCols
) ;
using namespace std;
@ -97,7 +100,8 @@ pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> generateMaskWithTail(
int outputHeight,
int sizeThreshold,
int rowRange,
int ignoreSide
int skipLeftCols,
int skipRightCols
);