public bool FindNccModel(string partName, System.Drawing.Bitmap srcImage, System.Drawing.Rectangle roi, ref MatchResult_Ncc result) { if (srcImage == null) { return(false); } bool bIsSuccess = false; HTuple row, column, angle, score; HTuple hv_HomMat2D; HImage hImage = new HImage(); try { int modelID = 0; GC.Collect(); if (!CheckPartModelIdIsExist(partName, ref modelID)) { return(false); } if (m_hoRectangle != null) { m_hoRectangle.Dispose(); } m_hoRectangle.GenRectangle1(1.0 * roi.Top, roi.Left, roi.Bottom, roi.Right); m_hoRectangle.AreaCenter(out m_roiRow, out m_roiColumn); hImage.GenEmptyObj(); //hImage = VisionProcess.Bitmap2HImage_8(srcImage); hImage.FindNccModel(m_listPartModelID[modelID].ModelID, 0, 0, 0.6, 1, 0.5, "true", m_iNumLevels, out row, out column, out angle, out score); if (score > 0.1) { HOperatorSet.VectorAngleToRigid(m_roiRow, m_roiColumn, new HTuple(0), row, column, new HTuple(0), out hv_HomMat2D); HRegion affRoi = m_hoRectangle.AffineTransRegion(new HHomMat2D(hv_HomMat2D), "false"); int right, bottom; affRoi.SmallestRectangle1(out result.Y, out result.X, out bottom, out right); result.Width = right - result.X; result.Height = bottom - result.Y; result.Score = 100 * score.D; bIsSuccess = true; } GC.Collect(); } catch { srcImage.Save("FindNccModelException_" + partName, System.Drawing.Imaging.ImageFormat.Bmp); //LogFile.AppendText("FindNccModel函数Exception:"+ex.Message.ToString()); //System.Windows.MessageBox.Show(ex.Message.ToString()); bIsSuccess = false; } return(bIsSuccess); }
public void FindShapeModel(HImage Img, HWindow window, double row1, double col1, double row2, double col2, double rowMark, double colMark) { double S1, S2; HTuple RowCheck, ColumnCheck, AngleCheck, Score; HHomMat2D Matrix = new HHomMat2D(); HRegion ModelRegionTrans; HTuple row1Check, col1Check; HTuple row2Check, col2Check; HRegion Rectangle1 = new HRegion(); //HRegion Rectangle2 = new HRegion(); //HMeasure Measure1, Measure2; //HTuple RowEdgeFirst1, ColumnEdgeFirst1; //HTuple AmplitudeFirst1, RowEdgeSecond1; //HTuple ColumnEdgeSecond1, AmplitudeSecond1; //HTuple IntraDistance1, InterDistance1; //HTuple RowEdgeFirst2, ColumnEdgeFirst2; //HTuple AmplitudeFirst2, RowEdgeSecond2; //HTuple ColumnEdgeSecond2, AmplitudeSecond2; //HTuple IntraDistance2, InterDistance2; //HTuple MinDistance; //int NumLeads; //HSystem.SetSystem("flush_graphic", "false"); //Img.GrabImage(Framegrabber); //Img.DispObj(Window); // Find the IC in the current image. S1 = HSystem.CountSeconds(); ShapeModel.FindShapeModel(Img, 0, new HTuple(360).TupleRad().D, 0.7, 1, 0.5, "least_squares", 4, 0.9, out RowCheck, out ColumnCheck, out AngleCheck, out Score); S2 = HSystem.CountSeconds(); if (RowCheck.Length == 1) { //MatchingScoreLabel.Text = "Score: " + // String.Format("{0:F5}", Score.D); // Rotate the model for visualization purposes. //创建严格的仿射变换VectorAngleToRigid Matrix.VectorAngleToRigid(new HTuple((row1 + row2) / 2), new HTuple((col1 + col2) / 2), new HTuple(0.0), RowCheck - rowMark + rowOrg, ColumnCheck - colMark + colOrg, AngleCheck); //根据Matrix变换ModelRegion ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false"); window.SetColor("red"); window.SetDraw("margin"); window.SetLineWidth(2); ModelRegionTrans.DispObj(window); // Compute the parameters of the measurement rectangles. Matrix.AffineTransPixel(new HTuple(row1), new HTuple(col1), out row1Check, out col1Check); Matrix.AffineTransPixel(new HTuple(row2), new HTuple(col2), out row2Check, out col2Check); Rectangle1.GenRectangle1(row1Check, col1Check, row2Check, col2Check); window.SetColor("green"); window.SetDraw("margin"); window.SetLineWidth(1); Rectangle1.DispObj(window); window.SetColor("gold"); window.DispLine(RowCheck - 15, ColumnCheck, RowCheck + 15, ColumnCheck); window.DispLine(RowCheck, ColumnCheck - 15, RowCheck, ColumnCheck + 45); // For visualization purposes, generate the two rectangles as // regions and display them. //Rectangle1.GenRectangle2(Rect1RowCheck.D, Rect1ColCheck.D, // RectPhi + AngleCheck.D, // RectLength1, RectLength2); //Rectangle2.GenRectangle2(Rect2RowCheck.D, Rect2ColCheck.D, // RectPhi + AngleCheck.D, // RectLength1, RectLength2); //window.SetColor("blue"); //window.SetDraw("margin"); //Rectangle1.DispObj(window); //Rectangle2.DispObj(window); } //MatchingTimeLabel.Text = "Time: " + // String.Format("{0,4:F1}", (S2 - S1)*1000) + "ms"; //MatchingScoreLabel.Text = "Score: "; //{ // MatchingScoreLabel.Text = "Score: " + // String.Format("{0:F5}", Score.D); // // Rotate the model for visualization purposes. // Matrix.VectorAngleToRigid(new HTuple(Row), new HTuple(Column), new HTuple(0.0), // RowCheck, ColumnCheck, AngleCheck); // ModelRegionTrans = ModelRegion.AffineTransRegion(Matrix, "false"); //window.SetColor("green"); //window.SetDraw("fill"); ////ModelRegionTrans.DispObj(window); }