using System; using HalconDotNet; namespace HDisplay.ViewROI { /// /// This class demonstrates one of the possible implementations for a /// (simple) rectangularly shaped ROI. To create this rectangle we use /// a center point (midR, midC), an orientation 'phi' and the half /// edge lengths 'length1' and 'length2', similar to the HALCON /// operator gen_rectangle2(). /// The class ROIRectangle2 inherits from the base class ROI and /// implements (besides other auxiliary methods) all virtual methods /// defined in ROI.cs. /// public class ROIRectangle2 : ROI { /// Half length of the rectangle side, perpendicular to phi private double length1; /// Half length of the rectangle side, in direction of phi private double length2; /// Row coordinate of midpoint of the rectangle private double midR; /// Column coordinate of midpoint of the rectangle private double midC; /// Orientation of rectangle defined in radians. private double phi; //auxiliary variables HTuple rowsInit; HTuple colsInit; HTuple rows; HTuple cols; HHomMat2D hom2D, tmp; public double Length1 { get => length1; set => Set(ref length1, value); } public double Length2 { get => length2; set => Set(ref length2, value); } public double MidR { get => midR; set => Set(ref midR, value); } public double MidC { get => midC; set => Set(ref midC, value); } public double Phi { get => phi; set => Set(ref phi, value); } /// Constructor public ROIRectangle2() { NumHandles = 6; // 4 corners + 1 midpoint + 1 rotationpoint activeHandleIdx = 4; } /// Creates a new ROI instance at the mouse position /// /// x (=column) coordinate for interactive ROI /// /// /// y (=row) coordinate for interactive ROI /// public override void createROI(double midX, double midY) { midR = midY; midC = midX; length1 = 100; length2 = 50; phi = 0.0; rowsInit = new HTuple(new double[] {-1.0, -1.0, 1.0, 1.0, 0.0, 0.0 }); colsInit = new HTuple(new double[] {-1.0, 1.0, 1.0, -1.0, 0.0, 0.6 }); //order ul , ur, lr, ll, mp, arrowMidpoint hom2D = new HHomMat2D(); tmp = new HHomMat2D(); updateHandlePos(); } public void SetRoi(double row, double col, double r, double len1, double len2) { midR = row; midC = col; length1 = len1; length2 = len2; phi = r; updateHandlePos(); } /// Paints the ROI into the supplied window /// HALCON window public override void draw(HalconDotNet.HWindow window) { window.DispRectangle2(midR, midC, -phi, length1, length2); for (int i =0; i < NumHandles; i++) window.DispRectangle2(rows[i].D, cols[i].D, -phi, 5, 5); window.DispArrow(midR, midC, midR + (Math.Sin(phi) * length1 * 1.2), midC + (Math.Cos(phi) * length1 * 1.2), 2.0); } /// /// Returns the distance of the ROI handle being /// closest to the image point(x,y) /// /// x (=column) coordinate /// y (=row) coordinate /// /// Distance of the closest ROI handle. /// public override double distToClosestHandle(double x, double y) { double max = 10000; double [] val = new double[NumHandles]; for (int i=0; i < NumHandles; i++) val[i] = HMisc.DistancePp(y, x, rows[i].D, cols[i].D); for (int i=0; i < NumHandles; i++) { if (val[i] < max) { max = val[i]; activeHandleIdx = i; } } return val[activeHandleIdx]; } /// /// Paints the active handle of the ROI object into the supplied window /// /// HALCON window public override void displayActive(HalconDotNet.HWindow window) { window.DispRectangle2(rows[activeHandleIdx].D, cols[activeHandleIdx].D, -phi, 5, 5); if (activeHandleIdx == 5) window.DispArrow(midR, midC, midR + (Math.Sin(phi) * length1 * 1.2), midC + (Math.Cos(phi) * length1 * 1.2), 2.0); } /// Gets the HALCON region described by the ROI public override HRegion getRegion() { HRegion region = new HRegion(); region.GenRectangle2(midR, midC, -phi, length1, length2); return region; } /// /// Gets the model information described by /// the interactive ROI /// public override HTuple getModelData() { return new HTuple(new double[] { midR, midC, phi, length1, length2 }); } /// /// Recalculates the shape of the ROI instance. Translation is /// performed at the active handle of the ROI object /// for the image coordinate (x,y) /// /// x mouse coordinate /// y mouse coordinate public override void moveByHandle(double newX, double newY) { double vX, vY, x=0, y=0; switch (activeHandleIdx) { case 0: case 1: case 2: case 3: tmp = hom2D.HomMat2dInvert(); x = tmp.AffineTransPoint2d(newX, newY, out y); length2 = Math.Abs(y); length1 = Math.Abs(x); checkForRange(x, y); break; case 4: midC = newX; midR = newY; break; case 5: vY = newY - rows[4].D; vX = newX - cols[4].D; phi = Math.Atan2(vY, vX); break; } updateHandlePos(); }//end of method /// /// Auxiliary method to recalculate the contour points of /// the rectangle by transforming the initial row and /// column coordinates (rowsInit, colsInit) by the updated /// homography hom2D /// private void updateHandlePos() { hom2D.HomMat2dIdentity(); hom2D = hom2D.HomMat2dTranslate(midC, midR); hom2D = hom2D.HomMat2dRotateLocal(phi); tmp = hom2D.HomMat2dScaleLocal(length1, length2); cols = tmp.AffineTransPoint2d(colsInit, rowsInit, out rows); } /* This auxiliary method checks the half lengths * (length1, length2) using the coordinates (x,y) of the four * rectangle corners (handles 0 to 3) to avoid 'bending' of * the rectangular ROI at its midpoint, when it comes to a * 'collapse' of the rectangle for length1=length2=0. * */ private void checkForRange(double x, double y) { switch (activeHandleIdx) { case 0: if ((x < 0) && (y < 0)) return; if (x >= 0) length1 = 0.01; if (y >= 0) length2 = 0.01; break; case 1: if ((x > 0) && (y < 0)) return; if (x <= 0) length1 = 0.01; if (y >= 0) length2 = 0.01; break; case 2: if ((x > 0) && (y > 0)) return; if (x <= 0) length1 = 0.01; if (y <= 0) length2 = 0.01; break; case 3: if ((x < 0) && (y > 0)) return; if (x >= 0) length1 = 0.01; if (y <= 0) length2 = 0.01; break; default: break; } } }//end of class }//end of namespace