Commit 022d21a3 张东亮

视觉检测锁修改

1 个父辈 5fc9789c
...@@ -259,169 +259,198 @@ namespace DeviceLibrary ...@@ -259,169 +259,198 @@ namespace DeviceLibrary
int retrytime = 0; int retrytime = 0;
if (Monitor.TryEnter(testLocObj)) if (Monitor.TryEnter(testLocObj))
{ {
retry: try
string logtxt = $"【" + cameraName + "】开始取图片测试是否有料盘" + "\r\n";
DateTime startTime = DateTime.Now;
Bitmap bmp = null;
bool usebarcode = Setting_Init.CamTestReel_useBarcode;
if (usebarcode)
{ {
try retry:
string logtxt = $"【" + cameraName + "】开始取图片测试是否有料盘" + "\r\n";
DateTime startTime = DateTime.Now;
Bitmap bmp = null;
bool usebarcode = Setting_Init.CamTestReel_useBarcode;
if (usebarcode)
{ {
//bmp = new Bitmap(@"D:\logs\10-30-51-138.bmp"); try
bmp = Camera._cam.GrabOne(cameraName);
if (bmp == null)
{ {
if (retrytime > 2) //bmp = new Bitmap(@"D:\logs\10-30-51-138.bmp");
return null; bmp = Camera._cam.GrabOne(cameraName);
retrytime++; if (bmp == null)
Camera._cam.Close(cameraName); {
//LoadCamera(true); if (retrytime > 2)
return null;
retrytime++;
Camera._cam.Close(cameraName);
//LoadCamera(true);
LogUtil.info($"bitmap为空重试第{retrytime}次"); LogUtil.info($"bitmap为空重试第{retrytime}次");
Thread.Sleep(2000); Thread.Sleep(2000);
goto retry; goto retry;
} }
logtxt += $"【" + cameraName + "】获取到图像" + "\r\n"; logtxt += $"【" + cameraName + "】获取到图像" + "\r\n";
string code = Setting_Init.CamTestReel_barcode; string code = Setting_Init.CamTestReel_barcode;
int codeThreshold = Setting_Init.CamTestReel_barcodeThreshold; int codeThreshold = Setting_Init.CamTestReel_barcodeThreshold;
//if (ConfigHelper.Config.Get("CamTestReel_debug", false)) //if (ConfigHelper.Config.Get("CamTestReel_debug", false))
// srcimg = SaveImageToFile("test", cameraName, bmp); // srcimg = SaveImageToFile("test", cameraName, bmp);
bool hasReel = false; bool hasReel = false;
RemoteDecodeHelper.RemoteDecodeParam remoteDecodeParam = new RemoteDecodeHelper.RemoteDecodeParam RemoteDecodeHelper.RemoteDecodeParam remoteDecodeParam = new RemoteDecodeHelper.RemoteDecodeParam
{
codeTypeList = codeTypeList.ToArray(),
codeCount = QRCodeCount,
timeout = CodeTimeOut
};
List<CodeInfo> cc = RemoteDecodeHelper.DecodeRequest(bmp, remoteDecodeParam);
int count = 0;
if (cc == null) hasReel = true;
else
{
var res = cc.FindAll(s => code.Equals(s.CodeStr));
count = res.Count();
if (res == null || count < codeThreshold)
{ {
hasReel = true; codeTypeList = codeTypeList.ToArray(),
codeCount = QRCodeCount,
timeout = CodeTimeOut
};
List<CodeInfo> cc = RemoteDecodeHelper.DecodeRequest(bmp, remoteDecodeParam);
int count = 0;
if (cc == null) hasReel = true;
else
{
var res = cc.FindAll(s => code.Equals(s.CodeStr));
count = res.Count();
if (res == null || count < codeThreshold)
{
hasReel = true;
}
} }
} string r = "";
string r = ""; if (cc != null && cc.Count > 0)
if (cc != null && cc.Count > 0) {
{ cc.ForEach((c) =>
cc.ForEach((c) => {
c.CodeStr = CodeManager.ReplaceCode(c.CodeStr);
r += "#" + c.CodeStr;
});
}
logtxt += $" 一维码扫描结果:{count}[{r}],设置可扫描到条码数量:{codeThreshold},result:{((count < codeThreshold) ? "有料" : "无料")}\r\n";
if (hasReel != lastHasReel)
{ {
c.CodeStr = CodeManager.ReplaceCode(c.CodeStr); lastHasReel = hasReel;
r += "#" + c.CodeStr; }
}); else if (!Setting_Init.CamTestReel_debug)
logtxt = "";
if (Setting_Init.CamTestReel_debug || (TestHasRight.HasValue && TestHasRight.Value != hasReel))
prcimg = SaveImageToFile("test2", cameraName, bmp);
TestHasRight = null;
return hasReel;
} }
logtxt += $" 一维码扫描结果:{count}[{r}],设置可扫描到条码数量:{codeThreshold},result:{((count < codeThreshold) ? "有料" : "无料")}\r\n"; catch (AccessViolationException e)
if (hasReel != lastHasReel)
{ {
lastHasReel = hasReel; LogUtil.error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
Camera._cam.Close(cameraName);
return null;
}
catch (Exception ex)
{
LogUtil.error(" 扫码出错:" + ex.ToString());
return null;
}
finally
{
if (bmp != null)
bmp.Dispose();
if (!string.IsNullOrEmpty(logtxt))
LogUtil.info(logtxt);
} }
else if (!Setting_Init.CamTestReel_debug)
logtxt = "";
if (Setting_Init.CamTestReel_debug || (TestHasRight.HasValue && TestHasRight.Value != hasReel))
prcimg = SaveImageToFile("test2", cameraName, bmp);
TestHasRight = null;
return hasReel;
}
catch (AccessViolationException e)
{
LogUtil.error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
Camera._cam.Close(cameraName);
return null;
}
catch (Exception ex)
{
LogUtil.error(" 扫码出错:" + ex.ToString());
return null;
}
finally
{
if (bmp != null)
bmp.Dispose();
if (!string.IsNullOrEmpty(logtxt))
LogUtil.info(logtxt);
Monitor.Exit(testLocObj);
} }
} else
else
{
try
{ {
//bmp = new Bitmap(@"D:\logs\10-30-51-138.bmp"); try
bmp = Camera._cam.GrabOneImage(cameraName, PixelType.RGB8);
if (bmp == null)
{ {
if (retrytime > 2) //bmp = new Bitmap(@"D:\logs\10-30-51-138.bmp");
return null; bmp = Camera._cam.GrabOneImage(cameraName, PixelType.RGB8);
retrytime++; if (bmp == null)
Camera._cam.Close(cameraName); {
//LoadCamera(true); if (retrytime > 2)
return null;
retrytime++;
Camera._cam.Close(cameraName);
//LoadCamera(true);
LogUtil.info($"bitmap为空重试第{retrytime}次"); LogUtil.info($"bitmap为空重试第{retrytime}次");
Thread.Sleep(2000); Thread.Sleep(2000);
goto retry; goto retry;
} }
logtxt += $"【" + cameraName + "】获取到图像" + "\r\n"; logtxt += $"【" + cameraName + "】获取到图像" + "\r\n";
int totalcover = Setting_Init.CamTestReel_totalcover; int totalcover = Setting_Init.CamTestReel_totalcover;
int hl = Setting_Init.CamTestReel_HL; int hl = Setting_Init.CamTestReel_HL;
int hh = Setting_Init.CamTestReel_HH; int hh = Setting_Init.CamTestReel_HH;
int ll = Setting_Init.CamTestReel_LL; int ll = Setting_Init.CamTestReel_LL;
int lh = Setting_Init.CamTestReel_LH; int lh = Setting_Init.CamTestReel_LH;
int sl = Setting_Init.CamTestReel_SL; int sl = Setting_Init.CamTestReel_SL;
int sh = Setting_Init.CamTestReel_SH; int sh = Setting_Init.CamTestReel_SH;
double threshold = Setting_Init.CamTestReel_threshold; double threshold = Setting_Init.CamTestReel_threshold;
if (Setting_Init.CamTestReel_debug) if (Setting_Init.CamTestReel_debug)
srcimg = SaveImageToFile("test", cameraName, bmp); srcimg = SaveImageToFile("test", cameraName, bmp);
var b = new Bitmap(bmp.Width / 2, bmp.Height / 2, bmp.PixelFormat); var b = new Bitmap(bmp.Width / 2, bmp.Height / 2, bmp.PixelFormat);
using (var g = Graphics.FromImage(b)) using (var g = Graphics.FromImage(b))
{ {
g.InterpolationMode = InterpolationMode.Low; g.InterpolationMode = InterpolationMode.Low;
g.DrawImage(bmp, 0, 0, b.Width, b.Height); g.DrawImage(bmp, 0, 0, b.Width, b.Height);
} }
ImageLockMode imageLockMode = ImageLockMode.ReadOnly; ImageLockMode imageLockMode = ImageLockMode.ReadOnly;
if (Setting_Init.CamTestReel_debug) if (Setting_Init.CamTestReel_debug)
imageLockMode = ImageLockMode.ReadWrite; imageLockMode = ImageLockMode.ReadWrite;
var bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), imageLockMode, b.PixelFormat); var bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), imageLockMode, b.PixelFormat);
var rois = TestRoi.GetRois(); var rois = TestRoi.GetRois();
if (rois == null || rois.Count == 0) if (rois == null || rois.Count == 0)
{ {
rois = new List<TestRoi>() { new TestRoi() }; rois = new List<TestRoi>() { new TestRoi() };
rois[0].CoverCount = totalcover; rois[0].CoverCount = totalcover;
rois[0].RoiPath.Add(new Point(0, 0)); rois[0].RoiPath.Add(new Point(0, 0));
rois[0].RoiPath.Add(new Point(bmp.Width, 0)); rois[0].RoiPath.Add(new Point(bmp.Width, 0));
rois[0].RoiPath.Add(new Point(bmp.Width, bmp.Height)); rois[0].RoiPath.Add(new Point(bmp.Width, bmp.Height));
rois[0].RoiPath.Add(new Point(0, bmp.Height)); rois[0].RoiPath.Add(new Point(0, bmp.Height));
} }
bool hasReel = false; bool hasReel = false;
for (int i = 0; i < rois.Count; i++) for (int i = 0; i < rois.Count; i++)
{
rois[i].CoverCount = totalcover;
double maskcout = 0;
var roi = rois[i];
var rp = roi.RoiPath.Select(r => new Point(r.X / 2, r.Y / 2)).ToList();
var fp = GetFilledPoints(rp);
var nfp = fp.Where(f => IsPointInPath(f.X, f.Y, rp)).ToList();
foreach (var xy in nfp)
{ {
int x = (xy.Y * bd.Width + xy.X) * 3; rois[i].CoverCount = totalcover;
//Marshal.WriteByte(bd.Scan0, x, 0); double maskcout = 0;
//Marshal.WriteByte(bd.Scan0, x + 1, 0); var roi = rois[i];
//Marshal.WriteByte(bd.Scan0, x + 2, 255); var rp = roi.RoiPath.Select(r => new Point(r.X / 2, r.Y / 2)).ToList();
var fp = GetFilledPoints(rp);
var nfp = fp.Where(f => IsPointInPath(f.X, f.Y, rp)).ToList();
foreach (var xy in nfp)
{
int x = (xy.Y * bd.Width + xy.X) * 3;
//Marshal.WriteByte(bd.Scan0, x, 0);
//Marshal.WriteByte(bd.Scan0, x + 1, 0);
//Marshal.WriteByte(bd.Scan0, x + 2, 255);
var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2);
var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1);
var cb = (int)Marshal.ReadByte(bd.Scan0, x);
var h = ColorHelper.RgbToHsv(new ColorRGB(cr, cg, cb));
if (h.H >= hl && h.H <= hh && h.V >= ll && h.V <= lh && h.S >= sl && h.S <= sh)
{
maskcout++;
if (Setting_Init.CamTestReel_debug)
{
Marshal.WriteByte(bd.Scan0, x, 0);
Marshal.WriteByte(bd.Scan0, x + 1, 0);
Marshal.WriteByte(bd.Scan0, x + 2, 255);
}
}
}
double calc = maskcout / (double)roi.CoverCount;
logtxt += $" 检测到Roi[{i}]覆盖面积计数:maskcout:{maskcout}/{roi.CoverCount}={calc}<{threshold},result:{(calc <= threshold ? "有料" : "无料")}\r\n";
if (calc <= threshold)
{
hasReel = true;
break;
}
}
/*
for (int x = 0; x < b.Width * b.Height * 3; x = x + 3)
{
var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2); var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2);
var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1); var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1);
var cb = (int)Marshal.ReadByte(bd.Scan0, x); var cb = (int)Marshal.ReadByte(bd.Scan0, x);
...@@ -430,78 +459,53 @@ namespace DeviceLibrary ...@@ -430,78 +459,53 @@ namespace DeviceLibrary
if (h.H >= hl && h.H <= hh && h.V >= ll && h.V <= lh && h.S >= sl && h.S <= sh) if (h.H >= hl && h.H <= hh && h.V >= ll && h.V <= lh && h.S >= sl && h.S <= sh)
{ {
maskcout++; maskcout++;
if (Setting_Init.CamTestReel_debug) if (ConfigHelper.Config.Get("CamTestReel_debug", false))
{ {
Marshal.WriteByte(bd.Scan0, x, 0); Marshal.WriteByte(bd.Scan0, x, 0);
Marshal.WriteByte(bd.Scan0, x + 1, 0); Marshal.WriteByte(bd.Scan0, x + 1, 0);
Marshal.WriteByte(bd.Scan0, x + 2, 255); Marshal.WriteByte(bd.Scan0, x + 2, 255);
} }
} }
} }*/
double calc = maskcout / (double)roi.CoverCount; b.UnlockBits(bd);
logtxt += $" 检测到Roi[{i}]覆盖面积计数:maskcout:{maskcout}/{roi.CoverCount}={calc}<{threshold},result:{(calc <= threshold ? "有料" : "无料")}\r\n";
if (calc <= threshold) if (hasReel != lastHasReel)
{ {
hasReel = true; lastHasReel = hasReel;
break;
} }
else if (!Setting_Init.CamTestReel_debug)
logtxt = "";
if (Setting_Init.CamTestReel_debug || (TestHasRight.HasValue && TestHasRight.Value != hasReel))
prcimg = SaveImageToFile("test2", cameraName, b);
b.Dispose();
TestHasRight = null;
return hasReel;
} }
catch (AccessViolationException e)
/*
for (int x = 0; x < b.Width * b.Height * 3; x = x + 3)
{ {
var cr = (int)Marshal.ReadByte(bd.Scan0, x + 2); LogUtil.error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
var cg = (int)Marshal.ReadByte(bd.Scan0, x + 1); Camera._cam.Close(cameraName);
var cb = (int)Marshal.ReadByte(bd.Scan0, x); return null;
}
var h = ColorHelper.RgbToHsv(new ColorRGB(cr, cg, cb)); catch (Exception ex)
if (h.H >= hl && h.H <= hh && h.V >= ll && h.V <= lh && h.S >= sl && h.S <= sh) {
{ LogUtil.error(" 扫码出错:" + ex.ToString());
maskcout++; return null;
if (ConfigHelper.Config.Get("CamTestReel_debug", false)) }
{ finally
Marshal.WriteByte(bd.Scan0, x, 0);
Marshal.WriteByte(bd.Scan0, x + 1, 0);
Marshal.WriteByte(bd.Scan0, x + 2, 255);
}
}
}*/
b.UnlockBits(bd);
if (hasReel != lastHasReel)
{ {
lastHasReel = hasReel; if (bmp != null)
bmp.Dispose();
if (!string.IsNullOrEmpty(logtxt))
LogUtil.info(logtxt);
} }
else if (!Setting_Init.CamTestReel_debug)
logtxt = "";
if (Setting_Init.CamTestReel_debug || (TestHasRight.HasValue && TestHasRight.Value != hasReel))
prcimg = SaveImageToFile("test2", cameraName, b);
b.Dispose();
TestHasRight = null;
return hasReel;
}
catch (AccessViolationException e)
{
LogUtil.error(" 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
Camera._cam.Close(cameraName);
return null;
}
catch (Exception ex)
{
LogUtil.error(" 扫码出错:" + ex.ToString());
return null;
}
finally
{
if (bmp != null)
bmp.Dispose();
if (!string.IsNullOrEmpty(logtxt))
LogUtil.info(logtxt);
Monitor.Exit(testLocObj);
} }
} }
finally
{
Monitor.Exit(testLocObj);
}
} }
else else
return null; return null;
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!