示例#1
0
        /// <summary>
        /// 在当前图像上按单元配置查找直线
        /// </summary>
        /// <param name="img"></param>
        /// <param name="hFixTool"></param>
        /// <returns></returns>
        public bool FindLine(HObject img)
        {
            m_rowBegin = new HTuple();
            m_colBegin = new HTuple();
            m_rowEnd   = new HTuple();
            m_colEnd   = new HTuple();

            m_hLine.Dispose();
            m_hCross.Dispose();
            m_hCrossDrop.Dispose();
            if (img == null)
            {
                return(false);
            }

            string strSelect = string.Empty;

            if (Hselect == "strongest")
            {
                strSelect = "all";
            }
            else
            {
                strSelect = Hselect;
            }

            HTuple imgWidth, imgHeight;

            HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight);

            m_imageWidth  = imgWidth;
            m_imageHeight = imgHeight;

            double x1, y1, x2, y2;
            HTuple in_phi_t = in_phi + data_from_phi + data_phi * 3.1415926 / 180;
            double angle    = in_phi_t[0].D + 3.1415926 / 2;

            x1 = (in_col + data_col)[0].D + L2 * Math.Cos(angle);
            x2 = (in_col + data_col)[0].D - L2 * Math.Cos(angle);
            y1 = (in_row + data_row)[0].D - L2 * Math.Sin(angle);
            y2 = (in_row + data_row)[0].D + L2 * Math.Sin(angle);

            HHomMat2D hom = new HHomMat2D();

            hom.HomMat2dIdentity();
            hom = hom.HomMat2dTranslate((y2 - y1) / (Hnum - 1), (x2 - x1) / (Hnum - 1));

            double rect_row  = y1;
            double rect_col  = x1;
            double rect_phi  = in_phi_t[0].D;
            double rect_len1 = L1;
            double rect_len2 = (double)Hwid / 2.0;
            HTuple hms       = null;

            HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, rect_phi, rect_len1,
                                              rect_len2, imgWidth, imgHeight, "nearest_neighbor", out hms);

            HTuple  Row         = new HTuple();
            HTuple  Col         = new HTuple();
            HTuple  Distance    = new HTuple();
            HTuple  outRow      = new HTuple();
            HTuple  outCol      = new HTuple();
            HTuple  outAmp      = new HTuple();
            HTuple  outDistance = new HTuple();
            HRegion regionCount = new HRegion();

            regionCount.GenEmptyRegion();
            HXLDCont contour = new HXLDCont();

            for (int i = 0; i < Hnum[0].I; ++i)
            {
                if (m_bShowRectang2)
                {
                    contour.Dispose();
                    contour.GenRectangle2ContourXld(rect_row, rect_col, rect_phi, rect_len1, rect_len2);
                    HRegion region;
                    region      = contour.GenRegionContourXld("margin");
                    regionCount = regionCount.Union2(region);
                }
                try
                {
                    HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, strSelect,
                                            out outRow, out outCol, out outAmp, out outDistance);
                }
                catch { continue; }

                if (strSelect == "all" && outRow.Length > 1)
                {
                    HTuple hIndex = outAmp.TupleSortIndex();
                    int    nMax   = hIndex[hIndex.Length - 1];
                    Row.Append(outRow[nMax]);
                    Col.Append(outCol[nMax]);
                }
                else
                {
                    Row.Append(outRow);
                    Col.Append(outCol);
                }
                hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col);
                HOperatorSet.TranslateMeasure(hms, rect_row, rect_col);
            }
            HOperatorSet.CloseMeasure(hms);
            if (m_bShowRectang2)
            {
                m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter");
            }

            if (Row.Length > 1)
            {
                HXLDCont counter = new HXLDCont();
                counter.GenContourPolygonXld(Row, Col);

                HTuple nr, nc, dist;
                counter.FitLineContourXld("tukey", -1, 0, 5, 2,
                                          out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist);

                HTuple rowDrop = new HTuple();
                HTuple colDrop = new HTuple();
                if (Row.Length - 3 > m_nNumDropPoints && m_nNumDropPoints > 0)
                {
                    HTuple distance = new HTuple();

                    for (int i = 0; i < Row.Length; ++i)
                    {
                        double dis = HMisc.DistancePl(Row[i], Col[i], m_rowBegin, m_colBegin, m_rowEnd, m_colEnd);
                        distance.Append(dis);
                    }
                    HTuple index = distance.TupleSortIndex();
                    index = index.TupleInverse();
                    for (int i = 0; i < m_nNumDropPoints; ++i)
                    {
                        int n = index[i];
                        rowDrop.Append(Row[n]);
                        colDrop.Append(Col[n]);
                    }
                    index = index.TupleFirstN(m_nNumDropPoints - 1);
                    Row   = Row.TupleRemove(index);
                    Col   = Col.TupleRemove(index);

                    counter.GenContourPolygonXld(Row, Col);
                    counter.FitLineContourXld("tukey", -1, 0, 5, 2,
                                              out m_rowBegin, out m_colBegin, out m_rowEnd, out m_colEnd, out nr, out nc, out dist);
                }

                if (m_bShowLine)
                {
                    HTuple rrow = new HTuple(m_rowBegin);
                    HTuple ccol = new HTuple(m_colBegin);
                    rrow.Append(m_rowEnd);
                    ccol.Append(m_colEnd);
                    m_hLine.GenContourPolygonXld(rrow, ccol);
                }

                if (m_bShowCross)
                {
                    m_hCross.GenEmptyObj();
                    m_hCross.GenCrossContourXld(Row, Col, 17, (new HTuple(45)).TupleRad());
                }

                if (m_bShowCrossDrop)
                {
                    m_hCrossDrop.GenEmptyObj();
                    m_hCrossDrop.GenCrossContourXld(rowDrop, colDrop, 17, (new HTuple(45)).TupleRad());
                }

                Result_Array[0] = m_rowBegin;
                Result_Array[1] = m_colBegin;
                Result_Array[2] = m_rowEnd;
                Result_Array[3] = m_colEnd;
                return(true);
            }
            return(false);
        }
示例#2
0
        public bool FindCircle(HObject img)
        {
            HTuple  Row         = new HTuple();
            HTuple  Col         = new HTuple();
            HTuple  Distance    = new HTuple();
            HTuple  outRow      = new HTuple();
            HTuple  outCol      = new HTuple();
            HTuple  outAmp      = new HTuple();
            HTuple  outDistance = new HTuple();
            HRegion regionCount = new HRegion();

            regionCount.GenEmptyRegion();
            HXLDCont contour = new HXLDCont();

            if (img == null)
            {
                return(false);
            }
            HTuple imgWidth, imgHeight;
            HTuple hms = null;

            HOperatorSet.GetImageSize(img, out imgWidth, out imgHeight);

            double xCenter, yCenter, Center_radius, angle, angleSplit;

            angle = 0;
            //分割角度
            angleSplit    = 2 * 3.1415926 / (Hnum);
            xCenter       = in_col[0].D + data_col;
            yCenter       = in_row[0].D + data_row;
            Center_radius = in_radius[0].D;

            HHomMat2D hom = new HHomMat2D();

            hom.HomMat2dIdentity();
            hom = hom.HomMat2dRotate(angleSplit, yCenter, xCenter);
            double rect_row, rect_col, angleStart, L1, L2;

            //分割矩形的中心坐标
            rect_row = yCenter;
            rect_col = xCenter + Center_radius;
            L1       = HLengh[0].D / 2;
            L2       = Hwid[0].D / 2;
            if (Hdirection != "in2out")
            {
                //由外向内
                angleStart = angle + (new HTuple(180)).TupleRad();
            }
            else
            {
                angleStart = angle;
            }

            for (int i = 0; i < Hnum[0].I; ++i)
            {
                if (m_bShowRectang2)
                {
                    contour.Dispose();
                    contour.GenRectangle2ContourXld(rect_row, rect_col, angleStart + angleSplit * i, L1, L2);
                    HRegion region;
                    region      = contour.GenRegionContourXld("margin");
                    regionCount = regionCount.Union2(region);
                    region.Dispose();
                }
                try
                {//out_row, out_col, out_radius;
                    HOperatorSet.GenMeasureRectangle2(rect_row, rect_col, angleStart + angleSplit * i, L1,
                                                      L2, imgWidth, imgHeight, "nearest_neighbor", out hms);
                    HOperatorSet.MeasurePos(img, hms, Hsomth, Hamp, Htransition, Hselect,
                                            out outRow, out outCol, out outAmp, out outDistance);
                }
                catch { continue; }
                if (Hselect == "all" && outRow.Length > 1)
                {
                    HTuple hIndex = outAmp.TupleSortIndex();
                    int    nMax   = hIndex[hIndex.Length - 1];
                    Row.Append(outRow[nMax]);
                    Col.Append(outCol[nMax]);
                }
                else
                {
                    Row.Append(outRow);
                    Col.Append(outCol);
                }
                hom.AffineTransPixel(rect_row, rect_col, out rect_row, out rect_col);
            }
            HOperatorSet.CloseMeasure(hms);
            if (m_bShowRectang2)
            {
                m_hRectang2.Dispose();
                m_hRectang2 = regionCount.GenContoursSkeletonXld(1, "filter");
            }
            if (Row.Length > Hnum[0].D / 3)
            {
                HXLDCont counter = new HXLDCont();
                counter.GenContourPolygonXld(Row, Col);

                HTuple startphi, endphi;
                HTuple pointorder;
                counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2,
                                            out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder);

                HTuple rowDrop = new HTuple();
                HTuple colDrop = new HTuple();
                if (Row.Length - 5 > m_nNumDropPoints && m_nNumDropPoints > 0)
                {
                    HTuple distance = new HTuple();
                    for (int i = 0; i < Row.Length; ++i)
                    {
                        double dis = HMisc.DistancePp(Row[i], Col[i], xCenter, yCenter);
                        distance.Append(Math.Abs(dis - Center_radius));
                    }
                    HTuple index = distance.TupleSortIndex();
                    index = index.TupleInverse();
                    for (int i = 0; i < m_nNumDropPoints; ++i)
                    {
                        int n = index[i];
                        rowDrop.Append(Row[n]);
                        colDrop.Append(Col[n]);
                    }
                    index = index.TupleFirstN(m_nNumDropPoints - 1);
                    Row   = Row.TupleRemove(index);
                    Col   = Col.TupleRemove(index);

                    counter.GenContourPolygonXld(Row, Col);
                    counter.FitCircleContourXld("atukey", -1, 0, 0, 3, 2,
                                                out out_row, out out_col, out out_radius, out startphi, out endphi, out pointorder);
                }
                m_hCircle.Dispose();
                m_centerCross.Dispose();
                if (m_bShowCircle)
                {
                    HOperatorSet.GenCrossContourXld(out m_centerCross, out_row, out_col, (out_radius / 2) + 1, (new HTuple(0)).TupleRad());
                    HOperatorSet.GenCircleContourXld(out m_hCircle, out_row, out_col, out_radius,
                                                     0, 3.1415926 * 2, "positive", 1);
                }
                m_hCross.Dispose();
                if (m_bShowCross)
                {
                    HOperatorSet.GenCrossContourXld(out m_hCross, Row, Col, 17, (new HTuple(45)).TupleRad());
                }

                Result_Array[0] = out_row;
                Result_Array[1] = out_col;
                Result_Array[2] = out_radius;
                return(true);
            }
            return(false);
        }