Example #1
0
 internal static extern IntPtr cvInitImageHeader(
     IplImage image,
     Size size,
     IplDepth depth,
     int channels,
     IplOrigin origin = IplOrigin.TopLeft,
     int align        = 4);
Example #2
0
        IplImage CreateEye(int size = 3, double value = EyeValue, IplDepth depth = IplDepth.F64)
        {
            var eye = new IplImage(new Size(size, size), depth, 1);

            CV.SetIdentity(eye, Scalar.All(value));
            return(eye);
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IplImage"/> class with the
        /// specified <paramref name="size"/>, pixel bit <paramref name="depth"/> and
        /// <paramref name="channels"/> per element. A pointer to the image raw
        /// <paramref name="data"/> is provided.
        /// </summary>
        /// <param name="size">The pixel-accurate size of the <see cref="IplImage"/>.</param>
        /// <param name="depth">The bit depth of image pixels.</param>
        /// <param name="channels">The number of channels per pixel.</param>
        /// <param name="data">A pointer to the image raw pixel data.</param>
        public IplImage(Size size, IplDepth depth, int channels, IntPtr data)
            : base(true)
        {
            var pImage = NativeMethods.cvCreateImageHeader(size, depth, channels);

            SetHandle(pImage);
            SetData(data, WidthStep);
        }
Example #4
0
        public static IplImage EnsureImageFormat(IplImage output, Size size, IplDepth depth, int channels)
        {
            if (output == null || output.Size != size || output.Depth != depth || output.Channels != channels)
            {
                if (output != null)
                {
                    output.Close();
                }
                return(new IplImage(size, depth, channels));
            }

            return(output);
        }
Example #5
0
        public static Depth FromIplDepth(IplDepth depth)
        {
            const int ConversionOffset = (int)
                                         (Depth.U8 +
                                          ((int)Depth.U16 << 4) +
                                          ((int)Depth.F32 << 8) +
                                          ((int)Depth.F64 << 16) +
                                          ((int)Depth.S8 << 20) +
                                          ((int)Depth.S16 << 24) +
                                          ((int)Depth.S32 << 28));
            var depthShift = (((int)depth & 0xF0) >> 2) + (unchecked ((int)depth & 0x80000000) != 0 ? 20 : 0);

            return((Depth)((ConversionOffset >> depthShift) & 15));
        }
Example #6
0
        public override IObservable <IplImage> Process(IObservable <IplImage> source)
        {
            return(source.Select(input =>
            {
                if (conversionChanged)
                {
                    depth = Conversion.GetConversionDepth();
                    numChannels = Conversion.GetConversionNumChannels();
                    conversionChanged = false;
                }

                var output = new IplImage(input.Size, depth, numChannels);
                CV.CvtColor(input, output, conversion);
                return output;
            }));
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IplImage"/> class with the
 /// specified <paramref name="size"/>, pixel bit <paramref name="depth"/> and
 /// <paramref name="channels"/> per element.
 /// </summary>
 /// <param name="size">The pixel-accurate size of the <see cref="IplImage"/>.</param>
 /// <param name="depth">The bit depth of image pixels.</param>
 /// <param name="channels">The number of channels per pixel.</param>
 public IplImage(Size size, IplDepth depth, int channels)
     : this(NativeMethods.cvCreateImage(size, depth, channels), true)
 {
 }
Example #8
0
        /// <summary>
        /// 计算两张图片的相似度,取值范围[0,1], 等于1代表完全相同
        /// </summary>
        /// <returns>返回两张图片的相似度</returns>
        public double CalcSSIM()
        {
            if (SSIM[(int)RGBIndex.All] != -1)
            {
                return(SSIM[(int)RGBIndex.All]);
            }

            if (Image1 == null)
            {
                throw new ImageDiffException("image1 can not be null.");
            }
            if (Image2 == null)
            {
                throw new ImageDiffException("image2 can not be null.");
            }

            Image <Bgr, Byte> img1_temp = null;
            Image <Bgr, Byte> img2_temp = null;

            try
            {
                img1_temp = new Image <Bgr, Byte>(Image1);
                img2_temp = new Image <Bgr, Byte>(Image2);
            }
            catch (Exception ex)
            {
                throw new ImageDiffException(ex.Message);
            }

            if (img1_temp.Size != img2_temp.Size || img1_temp.NumberOfChannels != img2_temp.NumberOfChannels)
            {
                throw outOfSizeException;
            }

            int      imageWidth  = img1_temp.Width;
            int      imageHeight = img1_temp.Height;
            int      nChan       = img1_temp.NumberOfChannels;
            IplDepth depth32F    = IplDepth.IplDepth32F;
            Size     imageSize   = new Size(imageWidth, imageHeight);

            Image <Bgr, Single> img1 = img1_temp.ConvertScale <Single>(1.0, 1);
            Image <Bgr, Single> img2 = img2_temp.ConvertScale <Single>(1.0, 1);
            Image <Bgr, Byte>   diff = img2_temp.Copy();

            Image <Bgr, Single> img1_sq   = img1.Pow(2);
            Image <Bgr, Single> img2_sq   = img2.Pow(2);
            Image <Bgr, Single> img1_img2 = img1.Mul(img2);

            Image <Bgr, Single> mu1 = img1.SmoothGaussian(11, 11, 1.5, 0);
            Image <Bgr, Single> mu2 = img2.SmoothGaussian(11, 11, 1.5, 0);

            Image <Bgr, Single> mu1_sq  = mu1.Pow(2);
            Image <Bgr, Single> mu2_sq  = mu2.Pow(2);
            Image <Bgr, Single> mu1_mu2 = mu1.Mul(mu2);

            Image <Bgr, Single> sigma1_sq = img1_sq.SmoothGaussian(11, 11, 1.5, 0);

            sigma1_sq = sigma1_sq.AddWeighted(mu1_sq, 1, -1, 0);

            Image <Bgr, Single> sigma2_sq = img2_sq.SmoothGaussian(11, 11, 1.5, 0);

            sigma2_sq = sigma2_sq.AddWeighted(mu2_sq, 1, -1, 0);

            Image <Bgr, Single> sigma12 = img1_img2.SmoothGaussian(11, 11, 1.5, 0);

            sigma12 = sigma12.AddWeighted(mu1_mu2, 1, -1, 0);

            // (2*mu1_mu2 + C1)
            Image <Bgr, Single> temp1 = mu1_mu2.ConvertScale <Single>(2, 0);

            temp1 = temp1.Add(new Bgr(C1, C1, C1));

            // (2*sigma12 + C2)
            Image <Bgr, Single> temp2 = sigma12.ConvertScale <Single>(2, 0);

            temp2 = temp2.Add(new Bgr(C2, C2, C2));

            // ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
            Image <Bgr, Single> temp3 = temp1.Mul(temp2);

            // (mu1_sq + mu2_sq + C1)
            temp1 = mu1_sq.Add(mu2_sq);
            temp1 = temp1.Add(new Bgr(C1, C1, C1));

            // (sigma1_sq + sigma2_sq + C2)
            temp2 = sigma1_sq.Add(sigma2_sq);
            temp2 = temp2.Add(new Bgr(C2, C2, C2));

            // ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
            temp1 = temp1.Mul(temp2, 1);

            // ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
            Image <Bgr, Single> ssim_map = new Image <Bgr, float>(imageSize);

            CvInvoke.Divide(temp3, temp1, ssim_map);

            Bgr       avg = new Bgr();
            MCvScalar sdv = new MCvScalar();

            ssim_map.AvgSdv(out avg, out sdv);

            SSIM[(int)RGBIndex.Red]   = avg.Red;
            SSIM[(int)RGBIndex.Green] = avg.Green;
            SSIM[(int)RGBIndex.Blue]  = avg.Blue;
            SSIM[(int)RGBIndex.All]   = (avg.Red + avg.Green + avg.Blue) / 3.0;

            if (SSIM[(int)RGBIndex.All] == 1)            //Same Image
            {
                NumDifferences = 0;
                return(SSIM[(int)RGBIndex.All]);
            }

            Image <Gray, Single> gray32 = new Image <Gray, float>(imageSize);

            CvInvoke.CvtColor(ssim_map, gray32, ColorConversion.Bgr2Gray);

            Image <Gray, Byte> gray8 = gray32.ConvertScale <Byte>(255, 0);
            Image <Gray, Byte> gray1 = gray8.ThresholdBinaryInv(new Gray(254), new Gray(255));

            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();

            CvInvoke.FindContours(gray1, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);

            NumDifferences = contours.Size;
            if (ImageDifferent == null)
            {
                return(SSIM[(int)RGBIndex.All]);
            }

            for (int i = 0; i < NumDifferences; i++)
            {
                using (VectorOfPoint contour = contours[i])
                {
                    Rectangle rect = CvInvoke.BoundingRectangle(contour);
                    diff.Draw(rect, new Bgr(RectColor), Thcikness);
                }
            }

            try
            {
                diff.Save(ImageDifferent);
            }
            catch (Exception ex)
            {
                throw new ImageDiffException(ex.Message);
            }
            return(SSIM[(int)RGBIndex.All]);
        }
Example #9
0
 private static extern IntPtr cveInitImageHeader(IntPtr image, ref Size size, IplDepth depth, int channels, int origin, int align);
Example #10
0
 private static extern IntPtr cveCreateImageHeader(ref Size size, IplDepth depth, int channels);
Example #11
0
 public static IntPtr cvInitImageHeader(IntPtr image, Size size, IplDepth depth, int channels, int origin = 0, int align = 4)
 {
     return cveInitImageHeader(image, ref size, depth, channels, origin, align);
 }
Example #12
0
 public static IntPtr cvCreateImageHeader(Size size, IplDepth depth, int channels)
 {
     return cveCreateImageHeader(ref size, depth, channels);
 }
Example #13
0
        static void GetImageDepth(SapFormat format, out IplDepth depth, out int channels, out SapFormat outputFormat)
        {
            switch (format)
            {
            case SapFormat.Binary:
            case SapFormat.Mono8:
                channels     = 1;
                depth        = IplDepth.U8;
                outputFormat = SapFormat.Mono8;
                break;

            case SapFormat.RGB888:
                channels     = 3;
                depth        = IplDepth.U8;
                outputFormat = SapFormat.RGB888;
                break;

            case SapFormat.AYU2:
            case SapFormat.BICOLOR1616:
            case SapFormat.BICOLOR88:
            case SapFormat.ColorI10:
            case SapFormat.ColorI11:
            case SapFormat.ColorI12:
            case SapFormat.ColorI13:
            case SapFormat.ColorI14:
            case SapFormat.ColorI15:
            case SapFormat.ColorI16:
            case SapFormat.ColorI8:
            case SapFormat.ColorI9:
            case SapFormat.ColorNI10:
            case SapFormat.ColorNI11:
            case SapFormat.ColorNI12:
            case SapFormat.ColorNI13:
            case SapFormat.ColorNI14:
            case SapFormat.ColorNI15:
            case SapFormat.ColorNI16:
            case SapFormat.ColorNI8:
            case SapFormat.ColorNI9:
            case SapFormat.Complex:
            case SapFormat.FPoint:
            case SapFormat.Float:
            case SapFormat.HSI:
            case SapFormat.HSIP16:
            case SapFormat.HSIP8:
            case SapFormat.HSV:
            case SapFormat.HSVP16:
            case SapFormat.HSVP8:
            case SapFormat.IYU1:
            case SapFormat.IYU2:
            case SapFormat.Int10:
            case SapFormat.Int11:
            case SapFormat.Int12:
            case SapFormat.Int13:
            case SapFormat.Int14:
            case SapFormat.Int15:
            case SapFormat.Int16:
            case SapFormat.Int24:
            case SapFormat.Int32:
            case SapFormat.Int64:
            case SapFormat.Int8:
            case SapFormat.Int9:
            case SapFormat.LAB:
            case SapFormat.LAB101010:
            case SapFormat.LAB16161616:
            case SapFormat.Mono10:
            case SapFormat.Mono11:
            case SapFormat.Mono12:
            case SapFormat.Mono13:
            case SapFormat.Mono14:
            case SapFormat.Mono15:
            case SapFormat.Mono16:
            case SapFormat.Mono16P2:
            case SapFormat.Mono16P3:
            case SapFormat.Mono16P4:
            case SapFormat.Mono24:
            case SapFormat.Mono32:
            case SapFormat.Mono64:
            case SapFormat.Mono8P2:
            case SapFormat.Mono8P3:
            case SapFormat.Mono8P4:
            case SapFormat.Mono9:
            case SapFormat.Point:
            case SapFormat.RGB101010:
            case SapFormat.RGB161616:
            case SapFormat.RGB16161616:
            case SapFormat.RGB161616_MONO16:
            case SapFormat.RGB5551:
            case SapFormat.RGB565:
            case SapFormat.RGB8888:
            case SapFormat.RGB888_MONO8:
            case SapFormat.RGBP16:
            case SapFormat.RGBP8:
            case SapFormat.RGBR888:
            case SapFormat.UYVY:
            case SapFormat.Unknown:
            case SapFormat.Y211:
            case SapFormat.YUVP16:
            case SapFormat.YUVP8:
            case SapFormat.YUY2:
            case SapFormat.YUYV:
            case SapFormat.YVYU:
            default: throw new ArgumentException("Unsupported pixel type.", "format");
            }
        }
Example #14
0
 public static IntPtr cvInitImageHeader(IntPtr image, Size size, IplDepth depth, int channels, int origin = 0, int align = 4)
 {
     return(cveInitImageHeader(image, ref size, depth, channels, origin, align));
 }
Example #15
0
 public static IntPtr cvCreateImageHeader(Size size, IplDepth depth, int channels)
 {
     return(cveCreateImageHeader(ref size, depth, channels));
 }
Example #16
0
        static void GetImageDepth(PixelType pixelType, out IplDepth depth, out int channels, out PixelType outputFormat)
        {
            switch (pixelType)
            {
            case PixelType.BGR10V1packed:
            case PixelType.BGR10V2packed:
            case PixelType.BGR10packed:
            case PixelType.BGR12packed:
            case PixelType.BGR8packed:
            case PixelType.BGRA8packed:
                outputFormat = PixelType.BGR8packed;
                depth        = IplDepth.U8;
                channels     = 3;
                break;

            case PixelType.BayerBG10:
            case PixelType.BayerBG10pp:
            case PixelType.BayerBG12:
            case PixelType.BayerBG12Packed:
            case PixelType.BayerBG12p:
            case PixelType.BayerBG16:
            case PixelType.BayerBG8:
                outputFormat = PixelType.BGR8packed;
                depth        = IplDepth.U8;
                channels     = 3;
                break;

            case PixelType.BayerGB10:
            case PixelType.BayerGB10pp:
            case PixelType.BayerGB12:
            case PixelType.BayerGB12Packed:
            case PixelType.BayerGB12p:
            case PixelType.BayerGB16:
            case PixelType.BayerGB8:
                outputFormat = PixelType.BGR8packed;
                depth        = IplDepth.U8;
                channels     = 3;
                break;

            case PixelType.BayerGR10:
            case PixelType.BayerGR10pp:
            case PixelType.BayerGR12:
            case PixelType.BayerGR12Packed:
            case PixelType.BayerGR12p:
            case PixelType.BayerGR16:
            case PixelType.BayerGR8:
                outputFormat = PixelType.BGR8packed;
                depth        = IplDepth.U8;
                channels     = 3;
                break;

            case PixelType.BayerRG10:
            case PixelType.BayerRG10pp:
            case PixelType.BayerRG12:
            case PixelType.BayerRG12Packed:
            case PixelType.BayerRG12p:
            case PixelType.BayerRG16:
            case PixelType.BayerRG8:
                outputFormat = PixelType.BGR8packed;
                depth        = IplDepth.U8;
                channels     = 3;
                break;

            case PixelType.Double:
                outputFormat = PixelType.Double;
                depth        = IplDepth.F64;
                channels     = 1;
                break;

            case PixelType.Mono10:
            case PixelType.Mono10p:
            case PixelType.Mono10packed:
            case PixelType.Mono12:
            case PixelType.Mono12p:
            case PixelType.Mono12packed:
            case PixelType.Mono16:
                outputFormat = PixelType.Mono16;
                depth        = IplDepth.U16;
                channels     = 1;
                break;

            case PixelType.Mono1packed:
            case PixelType.Mono2packed:
            case PixelType.Mono4packed:
            case PixelType.Mono8:
            case PixelType.Mono8signed:
                outputFormat = PixelType.Mono8;
                depth        = IplDepth.U8;
                channels     = 1;
                break;

            case PixelType.RGB10packed:
            case PixelType.RGB10planar:
            case PixelType.RGB12V1packed:
            case PixelType.RGB12packed:
            case PixelType.RGB12planar:
            case PixelType.RGB16packed:
            case PixelType.RGB16planar:
            case PixelType.RGB8packed:
            case PixelType.RGB8planar:
            case PixelType.RGBA8packed:
                outputFormat = PixelType.BGR8packed;
                depth        = IplDepth.U8;
                channels     = 3;
                break;

            case PixelType.YUV411packed:
            case PixelType.YUV422_YUYV_Packed:
            case PixelType.YUV422packed:
            case PixelType.YUV444packed:
                outputFormat = PixelType.BGR8packed;
                depth        = IplDepth.U8;
                channels     = 3;
                break;

            case PixelType.Undefined:
            default: throw new CaptureException("Undefined pixel type.");
            }
        }
Example #17
0
 internal static extern IntPtr cvCreateImage(Size size, IplDepth depth, int channels);
Example #18
0
 private static extern IntPtr cveInitImageHeader(IntPtr image, ref Size size, IplDepth depth, int channels, int origin, int align);
Example #19
0
 private static extern IntPtr cveCreateImageHeader(ref Size size, IplDepth depth, int channels);