Vuser_607ziMphQuUQY
2026-06-02 17:36
VM算法平台

调用DL图像分割算子,但是识别不到瑕疵

有没有大佬帮忙看看是什么问题?

using System;
using System.Collections.Generic;
using System.IO;
using VisionDesigner; // VisionDesigner机器视觉平台核心库
using VisionDesigner.MVDCNNSegment; // CNN语义分割专用模块

namespace CNNSegmentConsoleDemo
{
/// <summary>
/// CNN图像分割控制台演示程序
/// 功能:使用预训练的深度学习模型对图像进行语义分割
/// 应用场景:纸杯口视觉检测(baokou - 杯口检测)
/// </summary>
class Program
{
/// <summary>
/// 程序主入口方法
/// </summary>
/// <param name="args">命令行参数(未使用)</param>
static void Main(string[] args)
{
try
{
// ========== 1. 定义输入文件路径 ==========
// 待分割的BMP图像文件路径(纸杯口检测场景)
string imagePath = @"D:\baokou\baokou-12-01-06-124.bmp";
// 预训练的CNN分割模型文件(.bin格式,包含神经网络权重和配置)
strin***odelPath = @"D:\baokou\DL_COH_1_V2_VM440_0415174340427.bin";

Console.WriteLine("检查输入文件...");

// ========== 2. 文件有效性检查 ==========
// 检查图像文件是否存在
if (!File.Exists(imagePath))
{
Console.WriteLine("错误: 图像文件不存在: {0}", imagePath);
Console.ReadKey();
return;
}

// 检查模型文件是否存在
if (!File.Exists(modelPath))
{
Console.WriteLine("错误: 模型文件不存在: {0}", modelPath);
Console.ReadKey();
return;
}

// 输出文件路径信息
Console.WriteLine("图像文件: {0}", imagePath);
Console.WriteLine("模型文件: {0}", modelPath);
Console.WriteLine("开始加载...");

// ========== 3. 初始化核心对象 ==========
// CMvdImage: VisionDesigner图像容器,用于加载和存储图像数据
CMvdImage inputImage = new CMvdImage();

// CNNSegmentTool: CNN分割工具核心类
// 参数指定运行平台:MVD_ALGORITHM_PLATFORM_CPU(CPU平台)
CNNSegmentTool tool = new CNNSegmentTool(MVD_ALGORITHM_PLATFORM_TYPE.MVD_ALGORITHM_PLATFORM_CPU);

// CNNSegmentModelInfo: 模型信息容器,存储模型元数据(类型、标签列表等)
CNNSegmentModelInfo modelinfo = new CNNSegmentModelInfo();

// ========== 4. 加载图像 ==========
// 从文件路径初始化图像
inputImage.InitImage(imagePath);
Console.WriteLine("图像加载成功,尺寸: {0} x {1}", inputImage.Width, inputImage.Height);

// 将加载的图像设置为分割工具的输入
tool.InputImage = inputImage;

// 获取图像尺寸(转为整型,后续可能用于计算)
int nImageWidth = Convert.ToInt32(inputImage.Width);
int nImageHeight = Convert.ToInt32(inputImage.Height);

// 定义感兴趣区域(ROI),此处设置为1x1像素的小区域(实际使用中可根据需求调整)
// CMvdRectangleF rect = new CMvdRectangleF(1, 1, 1, 1);

// ========== 5. 加载模型并配置参数 ==========
// 从模型文件加载预训练权重
tool.BasicParam.LoadModel(modelPath);
Console.WriteLine("模型加载成功");

// 设置分割参数:MinScore(最小置信度阈值)
tool.SetRunParam("MinScore", "100");

// ========== 6. 执行语义分割 ==========
Console.WriteLine("开始执行分割...");
// Run()方法执行深度学习推理,对输入图像进行像素级分类
tool.Run();
Console.WriteLine("分割完成");

// ========== 7. 处理并保存分割结果 ==========
// 获取类别映射图(ClassMap):每个像素值代表其所属类别索引
CMvdImage classMap = tool.Result.ClassMap;
// 保存类别映射图到文件
classMap.SaveImage("class_map.bmp");
Console.WriteLine("类别图已保存: class_map.bmp");

// 输出置信度分数直方图(ScoreHist):统计各置信度区间的像素数量
Console.WriteLine("ScoreHist:");
List<uint> scoreHist = tool.Result.ScoreHist;
foreach (var item in scoreHist)
{
Console.Write(" {0}", item);
}
Console.WriteLine();

// 解析模型信息,获取模型类型和标签列表
modelinfo = tool.ParseModel();
Console.WriteLine("Model Type: {0}", modelinfo.ModelType);
Console.WriteLine("Label Count: {0}", modelinfo.LabelList.Count);

// ========== 8. 遍历每个类别,保存独立的分割图 ==========
for (int i = 0; i < modelinfo.LabelList.Count; i++)
{
// 获取当前类别的名称
string labelName = modelinfo.LabelNameList[i];
// 查询该类别的分割结果信息
CNNSegmentClassInfo segmentinfo = tool.Result.QueryClassInfo(labelName);

// 检查类别信息是否有效
if (segmentinfo == null)
{
Console.WriteLine("警告: Label {0} ({1}) 的类别信息为空", i, labelName);
continue;
}

// 输出类别详细信息
Console.WriteLine("Label index:{0},Label:{1},Label Name:{2}", i, segmentinfo.Label, segmentinfo.LabelName);

// 获取该类别的二值分割图(SegmentMap)
CMvdImage segmentMap = segmentinfo.SegmentMap;
// 检查分割图是否有效
if (segmentMap == null)
{
Console.WriteLine("警告: Label {0} ({1}) 的分割图为空", i, labelName);
continue;
}

// 生成分割图文件名并保存
string segmentMapStr = "segment_map";
segmentMap.SaveImage(segmentMapStr + i.ToString() + ".bmp");
Console.WriteLine("分割图已保存: {0}", segmentMapStr + i.ToString() + ".bmp");
}

// 程序执行完成
Console.WriteLine("Running finish.");
}
// 捕获VisionDesigner库专用异常(包含详细错误码)
catch (MvdException ex)
{
Console.WriteLine("Running fail,ErrorCode:0x{0:X8}", ex.ErrorCode);
Console.WriteLine("Error Description: {0}", ex.Message);
}
// 捕获通用异常(处理其他未预期的错误)
catch (System.Exception ex)
{
Console.WriteLine("Running fail,ErrorMessage:{0}", ex.Message);
Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
}
// 等待用户按键后退出
Console.ReadKey();
}
}
}

----------输出结果-----------

开始执行分割...
num_class: 8 num_class: 8 ASI_convert_modelnum_class: 8 num_class: 8 分割完成
类别图已保存: class_map.bmp
ScoreHist:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
所有分割图为空
输出结果类别图class_map.bmp是纯黑的
同一张图同一个bin文件在vm软件里面可以正确识别瑕疵


  • 36
  • 0
  • 分享

暂无回答

请升级浏览器版本

您正在使用的浏览器版本过低,请升级最新版本以获得更好的体验。

推荐使用以下浏览器