Commit 2f57f973 刘韬

添加压紧轴按使用次数回原Comp_Axis_HomeResetTimes

翻版料盘颜色识别添加按roi识别的能力
1 个父辈 68123d21
此文件类型无法预览
此文件类型无法预览
......@@ -50,6 +50,9 @@
<Reference Include="log4net">
<HintPath>..\DLL\log4net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
......@@ -82,6 +85,7 @@
<Compile Include="DeviceLibrary\AxisBean.cs" />
<Compile Include="DeviceLibrary\ServerCommunication_AgvProcess.cs" />
<Compile Include="DeviceLibrary\VisionHelper.cs" />
<Compile Include="TestRoi.cs" />
<Compile Include="theMachine\BoxTransport.cs" />
<Compile Include="theMachine\Common.cs" />
<Compile Include="theMachine\JobList.cs" />
......
......@@ -10,6 +10,7 @@ namespace DeviceLibrary
{
public object ValueEdit(object value)
{
TestRoi.ReloadConfig();
Task.Run(() => CodeManager.TestHasReel(CodeManager.hikNameList[0],out _,out _));
return "Click To Test";
}
......
......@@ -15,6 +15,8 @@ using System.Threading.Tasks;
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using HalconDotNet;
using System.Web;
namespace DeviceLibrary
{
......@@ -181,7 +183,7 @@ namespace DeviceLibrary
List<CodeInfo> cc = new List<CodeInfo>();
cc = RemoteDecodeHelper.DecodeRequest(bmp, remoteDecodeParam);
if (cc != null)
if (cc != null && cc.Count>0)
{
lock (codeList)
{
......@@ -260,6 +262,7 @@ namespace DeviceLibrary
Bitmap bmp = null;
try
{
//bmp = new Bitmap(@"D:\logs\10-30-51-138.bmp");
bmp = Camera._cam.GrabOneImage(cameraName, PixelType.RGB8);
if (bmp == null)
{
......@@ -275,11 +278,11 @@ namespace DeviceLibrary
}
logtxt +=$"【" + cameraName + "】获取到图像"+"\r\n";
int totalcover = ConfigHelper.Config.Get("CamTestReel_totalcover", 69577);
int hl = ConfigHelper.Config.Get("CamTestReel_HL", 18);
int hh = ConfigHelper.Config.Get("CamTestReel_HH", 47);
int hl = ConfigHelper.Config.Get("CamTestReel_HL", 40);
int hh = ConfigHelper.Config.Get("CamTestReel_HH", 70);
int ll = ConfigHelper.Config.Get("CamTestReel_LL", 15);
int lh = ConfigHelper.Config.Get("CamTestReel_LH", 100);
int sl = ConfigHelper.Config.Get("CamTestReel_SL", 95);
int sl = ConfigHelper.Config.Get("CamTestReel_SL", 20);
int sh = ConfigHelper.Config.Get("CamTestReel_SH", 100);
double threshold = ConfigHelper.Config.Get("CamTestReel_threshold", 0.6);
......@@ -287,7 +290,7 @@ namespace DeviceLibrary
if (ConfigHelper.Config.Get("CamTestReel_debug",false))
srcimg= SaveImageToFile("test", cameraName, bmp);
double maskcout = 0;
var b = new Bitmap(bmp.Width / 2, bmp.Height / 2, bmp.PixelFormat);
using (var g = Graphics.FromImage(b))
{
......@@ -299,6 +302,58 @@ namespace DeviceLibrary
imageLockMode = ImageLockMode.ReadWrite;
var bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), imageLockMode, b.PixelFormat);
var rois = TestRoi.GetRois();
if (rois == null || rois.Count == 0)
{
rois = new List<TestRoi>() { new TestRoi() };
rois[0].CoverCount = totalcover;
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, bmp.Height));
rois[0].RoiPath.Add(new Point(0, bmp.Height));
}
bool hasReel=false;
for (int i = 0; i < rois.Count; i++) {
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;
//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 (ConfigHelper.Config.Get("CamTestReel_debug", false))
{
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);
......@@ -316,12 +371,9 @@ namespace DeviceLibrary
Marshal.WriteByte(bd.Scan0, x + 2, 255);
}
}
}
}*/
b.UnlockBits(bd);
double calc = maskcout / (double)totalcover;
logtxt+=$" 检测到覆盖面积计数:maskcout:{maskcout}/{totalcover}={calc}<{threshold},result:{calc <= threshold}";
bool hasReel = calc <= threshold;
if (hasReel != lastHasReel) {
lastHasReel = hasReel;
}else if (!ConfigHelper.Config.Get("CamTestReel_debug", false))
......@@ -440,5 +492,70 @@ namespace DeviceLibrary
return "";
}
}
private static List<Point> GetFilledPoints(List<Point> points)
{
List<Point> filledPoints = new List<Point>();
// 获取路径的边界框
int minX = int.MaxValue;
int minY = int.MaxValue;
int maxX = int.MinValue;
int maxY = int.MinValue;
foreach (Point point in points)
{
if (point.X < minX)
{
minX = point.X;
}
if (point.Y < minY)
{
minY = point.Y;
}
if (point.X > maxX)
{
maxX = point.X;
}
if (point.Y > maxY)
{
maxY = point.Y;
}
}
// 遍历边界框内的每个像素点
for (int y = minY; y <= maxY; y++)
{
for (int x = minX; x <= maxX; x++)
{
if (IsPointInPath(x, y, points))
{
filledPoints.Add(new Point(x, y));
}
}
}
return filledPoints;
}
private static bool IsPointInPath(int x, int y, List<Point> points)
{
bool inside = false;
for (int i = 0, j = points.Count - 1; i < points.Count; j = i++)
{
if (((points[i].Y > y) != (points[j].Y > y)) &&
(x < (points[j].X - points[i].X) * (y - points[i].Y) / (points[j].Y - points[i].Y) + points[i].X))
{
inside = !inside;
}
}
return inside;
}
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DeviceLibrary
{
internal class TestRoi
{
public int CoverCount = 1;
public List<Point> RoiPath = new List<Point>();
const string roifile = "Config\\coverroi.json";
static List<TestRoi> listroi=null;
public static List<TestRoi> GetRois() {
return listroi;
}
static TestRoi()
{
ReloadConfig();
}
public static void ReloadConfig() {
if (!File.Exists(roifile))
return;
//if (listroi != null)
// return;
var roidata = File.ReadAllText(roifile);
listroi = JsonConvert.DeserializeObject<List<TestRoi>>(roidata);
return;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net462" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
</packages>
\ No newline at end of file
......@@ -24,7 +24,7 @@ namespace DeviceLibrary
AxisBean Comp_Axis;
MoveInfo MoveInfo;
public string ErrMsgTxt = "";
int Comp_Axis_HomeResetTimes = ConfigHelper.Config.Get($"Comp_Axis_HomeResetTimes",999);
public bool IgnoreX09 = false;
public event Action<string, StoreMoveType, bool> InOutEndProcessEvent;
......@@ -395,7 +395,14 @@ namespace DeviceLibrary
else
UpDown_Axis.AbsMove(MoveInfo, Config.UpDown_P3, Config.UpDown_P1_speed);
Middle_Axis.AbsMove(MoveInfo, Config.Middle_P1, Config.Middle_P1_speed);
MoveInfo.log($"{storeMoveType}:上下轴,旋转返回待机点P1");
if (Comp_Axis_HomeResetTimes<=0)
{
Comp_Axis_HomeResetTimes = ConfigHelper.Config.Get<int>("Comp_Axis_HomeResetTimes");
Comp_Axis.HomeMove(MoveInfo);
MoveInfo.log($"{storeMoveType}:Comp_Axis到达最大次数开始回原");
}
Comp_Axis_HomeResetTimes--;
MoveInfo.log($"{storeMoveType}:上下轴,旋转返回待机点P1,comrest:{Comp_Axis_HomeResetTimes}");
ErrMsgTxt = "";
break;
case MoveStep.StoreTS17:
......
......@@ -465,6 +465,8 @@ namespace DeviceLibrary
List<CodeInfo> LastCodeList;
LastCodeList = CodeManager.CameraScan(CodeManager.hikNameList);
if (LastCodeList.Count==0)
LastCodeList = CodeManager.CameraScan(CodeManager.hikNameList);
//BoxParam labelParam = new BoxParam();
//labelParam.codeInfos = new List<CodeInfo>(LastCodeList);
......
using ConfigHelper;
using DeviceLibrary;
using log4net.Config;
using OnlineStore.Common;
using System;
......@@ -22,57 +23,11 @@ namespace TheMachine
[STAThread]
static void Main()
{
//var x = new byte[] { 0x02, 0x41, 0x62, 0x2B, 0x30, 0x30, 0x31, 0x2E, 0x30, 0x30, 0x30, 0x32, 0x37, 0x03 };
//01仪表发送的毛重消息为02 41 62 2B 30 30 31 2E 30 30 30 32 37 03。
//2B 30 30 31 2E 30 30 30位具体的毛重数据,转换为ASCII码为 +001.000;
//32H为校验码高位,37H为校验码低位,03H为结束符;
//var datatxt = System.Text.Encoding.ASCII.GetString(x);
//datatxt = datatxt.Substring(3, 8);
//double.TryParse(datatxt, out double value);
//Console.WriteLine(value);
/*
int ff = 0;
var f = @"D:\rick\vs\SO1069MIMO_PLUS\Image_20220511153145272.bmp";
var bmp = new Bitmap(f);
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var b = new Bitmap(bmp.Width/2, bmp.Height/2,bmp.PixelFormat);
using (var g = Graphics.FromImage(b))
{
g.InterpolationMode = InterpolationMode.Low;
g.DrawImage(bmp, 0, 0, b.Width, b.Height);
}
var bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, b.PixelFormat);
var Pixels = new byte[b.Width * b.Height * 3];
// Copy data from pointer to array
Marshal.Copy(bd.Scan0, Pixels, 0, Pixels.Length);
Config.LoadMyConfig(new Setting_Init().GetType());
//ConfigHelper.Config.Set("CamTestReel_debug", true);
//CodeManager.TestHasReel("", out string a, out string b);
//return;
for (int x = 0; x < b.Width * b.Height * 3; x += 3)
{
//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 cr = (int)Pixels[x + 2];
var cg = (int)Pixels[x + 1];
var cb = (int)Pixels[x];
var h = ColorHelper.RgbToHsv(new ColorRGB(cr, cg, cb));
if (h.H >= 59 && h.H <= 61 && h.V >= 90 && h.S >= 90)
{
ff++;
//Marshal.WriteByte(bd.Scan0, x,0);
//Marshal.WriteByte(bd.Scan0, x+1, 0);
//Marshal.WriteByte(bd.Scan0, x+2, 255);
}
}
b.UnlockBits(bd);
stopwatch.Stop();
var t = stopwatch.ElapsedMilliseconds / 1000d;
b.Save(@"D:\rick\vs\SO1069MIMO_PLUS\11-57-09-156_11.bmp");
*/
if (ConfigHelper.Config.Get("Func_CheckReelLocInFix", false))
DeviceLibrary.VisionHelper.CheckAndRunServer();
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!