示例#1
0
        // 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);
            }
        }
示例#2
0
        private static void IVA_Mask_From_ROI(VisionImage image, Roi roi, bool invertMask, bool extractRegion)
        {
            using (VisionImage imageMask = new VisionImage(ImageType.U8, 7))
            {
                PixelValue fillValue = new PixelValue(255);

                // Transforms the region of interest into a mask image.
                Algorithms.RoiToMask(imageMask, roi, fillValue, image);

                if (invertMask)
                {
                    // Inverts the mask image.
                    Algorithms.Xor(imageMask, fillValue, imageMask);
                }

                // Masks the input image using the mask image we just created.
                Algorithms.Mask(image, image, imageMask);

                if (extractRegion)
                {
                    // Extracts the bounding box.
                    Algorithms.Extract(image, image, roi.GetBoundingRectangle());
                }
            }
        }
示例#3
0
        public static void LearnPattern(ImageViewer SourceImage, float fUpper = 0, float fLower = 0)
        {
            using (VisionImage plane = new VisionImage(ImageType.U8, 7))
            {
                // Extract the green color plane and copy it to the main image.
                if (SourceImage.Image.Type == ImageType.Rgb32)
                {
                    Algorithms.ExtractColorPlanes(SourceImage.Image, NationalInstruments.Vision.ColorMode.Rgb, null, plane, null);
                    Algorithms.Copy(plane, SourceImage.Image);
                }
            }
//          Algorithms.LearnPattern2(SourceImage.Image);
            OvalContour vaRect2 = new OvalContour(0, 0, 0, 0);
            Roi         roi     = new Roi();

            roi.Add(vaRect2);
            // Histogram Grayscale
            using (VisionImage imageMask = new VisionImage(ImageType.U8, 7))
            {
                RotationAngleRange ra        = new RotationAngleRange(fLower, fUpper);
                PixelValue         fillValue = new PixelValue(255);
                Algorithms.RoiToMask(imageMask, roi, fillValue, SourceImage.Image);
                Algorithms.LearnPattern2(SourceImage.Image, imageMask, MatchingAlgorithm.MatchGrayValuePyramid, ra);
            }
            roi.Dispose();
        }
        private PixelValue SetPixelValue(ImageType type, float grayConstant, byte redConstant, byte greenConstant, byte buleConstant)
        {
            PixelValue val = new PixelValue();

            switch (type)
            {
            case ImageType.U8:
            case ImageType.U16:
            case ImageType.I16:
                val = new PixelValue(grayConstant);
                break;

            case ImageType.Complex:
                val = new PixelValue(new Complex());
                break;

            case ImageType.Rgb32:
                val = new PixelValue(new Rgb32Value(redConstant, greenConstant, buleConstant));
                break;

            case ImageType.Hsl32:
                val = new PixelValue(new Hsl32Value());
                break;
            }

            return(val);
        }
        /////////////////////////////////////////////////////////////////////////////////
        ///
        /// Function Name: IVA_SetPixelValue
        ///
        /// Description  : Initialize a PixelValue structure
        ///
        /// Parameters   : imageType          -  image type
        ///                grayscaleConstant
        ///                redConstant
        ///                greenConstant
        ///                blueConstant
        ///
        /// Return Value : a PixelValue with the specified parameters
        ///
        /////////////////////////////////////////////////////////////////////////////////
        public static PixelValue IVA_SetPixelValue(ImageType imageType,
                                                   float grayscaleConstant,
                                                   byte redConstant,
                                                   byte greenConstant,
                                                   byte blueConstant)
        {
            PixelValue pVal;

            /// Sets the pixel value.
            switch (imageType)
            {
            case ImageType.U8:
            case ImageType.I16:
            case ImageType.U16:
            case ImageType.Single:
                pVal = new PixelValue(grayscaleConstant);
                break;

            case ImageType.Complex:
                pVal = new PixelValue(new Complex());
                break;

            case ImageType.Rgb32:
                pVal = new PixelValue(new Rgb32Value(redConstant, greenConstant, blueConstant));
                break;

            case ImageType.RgbU64:
                pVal = new PixelValue(new RgbU64Value(redConstant, greenConstant, blueConstant));
                break;

            case ImageType.Hsl32:
                pVal = new PixelValue(new Hsl32Value());
                break;

            default:
                pVal = new PixelValue();
                break;
            }
            return(pVal);
        }
示例#6
0
        public static double FindCircleHistogramMean(VisionImage image, OvalContour vaRect2)
        {
            Roi roi = new Roi();

            // Creates a new RectangleContour using the given values.

            roi.Add(vaRect2);
            // Histogram Grayscale
            double HistogramMean = -100;

            using (VisionImage imageMask = new VisionImage(ImageType.U8, 7))
            {
                PixelValue fillValue     = new PixelValue(255);
                Range      intervalRange = new Range(0, 0);

                Algorithms.RoiToMask(imageMask, roi, fillValue, image);

                // Calculates and returns statistical parameters on the image.
                HistogramMean = Algorithms.Histogram(image, 256, intervalRange, imageMask).Mean;
            }
            roi.Dispose();
            return(HistogramMean);
        }
        public override VisionResult Detected(VisionImage image, Dictionary <string, VisionResult> Result = null, VisionFlow parent = null, Shape newRoi = null)
        {
            VisionResult rtn = new VisionResult();

            rtn.State = VisionResultState.WaitCal;
            try
            {
                image.Type = ImageType.U16;
                PixelValue va1 = this.SetPixelValue(image.Type, (float)this.Gain, 0, 0, 0);
                Algorithms.Multiply(image, va1, image);
                PixelValue va2 = this.SetPixelValue(image.Type, (float)this.Offset, 0, 0, 0);
                Algorithms.Subtract(image, va2, image);
                Algorithms.Cast(image, image, ImageType.U8, -1);
                this.AddVisionResc(rtn, "增强成功");
                rtn.State = VisionResultState.OK;
            }
            catch (Exception ex)
            {
                this.AddVisionResc(rtn, ex.Message);
                rtn.State = VisionResultState.NG;
            }

            return(rtn);
        }
示例#8
0
 public static void ClearScreen(PixelValue value = PixelValue.Clear)
 {
     Memory.Write(ClearScreenAddress, (int)value);
 }