pad columns feature added
This commit is contained in:
parent
645f428d92
commit
bc9a5f9fc7
58
camera.cpp
58
camera.cpp
@ -46,11 +46,15 @@ ONNXRunner runner;
|
|||||||
|
|
||||||
|
|
||||||
std::map<std::string, int> params;
|
std::map<std::string, int> params;
|
||||||
int widthBlocks = 22;
|
int widthBlocks = 20;
|
||||||
int heightBlocks = 512;
|
int heightBlocks = 512;
|
||||||
int sizeThreshold = 20;
|
int sizeThreshold = 20;
|
||||||
|
int padLeft = 1;
|
||||||
|
int padRight = 1;
|
||||||
int rowRange = 0;
|
int rowRange = 0;
|
||||||
int ignoreSide=2;
|
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_0(0);
|
||||||
static std::vector<std::vector<uint8_t>> tail_1(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();
|
// 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;
|
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);
|
// 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);
|
std::vector<std::vector<uint8_t>> mask_Total = expandArray(mask_1,64);
|
||||||
|
|
||||||
// timer1.printElapsedTime("expand finished");
|
// timer1.printElapsedTime("expand finished");
|
||||||
@ -216,7 +221,7 @@ MIL_INT ProcessingFunction0(MIL_INT HookType, MIL_ID HookId, void *HookDataPtr)
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// save masks
|
// 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)
|
#if(GlobalDebug && DebugDetection)
|
||||||
VectorToImg(mask,"C:/Users/Pc/Desktop/img/mask" + std::to_string(FuncCount1) + ".bmp");
|
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");
|
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);
|
imwrite(image_path, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<vector<uint8_t>> generateMask(
|
vector<vector<uint8_t>> generateMask(
|
||||||
const MIL_ID& inputImg,
|
const MIL_ID& inputImg,
|
||||||
int outputWidth,
|
int outputWidth,
|
||||||
int outputHeight,
|
int outputHeight,
|
||||||
int sizeThreshold,
|
int sizeThreshold,
|
||||||
int ignoreSide=2
|
int skipLeftCols,
|
||||||
|
int skipRightCols
|
||||||
) {
|
) {
|
||||||
cv::Mat image = mil2mat(inputImg);
|
cv::Mat image = mil2mat(inputImg);
|
||||||
|
|
||||||
@ -1192,27 +1197,27 @@ vector<vector<uint8_t>> generateMask(
|
|||||||
int imageWidth = image.cols;
|
int imageWidth = image.cols;
|
||||||
int imageHeight = image.rows;
|
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;
|
int blockHeight = imageHeight / outputHeight;
|
||||||
|
|
||||||
vector<vector<uint8_t>> mask(outputHeight, vector<uint8_t>(outputWidth, 0));
|
vector<vector<uint8_t>> mask(outputHeight, vector<uint8_t>(outputWidth, 0));
|
||||||
|
|
||||||
for (int i = 0; i < outputHeight; ++i) {
|
for (int i = 0; i < outputHeight; ++i) {
|
||||||
for (int j = 0; j < outputWidth; ++j) {
|
for (int j = 0; j < outputWidth; ++j) {
|
||||||
if((j<=ignoreSide) ||((outputHeight-j)<= ignoreSide) )
|
int x_start = skipLeftCols + j * blockWidth;
|
||||||
{
|
|
||||||
mask[i][j] = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
int x_start = j * blockWidth;
|
|
||||||
int y_start = i * blockHeight;
|
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;
|
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));
|
cv::Mat block = image(cv::Rect(x_start, y_start, x_end - x_start, y_end - y_start));
|
||||||
|
|
||||||
int whitePixelCount = cv::countNonZero(block);
|
int whitePixelCount = cv::countNonZero(block);
|
||||||
|
|
||||||
if (whitePixelCount > sizeThreshold) {
|
if (whitePixelCount > sizeThreshold) {
|
||||||
mask[i][j] = 1;
|
mask[i][j] = 1;
|
||||||
}
|
}
|
||||||
@ -1222,6 +1227,7 @@ vector<vector<uint8_t>> generateMask(
|
|||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> applyRowRangeDelay(
|
pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> applyRowRangeDelay(
|
||||||
const vector<vector<uint8_t>>& mask,
|
const vector<vector<uint8_t>>& mask,
|
||||||
const vector<vector<uint8_t>>& tail,
|
const vector<vector<uint8_t>>& tail,
|
||||||
@ -1280,11 +1286,12 @@ pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> generateMaskWithTail(
|
|||||||
int outputWidth,
|
int outputWidth,
|
||||||
int outputHeight,
|
int outputHeight,
|
||||||
int sizeThreshold = 10,
|
int sizeThreshold = 10,
|
||||||
int rowRange = 50,
|
int rowRange=1,
|
||||||
int ignoreSide=2
|
int skipLeftCols=10,
|
||||||
|
int skipRightCols=10
|
||||||
) {
|
) {
|
||||||
// Generate the mask from the image
|
// 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
|
// Apply rowRange delay
|
||||||
return applyRowRangeDelay(mask, tail, rowRange);
|
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
|
//onnx_Mask
|
||||||
|
|||||||
12
camera.h
12
camera.h
@ -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>> 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);
|
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) ;
|
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);
|
void VectorToImg(const std::vector<std::vector<uint8_t>>& array, const std::string& image_path);
|
||||||
@ -33,13 +35,14 @@ void VectorToImg(const std::vector<std::vector<uint8_t>>& array, const std::stri
|
|||||||
cv::Mat mil2mat(MIL_ID mil_img);
|
cv::Mat mil2mat(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(
|
||||||
const MIL_ID& inputImg,
|
const MIL_ID& inputImg,
|
||||||
int outputWidth,
|
int outputWidth,
|
||||||
int outputHeight,
|
int outputHeight,
|
||||||
int sizeThreshold,
|
int sizeThreshold,
|
||||||
int ignoreSide
|
int skipLeftCols,
|
||||||
);
|
int skipRightCols
|
||||||
|
) ;
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
@ -97,7 +100,8 @@ pair<vector<vector<uint8_t>>, vector<vector<uint8_t>>> generateMaskWithTail(
|
|||||||
int outputHeight,
|
int outputHeight,
|
||||||
int sizeThreshold,
|
int sizeThreshold,
|
||||||
int rowRange,
|
int rowRange,
|
||||||
int ignoreSide
|
int skipLeftCols,
|
||||||
|
int skipRightCols
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user