// imaqDraw*OnImage uses this weird format for passing colors. // Most things are grayscale, RGB is 0xAABBGGRR, and HSL is 0xAABBGGRR as well, // meaning you have to convert it to an RGB value first. public static float ConvertPixelValueToFloat(PixelValue value, ImageType type) { switch (type) { case ImageType.U8: case ImageType.I16: case ImageType.U16: case ImageType.Single: return(value.Grayscale); case ImageType.Rgb32: Rgb32Value rgbValue = value.Rgb32; return(rgbValue.Blue << 16 | rgbValue.Green << 8 | rgbValue.Red); case ImageType.Hsl32: Hsl32Value hslValue = value.Hsl32; // We need to convert this to a Rgb32 first. CVI_Color2 hslColor = new CVI_Color2(); hslColor.Hsl = hslValue; CVI_Color2 result = VisionDllCommon.imaqChangeColorSpace2(ref hslColor, ColorMode.Hsl, ColorMode.Rgb, 0.0, IntPtr.Zero); Rgb32Value rgbToConvert = result.Rgb; return(rgbToConvert.Blue << 16 | rgbToConvert.Green << 8 | rgbToConvert.Red); default: // Don't DebugAssert here since the user could get here by passing an invalid image type. //Debug.Fail("Unexpected image type " + type + " passed to ConvertPixelValueToFloat!"); return(0.0F); } }
public static void IVA_OverlayLine(VisionImage image, int[] xCoordinates, int[] yCoordinates, byte[] colors) { Rgb32Value color = new Rgb32Value(colors[0], colors[1], colors[2]); PointContour startPoint = new PointContour(xCoordinates[0], yCoordinates[0]); PointContour endPoint = new PointContour(xCoordinates[1], yCoordinates[1]); image.Overlays.Default.AddLine(new LineContour(startPoint, endPoint), color); }
// Calculates the distance between the two color values in the CIE colorspace. private void CalculateColorDistance(Rgb32Value rgb32Value1, Rgb32Value rgb32Value2) { // Transform to the correct colorspace. CieLabValue cieLabValue1 = Algorithms.ConvertColorValue(new ColorValue(rgb32Value1), ColorMode.CieLab).CieLab; CieLabValue cieLabValue2 = Algorithms.ConvertColorValue(new ColorValue(rgb32Value2), ColorMode.CieLab).CieLab; // Calculate distance in CIE. double distance = Math.Pow(cieLabValue1.L - cieLabValue2.L, 2) + Math.Pow(cieLabValue1.A - cieLabValue2.A, 2) + Math.Pow(cieLabValue1.B - cieLabValue2.B, 2); distance = Math.Sqrt(distance); distanceTextBox.Text = String.Format("{0:0.00}", distance); }
public void showCross(double angle, PointContour center, Rgb32Value colcor) { double rate = angle / 180 * Math.PI; LineContour line1 = new LineContour(new PointContour(center.X - 2000 * Math.Sin(rate), center.Y - 2000 * Math.Cos(rate)), new PointContour(center.X + 2000 * Math.Sin(rate), center.Y + 2000 * Math.Cos(rate))); LineContour line2 = new LineContour(new PointContour(center.X + 2000 * Math.Cos(rate), center.Y - 2000 * Math.Sin(rate)), new PointContour(center.X - 2000 * Math.Cos(rate), center.Y + 2000 * Math.Sin(rate))); this.imageSet.Image.Overlays.Default.AddLine(line1, colcor); this.imageSet.Image.Overlays.Default.AddLine(line2, colcor); }
// Overlay an arrowhead at the end of a line segment. private void DrawArrow(Overlay overlay, LineContour line, Rgb32Value color) { // This code computes the position of the arrow. double dX = line.End.X - line.Start.X; double dY = line.End.Y - line.Start.Y; double lineAngle = Math.Atan2(dY, dX); // The arrow has 3 points. Collection <PointContour> arrowPoints = new Collection <PointContour>(); arrowPoints.Add(line.End); arrowPoints.Add(new PointContour(line.End.X - 6 * Math.Cos(lineAngle - .35), line.End.Y - 6 * Math.Sin(lineAngle - .35))); arrowPoints.Add(new PointContour(line.End.X - 6 * Math.Cos(lineAngle + .35), line.End.Y - 6 * Math.Sin(lineAngle + .35))); overlay.AddPolygon(new PolygonContour(arrowPoints), color, DrawingMode.PaintValue); }
// DisplayColorDistance plots two points in the CIE colorspace and draws // a line between these two points. private void DisplayColorDistance() { // Only handle two points. if (imageViewer1.Roi.Count != 2) { return; } // Get pixel values. Rgb32Value rgb32Value1 = imageViewer1.Image.GetPixel(imageViewer1.Roi[0].Shape as PointContour).Rgb32; Rgb32Value rgb32Value2 = imageViewer1.Image.GetPixel(imageViewer1.Roi[1].Shape as PointContour).Rgb32; // Remove any previous overlays. imageViewer2.Image.Overlays.Default.Clear(); // Transform to the correct colorspace. CieXyzValue cieXyzValue1 = Algorithms.ConvertColorValue(new ColorValue(rgb32Value1), ColorMode.CieXyz).CieXyz; CieXyzValue cieXyzValue2 = Algorithms.ConvertColorValue(new ColorValue(rgb32Value2), ColorMode.CieXyz).CieXyz; // Calculate points. double sum1 = cieXyzValue1.X + cieXyzValue1.Y + cieXyzValue1.Z; if (sum1 == 0.0) { sum1 = 1; } PointContour point1 = new PointContour(cieXyzValue1.Y / sum1 * 255, cieXyzValue1.X / sum1 * 255); double sum2 = cieXyzValue2.X + cieXyzValue2.Y + cieXyzValue2.Z; if (sum2 == 0.0) { sum2 = 1; } PointContour point2 = new PointContour(cieXyzValue2.Y / sum2 * 255, cieXyzValue2.X / sum2 * 255); // Display ovals around the points to make them more visible. imageViewer2.Image.Overlays.Default.AddOval(new OvalContour(point1.X - 5, point1.Y - 5, 10, 10), Rgb32Value.BlackColor); imageViewer2.Image.Overlays.Default.AddOval(new OvalContour(point2.X - 5, point2.Y - 5, 10, 10), Rgb32Value.BlackColor); // Draw a line between the points. imageViewer2.Image.Overlays.Default.AddLine(new LineContour(point1, point2), Rgb32Value.BlackColor); // Calculate distance in CIE CalculateColorDistance(rgb32Value1, rgb32Value2); }
private void timer1_Tick(object sender, EventArgs e) { double[] classCount = new double[8]; OverlayTextOptions textOptions = new OverlayTextOptions("Arial", 14); textOptions.HorizontalAlignment = HorizontalTextAlignment.Center; // Get the next image GetNextImage(imageViewer1.Image); // Process the image VisionImage binaryImage = new VisionImage(); Algorithms.Threshold(imageViewer1.Image, binaryImage, new Range(0, 200)); Algorithms.RejectBorder(binaryImage, binaryImage); Collection <ParticleReport> reports = Algorithms.ParticleReport(binaryImage); foreach (ParticleReport report in reports) { RectangleContour contour = report.BoundingRect; contour.Left -= 10; contour.Top -= 10; contour.Height += 20; contour.Width += 20; // Classify the part ClassifierReport classifierReport = classifier.Classify(binaryImage, new Roi(new Shape[] { contour })); // Display the result if (classifierReport.ClassificationScore > 500) { textOptions.BackgroundColor = Rgb32Value.TransparentColor; int classIndex = 0; Rgb32Value textColor = Rgb32Value.BlackColor; switch (classifierReport.BestClassName) { case "Motor": textColor = Rgb32Value.WhiteColor; classIndex = 0; break; case "Bolt": textColor = new Rgb32Value(Color.Cyan); classIndex = 1; break; case "Screw": textColor = Rgb32Value.RedColor; classIndex = 2; break; case "Gear": textColor = Rgb32Value.BlackColor; classIndex = 3; break; case "Washer": textColor = new Rgb32Value(Color.Magenta); classIndex = 4; break; case "Worm Gear": textColor = Rgb32Value.GreenColor; classIndex = 5; break; case "Bracket": textColor = new Rgb32Value(0x80, 0x80, 0); classIndex = 6; break; } classCount[classIndex]++; imageViewer1.Image.Overlays.Default.AddText(classifierReport.BestClassName, report.CenterOfMass, textColor, textOptions); } else { textOptions.BackgroundColor = Rgb32Value.BlackColor; imageViewer1.Image.Overlays.Default.AddText("Unknown!", report.CenterOfMass, Rgb32Value.RedColor, textOptions); } } classificationGraph1.PlotClassifier(classCount); }