Exemplo n.º 1
0
        public string RunMaxPoint(HObject Image, HObject orgionImage, HWindow_Final hWindow_Final, HTuple Homat, out FindMaxResult fResult)
        {
            fResult = new FindMaxResult();
            try
            {
                HObject Line = new HObject();
                //PreHandle(Image, out ScaleImage);
                FitLine(Image, null, out Line, Homat);

                //Image = hWindow_Final.Image;
                double xResolution = 0.006;
                if (MyGlobal.GoSDK.context.xResolution != 0)
                {
                    xResolution = MyGlobal.GoSDK.context.xResolution;
                }

                double     MoveLeft   = (inParam.PlateWidth) / xResolution;
                double     BaseOffset = inParam.BaseOffset / xResolution;
                HTuple     baseZ      = new HTuple();
                List <ROI> _roiList   = new List <ROI>();
                if (Homat.Length != 0)
                {
                    for (int i = 0; i < roiList.Count; i++)
                    {
                        _roiList.Add(new ROI());
                        HTuple coord = roiList[i].getModelData();
                        HTuple affinePx, affinePy, affinePx1, affinePy1;
                        HOperatorSet.AffineTransPoint2d(Homat, coord[0], coord[1], out affinePx, out affinePy);
                        HOperatorSet.AffineTransPoint2d(Homat, coord[2], coord[3], out affinePx1, out affinePy1);
                        ROI roi = new ROIRectangle1(affinePx, affinePy, affinePx1, affinePy1);
                        _roiList[i] = roi;
                    }
                }
                else
                {
                    _roiList = roiList;
                }
                if (_roiList.Count > 2)
                {
                    HTuple grayValue = new HTuple();
                    for (int i = 0; i < 2; i++)
                    {
                        HTuple  recR, recC;
                        HRegion temp = _roiList[i].getRegion();
                        HOperatorSet.GetRegionPoints(temp, out recR, out recC);
                        hWindow_Final.viewWindow.displayHobject(temp, "green", true);
                        HTuple GrayValue = new HTuple();

                        HTuple width, height;
                        HOperatorSet.GetImageSize(Image, out width, out height);
                        HTuple newReq = recR.TupleLessElem(height);
                        HTuple newCeq = recC.TupleLessElem(width);
                        HTuple Rid    = newReq.TupleFind(1);
                        HTuple Cid    = newCeq.TupleFind(1);
                        HTuple id     = Rid.TupleIntersection(Cid);
                        if (id.Length != 0)
                        {
                            recR = recR[id];
                            recC = recC[id];

                            HOperatorSet.GetGrayval(orgionImage, recR, recC, out GrayValue);
                            HTuple newGray = GrayValue.TupleGreaterElem(-30);
                            HTuple NeqId   = new HTuple();
                            HOperatorSet.TupleFind(newGray, 1, out NeqId);
                            GrayValue = GrayValue[NeqId];
                            HOperatorSet.TupleSort(GrayValue, out GrayValue);
                            int len   = GrayValue.Length;
                            int len10 = (int)(len * 0.05);
                            GrayValue = GrayValue.TupleSelectRange(len10, len - 1);
                            GrayValue = GrayValue.TupleSelectRange(0, GrayValue.Length - len10);
                            HTuple mean = GrayValue.TupleMean();
                            grayValue = grayValue.TupleConcat(mean);
                        }
                        else
                        {
                            return("基准区域超出图像");
                        }
                    }
                    baseZ = grayValue.TupleMean();
                }

                HTuple MaxZ = new HTuple();
                for (int i = 2; i < _roiList.Count; i++)
                {
                    HRegion temp          = _roiList[i].getRegion();
                    HObject regionContour = new HObject();
                    HOperatorSet.GenContourRegionXld(temp, out regionContour, "border");
                    HTuple intersectR, intersectC, isOverlapping;

                    //hWindow_Final.viewWindow.displayHobject(regionContour, "red", true);

                    HOperatorSet.IntersectionContoursXld(regionContour, Line, "mutual", out intersectR, out intersectC, out isOverlapping);
                    if (intersectR.Length > 1)
                    {
                        HTuple  Coord    = _roiList[i].getModelData();
                        HTuple  colLeft  = intersectC[0] - MoveLeft;
                        HTuple  rowLeft  = Coord[0];
                        HTuple  colRight = intersectC[1] + BaseOffset;
                        HTuple  rowRight = Coord[2];
                        HObject rec      = new HObject();
                        if (colRight.D <= colLeft.D)
                        {
                            colRight = colLeft.D + 1;
                        }
                        HOperatorSet.GenRectangle1(out rec, rowLeft, colLeft, rowRight, colRight);
                        //HOperatorSet.SetColor(hWindow_Final.HWindowHalconID, "red");
                        //HOperatorSet.SetDraw(hWindow_Final.HWindowHalconID, "fill");
                        //HOperatorSet.DispObj(rec, hWindow_Final.HWindowHalconID);
                        hWindow_Final.viewWindow.displayHobject(rec, "red", true);
                        HTuple recR, recC;
                        HOperatorSet.GetRegionPoints(rec, out recR, out recC);
                        HTuple GrayValue = new HTuple();
                        HTuple width, height;
                        HOperatorSet.GetImageSize(Image, out width, out height);
                        HTuple newReq = recR.TupleLessElem(height);
                        HTuple newCeq = recC.TupleLessElem(width);
                        HTuple Rid    = newReq.TupleFind(1);
                        HTuple Cid    = newCeq.TupleFind(1);
                        HTuple id     = Rid.TupleIntersection(Cid);
                        if (id.Length != 0)
                        {
                            recR = recR[id];
                            recC = recC[id];
                            HOperatorSet.GetGrayval(orgionImage, recR, recC, out GrayValue);
                        }
                        else
                        {
                            return("感光片区域超出图像");
                        }
                        HTuple newGray = GrayValue.TupleFind(-30);
                        GrayValue = GrayValue.TupleRemove(newGray);
                        HOperatorSet.TupleSort(GrayValue, out GrayValue);
                        int len   = GrayValue.Length;
                        int len10 = (int)(len * 0.05);
                        //GrayValue = GrayValue.TupleSelectRange(len10, len - 1);
                        GrayValue = GrayValue.TupleSelectRange(GrayValue.Length - len10 * 3 - 1, GrayValue.Length - len10);

                        HTuple max    = GrayValue.TupleMean();
                        HTuple subMax = max.D - baseZ.D;
                        double maxSub = Math.Round(subMax.D, 3);
                        MaxZ = MaxZ.TupleConcat(maxSub);
                        HOperatorSet.SetColor(hWindow_Final.HWindowHalconID, "blue");
                        HOperatorSet.SetFont(hWindow_Final.HWindowHalconID, "-Arial - 20 -");
                        HOperatorSet.SetTposition(hWindow_Final.HWindowHalconID, i * 10, 10);
                        HOperatorSet.WriteString(hWindow_Final.HWindowHalconID, maxSub.ToString());
                    }
                }
                if (MaxZ.Length == 0)
                {
                    return("遮光片区域未找到");
                }
                fResult.Max = MaxZ;
                HTuple maxAll = MaxZ.TupleMax();
                if (maxAll.D > inParam.DownLimits && maxAll.D < inParam.UpLimits)
                {
                    fResult.DetectOK = true;
                    HOperatorSet.SetColor(hWindow_Final.HWindowHalconID, "green");
                    HOperatorSet.SetFont(hWindow_Final.HWindowHalconID, "-Arial - 50 -");
                    HOperatorSet.SetTposition(hWindow_Final.HWindowHalconID, 0, 10);
                    HOperatorSet.WriteString(hWindow_Final.HWindowHalconID, "OK");
                }
                else
                {
                    fResult.DetectOK = false;
                    HOperatorSet.SetColor(hWindow_Final.HWindowHalconID, "red");
                    HOperatorSet.SetFont(hWindow_Final.HWindowHalconID, "-Arial - 50 -");
                    HOperatorSet.SetTposition(hWindow_Final.HWindowHalconID, 0, 10);
                    HOperatorSet.WriteString(hWindow_Final.HWindowHalconID, "NG");
                }
                return("OK");
            }
            catch (Exception ex)
            {
                return("RunMaxPoint" + ex.Message);
            }
        }