v1.2 新禾欣版本初版
This commit is contained in:
parent
5f50916938
commit
2764865ef5
Binary file not shown.
Binary file not shown.
@ -90,6 +90,10 @@ namespace LeatherApp
|
||||
public static string cloud_ip, cloud_username, cloud_password, CloudThisName;
|
||||
public static int cloud_port, cloud_open;
|
||||
|
||||
//传统算法
|
||||
public static bool OpenHalconDefect;
|
||||
public static string HalconParamPath;
|
||||
|
||||
public static void LoadCloudConfig()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(appBasePath))
|
||||
@ -212,6 +216,14 @@ namespace LeatherApp
|
||||
SkipLabelGrade = lsTmp.Split(new char[] { ',', ';' });
|
||||
else
|
||||
SkipLabelGrade = new string[1] { "" };
|
||||
|
||||
//传统参数
|
||||
OpenHalconDefect = ini.ReadBool("Halcon", "OpenHalconDefect");
|
||||
lsTmp = ini.ReadString("Halcon", "HalconParamPath", "").Trim();
|
||||
if (!string.IsNullOrWhiteSpace(lsTmp))
|
||||
HalconParamPath = lsTmp;
|
||||
else
|
||||
HalconParamPath = appBasePath;
|
||||
}
|
||||
|
||||
#region defectItemList <=> DefectItemList.json
|
||||
@ -245,6 +257,16 @@ namespace LeatherApp
|
||||
{
|
||||
try
|
||||
{
|
||||
if (id == -99)
|
||||
{
|
||||
JObject obj = new JObject();
|
||||
obj.Add("id", 0);
|
||||
obj.Add("code", "ng");
|
||||
obj.Add("name", "NG");
|
||||
obj.Add("color", "Red");
|
||||
return obj;
|
||||
}
|
||||
|
||||
var item = Config.defectItemList.FirstOrDefault(m => m.Value<int>("id") == id);
|
||||
if (item == null)
|
||||
return null;
|
||||
|
@ -10,11 +10,15 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using HalconDotNet;
|
||||
using System.Windows.Forms.VisualStyles;
|
||||
using LeatherApp.Utils;
|
||||
using Models;
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp.Extensions;
|
||||
using PG_Detect;
|
||||
using SqlSugar;
|
||||
using HZH_Controls;
|
||||
|
||||
namespace LeatherApp.Device
|
||||
{
|
||||
@ -93,22 +97,14 @@ namespace LeatherApp.Device
|
||||
private List<DefectTask> taskMakeTagList = new List<DefectTask>();
|
||||
|
||||
private string preModelName = "";
|
||||
|
||||
//传统算法核心
|
||||
private PGDetect tPGDetect = new PGDetect();
|
||||
public DefectLib()
|
||||
{
|
||||
}
|
||||
public bool start()
|
||||
{
|
||||
//List<DefectLabelInfo> DefectLabelInfoList = new List<DefectLabelInfo>();
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 1953, y = 429, w = 200, h = 200, classId = 1, confidence = 0.8, contrast = 0.9, i = 0, j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() {x = 1953,y = 929,w=900,h = 400,classId = 11,confidence = 0.8, contrast = 0.9, i = 0,j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 3169, y = 1029, w = 900, h = 400, classId = 11, confidence = 0.8, contrast = 0.9, i = 1, j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 4721, y = 919, w = 900, h = 400, classId = 11, confidence = 0.8, contrast = 0.9, i = 2, j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 6145, y = 829, w = 900, h = 400, classId = 11, confidence = 0.8, contrast = 0.9, i = 3, j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 8073, y = 929, w = 900, h = 400, classId = 11, confidence = 0.8, contrast = 0.9, i = 4, j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 9407, y = 929, w = 900, h = 400, classId = 11, confidence = 0.8, contrast = 0.9, i = 5, j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 10801, y = 999, w = 900, h = 400, classId = 11, confidence = 0.8, contrast = 0.9, i = 6, j = 0 });
|
||||
//DefectLabelInfoList.Add(new DefectLabelInfo() { x = 12465, y = 629, w = 900, h = 800, classId = 11, confidence = 0.8, contrast = 0.9, i = 7, j = 0 });
|
||||
//DefectLabelInfoList = HeBingDefect(16384, DefectLabelInfoList);
|
||||
try
|
||||
{
|
||||
//detector = CreateDetector(Config.model_path, Config.labels_path, true, 6);
|
||||
@ -238,6 +234,51 @@ namespace LeatherApp.Device
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool LoadModel(string modelPath)
|
||||
{
|
||||
int step = 0;
|
||||
try
|
||||
{
|
||||
if (!Config.OpenHalconDefect)
|
||||
{
|
||||
if (preModelName != modelPath)
|
||||
{
|
||||
step = 3;
|
||||
//先释放模型
|
||||
if (detector != IntPtr.Zero)
|
||||
{
|
||||
step = 4;
|
||||
DestroyDetector(detector);
|
||||
detector = IntPtr.Zero;
|
||||
}
|
||||
step = 5;
|
||||
detector = CreateDetector($"./models/{modelPath}");
|
||||
if (detector == IntPtr.Zero)
|
||||
throw new Exception($"模型({modelPath})初始化失败!");
|
||||
step = 6;
|
||||
preModelName = modelPath;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HObject obj = new HObject();
|
||||
Mat mat = new Mat($"./init.bmp");
|
||||
OpenCVUtil.MatToHObject(mat, out obj);
|
||||
//HOperatorSet.GenImageConst(out obj, "byte", 2048 * 8, 2048 * 2);//生成一幅图像,用byte格式,默认是黑色
|
||||
//Mat initMat = new Mat(2048 * 2, 2048 * 8, MatType.CV_8UC3);
|
||||
if (!tPGDetect.IniOptimization(obj, Config.HalconParamPath))
|
||||
throw new Exception($"传统算法({Config.HalconParamPath})初始化失败!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
WarningEvent?.Invoke(DateTime.Now, WarningEnum.Low, $"Load Model err({step}):" + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private void runStep()
|
||||
{
|
||||
int step = 0;
|
||||
@ -258,27 +299,13 @@ namespace LeatherApp.Device
|
||||
{
|
||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
||||
step =2;
|
||||
if (preModelName != task.modelName)
|
||||
{
|
||||
step = 3;
|
||||
//先释放模型
|
||||
if (detector != IntPtr.Zero)
|
||||
{
|
||||
step = 4;
|
||||
DestroyDetector(detector);
|
||||
detector = IntPtr.Zero;
|
||||
}
|
||||
step = 5;
|
||||
detector = CreateDetector($"./models/{task.modelName}");
|
||||
if (detector == IntPtr.Zero)
|
||||
throw new Exception($"模型({task.modelName})初始化失败!");
|
||||
step = 6;
|
||||
preModelName = task.modelName;
|
||||
}
|
||||
LoadModel(task.modelName);
|
||||
//源图
|
||||
//Bitmap bmp = yolo1.Read2Bmp(file_path);
|
||||
//切割图像,输入图像格式14208*10640
|
||||
stopwatch.Start();
|
||||
if (!Config.OpenHalconDefect)
|
||||
{
|
||||
//task.resizeBmp = OpenCVUtil.resize( task.bmp.Clone(), task.resize.Width, task.resize.Height);//在外面已做了resize
|
||||
|
||||
//Cv2.CvtColor(task.bmp, task.bmpBgr2rgb, ColorConversionCodes.BGR2RGB);
|
||||
@ -307,6 +334,33 @@ namespace LeatherApp.Device
|
||||
}
|
||||
step = 9;
|
||||
}
|
||||
else
|
||||
{
|
||||
step = 20;
|
||||
|
||||
int object_num = 0;
|
||||
//转换
|
||||
OpenCVUtil.MatToHObject(task.bmp, out task.ho_Img);
|
||||
|
||||
stopwatch.Stop();
|
||||
task.stopwatch[0] = stopwatch.ElapsedMilliseconds;
|
||||
stopwatch.Restart();
|
||||
step = 21;
|
||||
//预处理
|
||||
tPGDetect.Processing(task.ho_Img, out task.ho_imgG, out task.ho_ImageResult3, out task.ho_ImageSub,
|
||||
Config.HalconParamPath,
|
||||
out task.hv_RES, out task.hv_gauss_filter1, out task.hv_gauss_filter2,
|
||||
out task.hv_Energy_value, out task.hv_Region_S_value, out task.hv_class);
|
||||
|
||||
stopwatch.Stop();
|
||||
task.stopwatch[1] = stopwatch.ElapsedMilliseconds;
|
||||
lock (taskOperationList)
|
||||
{
|
||||
taskOperationList.Add(task);
|
||||
QueueCountEvent?.BeginInvoke(1, taskOperationList.Count, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -335,7 +389,7 @@ namespace LeatherApp.Device
|
||||
int cut_count=0,step = 0;
|
||||
try
|
||||
{
|
||||
if (task != null && task.bmps_cut.Count()>0)
|
||||
if (task != null && task.bmps_cut.Count()>0 && !Config.OpenHalconDefect)
|
||||
{
|
||||
//WarningEvent?.Invoke(DateTime.Now,WarningEnum.Low, "DefectLib tasks 运行推理...");
|
||||
cut_count = task.bmps_cut.Count();
|
||||
@ -375,6 +429,24 @@ namespace LeatherApp.Device
|
||||
QueueCountEvent?.BeginInvoke(2, taskMakeTagList.Count, null, null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
||||
stopwatch.Start();
|
||||
step = 22;
|
||||
//算法处理
|
||||
tPGDetect.Detect(task.ho_Img, task.ho_imgG, task.ho_ImageResult3, task.ho_ImageSub,
|
||||
task.hv_Energy_value, task.hv_Region_S_value, task.hv_class,
|
||||
out task.hv_defectinfo, out task.object_num);
|
||||
|
||||
stopwatch.Stop();
|
||||
task.stopwatch[2] = stopwatch.ElapsedMilliseconds;
|
||||
lock (taskMakeTagList)
|
||||
{
|
||||
taskMakeTagList.Add(task);
|
||||
QueueCountEvent?.BeginInvoke(2, taskMakeTagList.Count, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -663,6 +735,8 @@ namespace LeatherApp.Device
|
||||
int count = 0;
|
||||
liStep = 3;
|
||||
|
||||
if (!Config.OpenHalconDefect)
|
||||
{
|
||||
//车用革去除接头处横档误判
|
||||
bool haveJieTou = false;
|
||||
List<DefectLabelInfo> DefectLabelInfoList = new List<DefectLabelInfo>();
|
||||
@ -706,7 +780,7 @@ namespace LeatherApp.Device
|
||||
h = (int)task.output[index + 3],
|
||||
classId = (int)task.output[index + 4],
|
||||
confidence = Math.Round(task.output[index + 5], 2), //置信度
|
||||
contrast = ContrastToPercent( Math.Round(task.output[index + 6], 3)),//对比度
|
||||
contrast = ContrastToPercent(Math.Round(task.output[index + 6], 3)),//对比度
|
||||
//cmW = Math.Round(w * task.widthRatio / Config.cm2px_x, 2),
|
||||
//cmH = Math.Round(h * task.widthRatio / Config.cm2px_y, 2),
|
||||
cmW = Math.Round(w * 1.0 / Config.cm2px_x, 2),
|
||||
@ -836,7 +910,7 @@ namespace LeatherApp.Device
|
||||
|
||||
#region 合并接头横档
|
||||
liStep = 3000;
|
||||
if (DefectLabelInfoList.Count >0)
|
||||
if (DefectLabelInfoList.Count > 0)
|
||||
DefectLabelInfoList = HeBingDefect(task.bmp.Width, DefectLabelInfoList);
|
||||
liStep++;
|
||||
#endregion
|
||||
@ -886,14 +960,14 @@ namespace LeatherApp.Device
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if(qualifiedLimit.Area > 0 && DefectLabelInfoListByClassID[q].cmW * DefectLabelInfoListByClassID[q].cmH < qualifiedLimit.Area)
|
||||
if (qualifiedLimit.Area > 0 && DefectLabelInfoListByClassID[q].cmW * DefectLabelInfoListByClassID[q].cmH < qualifiedLimit.Area)
|
||||
{
|
||||
task.resultInfo += $" 不满足此产品瑕疵标准面积,跳过! \n";
|
||||
continue;
|
||||
}
|
||||
if (Config.getDefectCode(DefectLabelInfoListByClassID[q].classId) == "jietou")
|
||||
haveJieTou = true;
|
||||
if(haveJieTou && (Config.getDefectCode(DefectLabelInfoListByClassID[q].classId) == "hengdang"))
|
||||
if (haveJieTou && (Config.getDefectCode(DefectLabelInfoListByClassID[q].classId) == "hengdang"))
|
||||
{
|
||||
task.resultInfo += $" 判断为接头处横档,跳过! \n";
|
||||
continue;
|
||||
@ -957,6 +1031,80 @@ namespace LeatherApp.Device
|
||||
liStep++;//7
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
else
|
||||
{
|
||||
stopwatch.Restart();
|
||||
//打标
|
||||
var defectinfo = task.hv_defectinfo.ToOArr();
|
||||
List<DefectLabelInfo> DefectLabelInfoList = new List<DefectLabelInfo>();
|
||||
for (int i = 0; i < task.object_num; i++)
|
||||
{
|
||||
liStep = 23 + i;
|
||||
#region 检测信息汇总
|
||||
int index = i * 6;
|
||||
|
||||
// 打印输出信息(示例代码,根据实际情况进行修改和格式化)
|
||||
// 获取输出信息
|
||||
int x = (int)defectinfo[index].ToInt();
|
||||
int y = (int)defectinfo[index + 1].ToInt();
|
||||
int w = (int)defectinfo[index + 2].ToInt();
|
||||
int h = (int)defectinfo[index + 3].ToInt();
|
||||
int classId = -99;//(int)defectinfo[index + 4].ToInt();
|
||||
double confidence = Math.Round(defectinfo[index + 5].ToDouble(), 2); //置信度
|
||||
|
||||
int px = x;
|
||||
var cmX = Math.Round(px * task.widthRatio / Config.cm2px_x, 2);
|
||||
var cmY = Math.Round((task.bmp.Height - y - h) * task.widthRatio / Config.cm2px_y, 2);//外面计Y从右下角为原点
|
||||
|
||||
DefectLabelInfoList.Add(new DefectLabelInfo()
|
||||
{
|
||||
x = x,
|
||||
y = y,
|
||||
w = w,
|
||||
h = h,
|
||||
classId = classId,
|
||||
confidence = confidence, //置信度
|
||||
cmW = Math.Round(w * task.widthRatio / Config.cm2px_x, 2),
|
||||
cmH = Math.Round(h * task.widthRatio / Config.cm2px_y, 2),
|
||||
i = i,
|
||||
j = i,
|
||||
});
|
||||
//y = image_hight-y-h;//转到右下角为原点
|
||||
task.excelTable.Rows.Add($"{task.photoIndex}", cmX, cmY,
|
||||
Math.Round(w * task.widthRatio / Config.cm2px_x, 2), Math.Round(h * task.widthRatio / Config.cm2px_y, 2),
|
||||
i, classId, confidence, 0);
|
||||
|
||||
int penLine = 20;
|
||||
//打标
|
||||
var point1 = new OpenCvSharp.Point(x, y);
|
||||
var point2 = new OpenCvSharp.Point(point1.X + w + penLine / 2, point1.Y + h + penLine / 2);
|
||||
Cv2.Rectangle(task.bmpTag, point1, point2, new Scalar(0.0, 0.0, 255.0), penLine);//画打标点
|
||||
|
||||
//切小图
|
||||
int left, top, decX, decY;
|
||||
|
||||
int tdecX = (w / 2) / 2;
|
||||
int tdecY = w / h > 4 ? w / 8 : (h / 1) / 2;
|
||||
|
||||
decX = (w > 236 ? tdecX : 256 - w) / 2;
|
||||
decY = (h > 236 ? tdecY : 256 - h) / 2;
|
||||
|
||||
left = point1.X - decX;
|
||||
top = point1.Y - decY;
|
||||
if (left < 0) left = 0;
|
||||
if (top < 0) top = 0;
|
||||
|
||||
int width = w + decX * 2;
|
||||
int height = h + decY * 2;
|
||||
if (left + width > task.bmp.Width - 1) width = task.bmp.Width - left - 1;
|
||||
if (top + height > task.bmp.Height - 1) height = task.bmp.Height - top - 1;
|
||||
|
||||
Rect roi = new Rect(left, top, width, height);
|
||||
task.lstDefectBmp.Add(new Mat(task.bmpTag, roi).Clone());
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
liStep = 99;
|
||||
stopwatch.Stop();
|
||||
task.stopwatch[3] = stopwatch.ElapsedMilliseconds;
|
||||
@ -1081,6 +1229,14 @@ namespace LeatherApp.Device
|
||||
public long[] stopwatch = new long[4];
|
||||
|
||||
public int xw;
|
||||
|
||||
//传统算法使用
|
||||
public HObject ho_Img = new HObject();
|
||||
public HObject ho_imgG = new HObject();
|
||||
public HObject ho_ImageResult3 = new HObject();
|
||||
public HObject ho_ImageSub = new HObject();
|
||||
public HTuple hv_RES, hv_gauss_filter1, hv_gauss_filter2, hv_Energy_value, hv_Region_S_value, hv_class, hv_defectinfo;
|
||||
public int object_num = 0;
|
||||
}
|
||||
|
||||
public void add(DefectTask task)
|
||||
|
@ -59,6 +59,15 @@
|
||||
<Reference Include="ExcelNumberFormat, Version=1.1.0.0, Culture=neutral, PublicKeyToken=23c6f5d73be07eca, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\ExcelNumberFormat.1.1.0\lib\net20\ExcelNumberFormat.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="halcondotnet">
|
||||
<HintPath>Dll\halcondotnet.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="hdevenginedotnet">
|
||||
<HintPath>Dll\hdevenginedotnet.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="HZH_Controls">
|
||||
<HintPath>..\..\..\..\..\版博士\源码\V2.0\halftoneproject-master\BanBoShi\Dlls\HZH_Controls.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="IKapBoardClassLibrary">
|
||||
<HintPath>C:\Program Files\I-TEK OptoElectronics\IKapLibrary\Dll\C#\IKapBoardClassLibrary.dll</HintPath>
|
||||
</Reference>
|
||||
@ -86,6 +95,9 @@
|
||||
<Reference Include="OpenCvSharp.Extensions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6adad1e807fea099, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\OpenCvSharp4.Extensions.4.7.0.20230115\lib\net48\OpenCvSharp.Extensions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="PG_Detect">
|
||||
<HintPath>Dll\PG_Detect.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="S7.Net, Version=0.20.0.0, Culture=neutral, PublicKeyToken=d5812d469e84c693, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\S7netplus.0.20.0\lib\net462\S7.Net.dll</HintPath>
|
||||
</Reference>
|
||||
|
@ -64,6 +64,9 @@ namespace LeatherApp.Page
|
||||
private bool init_Cloud;
|
||||
//
|
||||
private bool _residueWarnningLenStop;
|
||||
|
||||
//判断是否已经扫码获取erp信息
|
||||
private bool _IsGetErpCode = false;
|
||||
public FHome(FProductInfo frm)
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -804,6 +807,8 @@ namespace LeatherApp.Page
|
||||
//
|
||||
pStopWatch.Restart();
|
||||
errCode = 19;
|
||||
//扫码成功
|
||||
_IsGetErpCode = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -1590,6 +1595,11 @@ namespace LeatherApp.Page
|
||||
//System.GC.Collect();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理完成回调
|
||||
/// </summary>
|
||||
/// <param name="res"></param>
|
||||
private void callBackDefectEvent(Device.DefectLib.DefectTask res)
|
||||
{
|
||||
{
|
||||
@ -1791,7 +1801,9 @@ namespace LeatherApp.Page
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据保存状态
|
||||
/// </summary>
|
||||
private bool _isDefect = false;
|
||||
private async void saveCurrRecord(int key, string batchId, string reelId, double erpLen)
|
||||
{
|
||||
@ -1892,6 +1904,14 @@ namespace LeatherApp.Page
|
||||
//启动
|
||||
private void btnStart_Click(object sender, EventArgs e)
|
||||
{
|
||||
if(!_IsGetErpCode)
|
||||
{
|
||||
AddTextEvent(DateTime.Now, "启动", "还未扫码获取检测信息!", WarningEnum.Low);
|
||||
return;
|
||||
}
|
||||
|
||||
devContainer.devCamer1.resetScanIndex();
|
||||
devContainer.devCamer2.resetScanIndex();
|
||||
AddTextEvent(DateTime.Now,"启动", "下发启动指令...");
|
||||
if (!Config.StopPLC)
|
||||
this.devContainer.devPlc.runDev();
|
||||
@ -2042,11 +2062,13 @@ namespace LeatherApp.Page
|
||||
pStopWatch.Stop();
|
||||
this.btnStart.Enabled = true;
|
||||
this.btnEnd.Enabled = this.btnPause.Enabled = false;//这里有问题,应该是devPlc回调设置
|
||||
_IsGetErpCode = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
AddTextEvent(DateTime.Now, "结束验布", "无数据结束验布!");
|
||||
_isDefect = false;
|
||||
_IsGetErpCode = false;
|
||||
}
|
||||
}
|
||||
private void lstboxLog_DrawItem(object sender, DrawItemEventArgs e)
|
||||
|
@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xaml;
|
||||
using DocumentFormat.OpenXml.Vml;
|
||||
using HalconDotNet;
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp.XImgProc;
|
||||
namespace LeatherApp.Utils
|
||||
@ -1241,5 +1243,155 @@ namespace LeatherApp.Utils
|
||||
return result;
|
||||
}
|
||||
|
||||
#region MatToHalcon
|
||||
public static void MatToHObject(Mat imgMat, out HObject imgHOject)
|
||||
{
|
||||
int ImageWidth = imgMat.Width;
|
||||
int ImageHeight = imgMat.Height;
|
||||
int channel = imgMat.Channels();
|
||||
long size = ImageWidth * ImageHeight * channel;
|
||||
int col_byte_num = ImageWidth * channel;
|
||||
|
||||
|
||||
byte[] rgbValues = new byte[size];
|
||||
unsafe
|
||||
{
|
||||
for (int i = 0; i < ImageHeight; i++)
|
||||
{
|
||||
IntPtr c = imgMat.Ptr(i);
|
||||
// 一行一行将mat 像素复制到byte[]
|
||||
Marshal.Copy(c, rgbValues, i * col_byte_num, col_byte_num);
|
||||
}
|
||||
void* p;
|
||||
IntPtr ptr;
|
||||
fixed (byte* pc = rgbValues)
|
||||
{
|
||||
p = (void*)pc;
|
||||
ptr = new IntPtr(p);
|
||||
}
|
||||
|
||||
|
||||
if (channel == 1)
|
||||
{
|
||||
HOperatorSet.GenImage1(out imgHOject, "byte", ImageWidth, ImageHeight, ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
HOperatorSet.GenImageInterleaved(out imgHOject, ptr, "bgr", ImageWidth, ImageHeight, 0, "byte", 0, 0, 0, 0, -1, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
#if false
|
||||
/// <summary>
|
||||
/// 把OpenCV图像转换到Halcon图像
|
||||
/// </summary>
|
||||
/// <param name="mImage">OpenCV图像_Mat</param>
|
||||
/// <returns>Halcon图像_HObject</returns>
|
||||
public HObject MatToHImage(Mat mImage)
|
||||
{
|
||||
try
|
||||
{
|
||||
HObject hImage;
|
||||
int matChannels = 0; // 通道数
|
||||
Type matType = null;
|
||||
int width, height; // 宽,高
|
||||
width = height = 0; // 宽,高初始化
|
||||
|
||||
// 获取通道数
|
||||
matChannels = mImage.Channels();
|
||||
if (matChannels == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (matChannels == 1) // 单通道
|
||||
{
|
||||
IntPtr ptr; // 灰度图通道
|
||||
Mat[] mats = mImage.Split();
|
||||
|
||||
// 改自:Mat.GetImagePointer1(mImage, out ptr, out matType, out width, out height); // ptr=2157902018096 cType=byte width=830 height=822
|
||||
ptr = mats[0].Data; // 取灰度图值
|
||||
matType = mImage.GetType(); // byte
|
||||
height = mImage.Rows; // 高
|
||||
width = mImage.Cols; // 宽
|
||||
|
||||
// 改自:hImage = new HObject(new OpenCvSharp.Size(width, height), MatType.CV_8UC1, new Scalar(0));
|
||||
byte[] dataGrayScaleImage = new byte[width * height]; //Mat dataGrayScaleImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
|
||||
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* ptrdata = dataGrayScaleImage)
|
||||
{
|
||||
|
||||
#region 按行复制
|
||||
//for (int i = 0; i < height; i++)
|
||||
//{
|
||||
// CopyMemory((IntPtr)(ptrdata + width * i), new IntPtr((long)ptr + width * i), width);
|
||||
//}
|
||||
|
||||
#endregion
|
||||
|
||||
CopyMemory((IntPtr)ptrdata, new IntPtr((long)ptr), width * height);
|
||||
HOperatorSet.GenImage1(out hImage, "byte", width, height, (IntPtr) ptrdata);
|
||||
|
||||
}
|
||||
}
|
||||
return hImage;
|
||||
}
|
||||
else if (matChannels == 3) // 三通道
|
||||
{
|
||||
IntPtr ptrRed; // R通道图
|
||||
IntPtr ptrGreen; // G通道图
|
||||
IntPtr ptrBlue; // B通道图
|
||||
Mat[] mats = mImage.Split();
|
||||
|
||||
ptrRed = mats[0].Data; // 取R通道值
|
||||
ptrGreen = mats[1].Data; // 取G通道值
|
||||
ptrBlue = mats[2].Data; // 取B通道值
|
||||
matType = mImage.GetType(); // 类型
|
||||
height = mImage.Rows; // 高
|
||||
width = mImage.Cols; // 宽
|
||||
|
||||
// 改自:hImage = new HObject(new OpenCvSharp.Size(width, height), MatType.CV_8UC1, new Scalar(0));
|
||||
byte[] dataRed = new byte[width * height]; //Mat dataGrayScaleImage = new Mat(new OpenCvSharp.Size(width, height), MatType.CV_8UC1);
|
||||
byte[] dataGreen = new byte[width * height];
|
||||
byte[] dataBlue = new byte[width * height];
|
||||
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* ptrdataRed = dataRed, ptrdataGreen = dataGreen, ptrdataBlue = dataBlue)
|
||||
{
|
||||
|
||||
#region 按行复制
|
||||
//HImage himg = new HImage("byte", width, height, (IntPtr)ptrdataRed);
|
||||
//for (int i = 0; i < height; i++)
|
||||
//{
|
||||
// CopyMemory((IntPtr)(ptrdataRed + width * i), new IntPtr((long)ptrRed + width * i), width);
|
||||
// CopyMemory((IntPtr)(ptrdataGreen + width * i), new IntPtr((long)ptrGreen + width * i), width);
|
||||
// CopyMemory((IntPtr)(ptrdataBlue + width * i), new IntPtr((long)ptrBlue + width * i), width);
|
||||
//}
|
||||
|
||||
#endregion
|
||||
|
||||
CopyMemory((IntPtr)ptrdataRed, new IntPtr((long)ptrRed), width * height); // 复制R通道
|
||||
CopyMemory((IntPtr)ptrdataGreen, new IntPtr((long)ptrGreen), width * height); // 复制G通道
|
||||
CopyMemory((IntPtr)ptrdataBlue, new IntPtr((long)ptrBlue), width * height); // 复制B通道
|
||||
HOperatorSet.GenImage3(out hImage, "byte", width, height, (IntPtr)ptrdataRed, (IntPtr)ptrdataGreen, (IntPtr)ptrdataBlue); // 合成
|
||||
}
|
||||
}
|
||||
return hImage;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
{"Direction":0,"IN_OP_SHOW":["XXXX XXXL","XXXX XXXX","XXXX XXXX","XXXX XXXX"],"IN_Waiting_Timeout":0,"OUT_OP_SHOW":["XXXX XXXH","XXXX XXXX","XXXX XXXX","XXXX XXXX"],"SleepPre":0,"SleepLater":0,"AsynRun":false,"Disable":false}
|
||||
{"Direction":0,"IN_OP_SHOW":["XXXX XXXH","XXXX XXXX","XXXX XXXX","XXXX XXXX"],"IN_Waiting_Timeout":0,"OUT_OP_SHOW":["XXXX XXXH","XXXX XXXX","XXXX XXXX","XXXX XXXX"],"SleepPre":0,"SleepLater":0,"AsynRun":false,"Disable":false}
|
Loading…
Reference in New Issue
Block a user