10.机器视觉通用平台之BLOB分析算法工具
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CvBase;
using CWindowTool;
using HalconDotNet;
using Newtonsoft.Json;
namespace CvImageTool
{
public class BlobTool
{
public FileINI INI = new FileINI();
public BlobParam blobParam; //匹配参数
private string cProcessSettingFilePath; //当前流程配置文件的路径
private string cImageOptSettingFilePath; //当前算法参数文件路径
public string imageSelect = ""; //图像输入参数选择项
public string affinMatrixSelect = ""; //仿射矩阵输入参数选择项
private int cProcessIndex = -1; //当前归属流程的索引
private int cImageOptIndex = -1; //当前归属算法的索引
public bool isShowResultRegion = false;
public bool isShowROI = false;
public bool isShowResultCenter = false;
public HObject Image;
public HTuple AffinMatrix;
//public HObject CutImage;
//public HTuple ModelHandle;
private List<BaseProcess> process;
private CWindows[] cCWindows;
public ROITool roiTool;
public BlobTool(int cProcessIndex, int cImageOptIndex,
string cProcessSettingFilePath, string cImageOptSettingFilePath, string cDescription,
List<BaseProcess> processes, CWindows[] cWindows)
{
this.cProcessSettingFilePath = cProcessSettingFilePath;
this.cImageOptSettingFilePath = cImageOptSettingFilePath;
this.cProcessIndex = cProcessIndex;
this.cImageOptIndex = cImageOptIndex;
this.process = processes;
this.cCWindows = cWindows;
blobParam = new BlobParam();
Image = new HObject();
roiTool = new ROITool(cImageOptSettingFilePath, cDescription + "_ROI");
}
/// <summary>
/// 读取配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool ReadFile(string cProcessDescription)
{
imageSelect = ""; affinMatrixSelect = "";
if (!File.Exists(cProcessSettingFilePath) || !File.Exists(cImageOptSettingFilePath)) return false;
imageSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Image", "", cProcessSettingFilePath);
affinMatrixSelect = FileINI.ReadValueFromIniFile(cProcessDescription, "Matrix", "", cProcessSettingFilePath);
isShowResultRegion = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResultRegion", "", cProcessSettingFilePath));
isShowResultCenter = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "isShowResultCenter", "", cProcessSettingFilePath));
isShowROI = Convert.ToBoolean(FileINI.ReadValueFromIniFile(cProcessDescription, "ShowROI", "", cProcessSettingFilePath));
blobParam = JsonConvert.DeserializeObject<BlobParam>(File.ReadAllText(cImageOptSettingFilePath));
return true;
}
/// <summary>
/// 写配置文件
/// </summary>
/// <param name="cProcessSettingFilePath"></param>
/// <param name="cImgeOptSettingFilePath"></param>
/// <returns></returns>
public bool WriteFile(string cProcessDescription)
{
FileINI.WriteValueFromIniFile(cProcessDescription, "Image", imageSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "Matrix", affinMatrixSelect, cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResultRegion", isShowResultRegion.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "isShowResultCenter", isShowResultCenter.ToString(), cProcessSettingFilePath);
FileINI.WriteValueFromIniFile(cProcessDescription, "ShowROI", isShowROI.ToString(), cProcessSettingFilePath);
var json = JsonConvert.SerializeObject(blobParam, Formatting.Indented);
File.WriteAllText(cImageOptSettingFilePath, json);
return true;
}
public void Running(out HObject ROI, out HObject resultRegion, out HTuple x, out HTuple y, out HTuple area)
{
ROI = new HObject(); resultRegion = new HObject(); x = new HTuple(); y = new HTuple(); area = new HTuple();
if (Image == null) return;
roiTool.ReadROI();
ROI = roiTool.ROI;
string[] imageOptAndParamName = affinMatrixSelect.Split('_');
ProcessTool.Instance.FindImageOpt(process[cProcessIndex], imageOptAndParamName[0], out BaseImageOpt byImageOpt, out int byIndex);
if (byIndex >= 0)
process[cProcessIndex].BaseImageOutParams[byIndex].GetHTupleValue(imageOptAndParamName[1], out AffinMatrix);
else
AffinMatrix = null;
if (blobParam.isAffin && AffinMatrix != null)
{
HOperatorSet.AffineTransRegion(ROI, out HObject NROI, AffinMatrix, "nearest_neighbor");
HObject hObject = null;
if (ROI != null) hObject = ROI;
ROI = NROI;
if (hObject != null) hObject.Dispose();
}
HOperatorSet.ReduceDomain(Image, ROI, out HObject ReduceImage);
HOperatorSet.Threshold(ReduceImage, out HObject Region, blobParam.MinThreshold, blobParam.MaxThreshold);
ReduceImage.Dispose();
HOperatorSet.Connection(Region, out HObject connectionRegion);
Region.Dispose();
HTuple names = new HTuple();
names.Append("area");
names.Append("height");
names.Append("width");
names.Append("circularity");
HTuple minValues = new HTuple();
minValues.Append(blobParam.MinArea);
minValues.Append(blobParam.MinHeight);
minValues.Append(blobParam.MinWidth);
minValues.Append(blobParam.MinCircularity);
HTuple maxValues = new HTuple();
maxValues.Append(blobParam.MaxArea);
maxValues.Append(blobParam.MaxHeight);
maxValues.Append(blobParam.MaxWidth);
maxValues.Append(blobParam.MaxCircularity);
resultRegion.Dispose();
HOperatorSet.SelectShape(connectionRegion, out resultRegion, names, "and", minValues, maxValues);
area.Dispose(); x.Dispose(); y.Dispose();
HOperatorSet.AreaCenter(resultRegion, out area, out y, out x);
if (blobParam.IsGetMaxRegion)
{
HTuple maxA = area.TupleMax();
resultRegion.Dispose();
HOperatorSet.SelectShape(connectionRegion, out HObject MresultRegion, "area", "and", maxA - 1, maxA + 1);
resultRegion = MresultRegion;
}
connectionRegion.Dispose();
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)