优化mask生成

This commit is contained in:
zjc-zjc-123 2024-12-08 17:28:24 +08:00
parent fd368e169b
commit c8a16e44fe
3 changed files with 60 additions and 30 deletions

View File

@ -1,15 +1,21 @@
#include "mask.h"
// 读取二值化的单通道一位图片并生成掩膜
std::vector<std::vector<bool>> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold = 10) {
// 读取图像
cv::Mat image = cv::imread(imagePath, cv::IMREAD_GRAYSCALE);
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
// 检查图像是否成功读取
if (image.empty()) {
std::cerr << "无法加载图像,请检查路径是否正确: " << imagePath << std::endl;
exit(EXIT_FAILURE);
}
#include "utils.h"
std::vector<std::vector<uint8_t>> generateMaskFromImage(const MIL_ID& inputImage, int widthBlocks, int heightBlocks, int threshold = 10, int rowRange = 50) {
// 读取图像
cv::Mat image = mil2mat(inputImage);
//
// // 检查图像是否成功读取
// if (image.empty()) {
// std::cerr << "无法加载图像,请检查路径是否正确: " << imagePath << std::endl;
// exit(EXIT_FAILURE);
// }
// 确保图像是二值化的
cv::threshold(image, image, 128, 255, cv::THRESH_BINARY);
@ -22,8 +28,8 @@ std::vector<std::vector<bool>> generateMaskFromImage(const std::string& imagePat
int blockWidth = imageWidth / widthBlocks;
int blockHeight = imageHeight / heightBlocks;
// 创建掩膜矩阵
std::vector<std::vector<bool>> mask(heightBlocks, std::vector<bool>(widthBlocks, false));
// 创建掩膜矩阵 (uint8_t 类型)
std::vector<std::vector<uint8_t>> mask(heightBlocks, std::vector<uint8_t>(widthBlocks, 0));
// 遍历每个块并统计白色像素点的数量
for (int i = 0; i < heightBlocks; ++i) {
@ -40,9 +46,24 @@ std::vector<std::vector<bool>> generateMaskFromImage(const std::string& imagePat
// 统计块中白色像素的数量
int whitePixelCount = cv::countNonZero(block);
// 如果白色像素数大于阈值,将该块标记为 true
// 如果白色像素数大于阈值,将该块标记为 255
if (whitePixelCount > threshold) {
mask[i][j] = true;
mask[i][j] = 1;
}
}
}
// 遍历每一列处理规则当某列出现第一个1时将其后rowRange行全部置为255
for (int j = 0; j < widthBlocks; ++j) {
bool marked = false; // 标记当前列是否已经处理过第一个1
for (int i = 0; i < heightBlocks; ++i) {
if (mask[i][j] == 1&& !marked) {
// 找到第一个1处理后面rowRange行
for (int k = i; k < std::min(i + rowRange, heightBlocks); ++k) {
mask[k][j] = 1;
}
marked = true; // 标记为已处理后续连续的1不再处理
}
}
}
@ -50,4 +71,3 @@ std::vector<std::vector<bool>> generateMaskFromImage(const std::string& imagePat
return mask;
}

View File

@ -8,9 +8,9 @@
#include <opencv2/opencv.hpp>
#include <vector>
#include"mil.h"
#include <iostream>
std::vector<std::vector<bool>> generateMaskFromImage(const std::string& imagePath, int widthBlocks, int heightBlocks, int threshold);
std::vector<std::vector<uint8_t>> generateMaskFromImage(const MIL_ID& inputImage, int widthBlocks, int heightBlocks, int threshold , int rowRange) ;
#endif //MASK_H

View File

@ -1,39 +1,49 @@
//
// Created by zjc on 24-11-26.
//
#include <Matrox/utils.h>
// #include <Matrox/utils.h>
#include "vector"
#include"iostream"
#include"string"
#include"Matrox/mask.h"
#include"mil.h"
#define Image_PATH3 MIL_TEXT("C:\\Users\\zjc\\Desktop\\mask_1.png")
MIL_ID MilApplication, MilSystem, MilDisplay;
int main() {
// 指定图像路径
std::string imagePath = "C:\\Users\\zjc\\Desktop\\suspect_mask.png";
std::unordered_map<std::string, int> config = loadConfig("C:\\Users\\zjc\\Desktop\\config\\mask_config.txt");
// 设置分块数量和白色像素点阈值
int widthBlocks = config["widthBlocks"];
int heightBlocks = config["heightBlocks"];
int threshold = config["threshold"];
// std::string imagePath = "C:\\Users\\zjc\\Desktop\\mask_1.png";
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
MIL_ID MilImage = M_NULL, MilHighSatResult = M_NULL, MilTemplateMatchingResult = M_NULL;
MbufRestore(Image_PATH3, MilSystem, &MilImage);
// std::unordered_map<std::string, int> config = loadConfig("C:\\Users\\zjc\\Desktop\\config\\mask_config.txt");
// // 设置分块数量和白色像素点阈值
// int widthBlocks = config["widthBlocks"];
// int heightBlocks = config["heightBlocks"];
// int threshold = config["threshold"];
// 输出参数值
std::cout << "widthBlocks: " << widthBlocks << std::endl;
// std::cout << "widthBlocks: " << widthBlocks << std::endl;
// int widthBlocks = 24;
// int heightBlocks = 24;
// int threshold = 20;
int widthBlocks = 24;
int heightBlocks = 1024;
int threshold = 20;
int rowRange = 50; // 后续50行设置为1
// 生成掩膜
std::vector<std::vector<bool>> mask = generateMaskFromImage(imagePath, widthBlocks, heightBlocks, threshold);
std::vector<std::vector<uint8_t>> mask = generateMaskFromImage(MilImage, widthBlocks, heightBlocks, threshold, rowRange);
// 打印掩膜结果
for (int i = 0; i < heightBlocks; ++i) {
for (int j = 0; j < widthBlocks; ++j)
{
std::cout << (mask[i][j] ? "1 " : "0 ");
for (int j = 0; j < widthBlocks; ++j) {
std::cout << (int)mask[i][j] << " ";
}
std::cout << std::endl;
}
MbufFree(MilImage);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}