コード例 #1
0
 public void DrawLine(System.Drawing.Point pt1, System.Drawing.Point pt2, System.Drawing.Color color, int thickness, int lineType, int shift)
 {
     // __CvScalar here is come instread CV_RGB
     PInvoke.cvLine(this.Internal, new __CvPoint(pt1.X, pt1.Y), new __CvPoint(pt2.X, pt2.Y),
                    new __CvScalar(color.R, color.G, color.B, 0), thickness, lineType, shift);
     CVUtils.CheckLastError();
 }
コード例 #2
0
ファイル: CVCapture.cs プロジェクト: tdck/opencvdotnet
        public void Open(string filename)
        {
            Release();

            capture.ptr = IntPtr.Zero;
            asImage     = null;

            string ext = System.IO.Path.GetExtension(filename);

            // if the extension of the filename is not AVI, try opening as an image.
            if (ext.ToUpper().CompareTo(".AVI") != 0)
            {
                asImage = new CVImage(filename);
            }
            else
            {
                capture = PInvoke.cvCreateFileCapture(filename);
                CVUtils.CheckLastError();
                if (capture.ptr == IntPtr.Zero)
                {
                    throw new CVException(
                              string.Format("Unable to open file	'{0}' for capture.", filename));
                }
            }

            this.filename = filename;
        }
コード例 #3
0
        ///<summery>
        ///Finds global minimum and maximum in array or subarray.
        ///</summery>
        public static void CVMinMaxLoc(CVImage image,
                                       out double minVal,
                                       out double maxVal,
                                       out System.Drawing.Point minLocation,
                                       out System.Drawing.Point maxLocation,
                                       CVArr mask)
        {
            // Prepare out paramaters:
            __CvPoint min_loc = new __CvPoint();
            __CvPoint max_loc = new __CvPoint();
            double    min_val = -1;
            double    max_val = -1;


            //CvArr tempMask = 0;
            //if (mask != nullptr) {
            //    tempMask = mask->Array;
            //}
            //CVArr tempMask = mask.Array;

            minLocation = new System.Drawing.Point(0, 0);
            maxLocation = new System.Drawing.Point(0, 0);

            // Native call to openCV cvMinMaxLoc:
            PInvoke.cvMinMaxLoc(
                new __CvArrPtr(image),
                out min_val, out max_val,
                out min_loc, out max_loc, new __CvArrPtr(mask));
            CVUtils.CheckLastError();
            minVal      = min_val;
            maxVal      = max_val;
            minLocation = new System.Drawing.Point(min_loc.x, min_loc.y);
            maxLocation = new System.Drawing.Point(max_loc.x, max_loc.y);
        }
コード例 #4
0
        public CVHistogram CalcHistogram(int[] binSizes, CVPair[] binRanges, CVImage mask)
        {
            CVHistogram h = new CVHistogram(binSizes, binRanges);

            __IplImagePtr[] images = new __IplImagePtr[this.Channels];
            if (this.Channels == 1)
            {
                images[0] = this.Internal;
            }
            else
            {
                CVImage[] planes = this.Split();
                for (int i = 0; i < planes.Length; ++i)
                {
                    images[i] = planes[i].Internal;
                }
            }

            __CvArrPtr maskArr = IntPtr.Zero;

            if (mask != null)
            {
                maskArr = mask.Array;
            }

            PInvoke.cvCalcHist(images, h.Internal, 0, maskArr);
            CVUtils.CheckLastError();
            return(h);
        }
コード例 #5
0
        ///<summery>
        /// Compares template against overlapped image regions
        /// <param name="image">
        /// Image where the search is running.
        /// It should be 8-bit or 32-bit floating-point.
        /// </param>
        /// <param name="templ">
        /// Searched template;
        /// must be not greater than the source image
        /// and the same data type as the image.
        /// </param>
        /// <param name="result">
        /// A map of comparison results; single-channel
        /// 32-bit floating-point.
        /// If image is W×H and templ is w×h then result must be W-w+1×H-h+1.
        /// </param>
        /// <param name="method">
        /// Specifies the way the template must be compared with
        /// image regions (see below).
        /// </param>
        /// <remarks>
        /// The function cvMatchTemplate is similiar to
        /// cvCalcBackProjectPatch. It slids through image,
        /// compares overlapped patches of size w×h with templ
        /// using the specified method and stores the comparison results to result. Here are the formula for the different comparison methods one may use (I denotes image, T - template, R - result. The summation is done over template and/or the image patch: x'=0..w-1, y'=0..h-1):
        ///
        /// method=CV_TM_SQDIFF:
        /// R(x,y)=sumx',y'[T(x',y')-I(x+x',y+y')]2

        /// method=CV_TM_SQDIFF_NORMED:
        /// R(x,y)=sumx',y'[T(x',y')-I(x+x',y+y')]2/sqrt[sumx',y'T(x',y')2•sumx',y'I(x+x',y+y')2]

        /// method=CV_TM_CCORR:
        /// R(x,y)=sumx',y'[T(x',y')•I(x+x',y+y')]

        /// method=CV_TM_CCORR_NORMED:
        /// R(x,y)=sumx',y'[T(x',y')•I(x+x',y+y')]/sqrt[sumx',y'T(x',y')2•sumx',y'I(x+x',y+y')2]

        /// method=CV_TM_CCOEFF:
        /// R(x,y)=sumx',y'[T'(x',y')•I'(x+x',y+y')],

        /// where T'(x',y')=T(x',y') - 1/(w•h)•sumx",y"T(x",y")
        ///       I'(x+x',y+y')=I(x+x',y+y') - 1/(w•h)•sumx",y"I(x+x",y+y")

        /// method=CV_TM_CCOEFF_NORMED:
        /// R(x,y)=sumx',y'[T'(x',y')•I'(x+x',y+y')]/sqrt[sumx',y'T'(x',y')2•sumx',y'I'(x+x',y+y')2]

        /// After the function finishes comparison, the best matches can be found as global minimums (CV_TM_SQDIFF*) or maximums (CV_TM_CCORR* and CV_TM_CCOEFF*) using cvMinMaxLoc function. In case of color image and template summation in both numerator and each sum in denominator is done over all the channels (and separate mean values are used for each channel).
        /// </remarks>
        ///</summery>
        public static CVImage MatchTemplate(CVImage image,
                                            CVImage templateToSearch,
                                            TemplateMatchMethod method)
        {
            //specify the size needed by the match function
            int resultW = image.Width - templateToSearch.Width + 1;
            int resultH = image.Height - templateToSearch.Height + 1;

            if (image.Channels > 1)
            {
                throw new CVException("CVMatchTemplate supports only one channel image format.");
            }
            if (!(image.Depth == CVDepth.Depth32F || image.Depth == CVDepth.Depth8U))
            {
                throw new CVException("CVMatchTemplate supports only 32F or 8U image format.");
            }
            if (image.Depth != templateToSearch.Depth || image.Channels != templateToSearch.Channels)
            {
                throw new CVException("image and template should be of the same type format.");
            }

            CVImage result = new CVImage(resultW, resultH, CVDepth.Depth32F, 1);

            // Native call to openCV cvMatchTemplate function:
            PInvoke.cvMatchTemplate(new __CvArrPtr(image), new __CvArrPtr(templateToSearch), new __CvArrPtr(result), (int)method);
            CVUtils.CheckLastError();
            return(result);
        }
コード例 #6
0
        public CVImage CalcBackProject(CVHistogram histogram)
        {
            CVImage[] planes = Split();

            CVImage backProjection =
                new CVImage(
                    planes[0].RegionOfInterest.Width,
                    planes[0].RegionOfInterest.Height,
                    planes[0].Depth,
                    planes[0].Channels);

            __IplImagePtr[] iplImages = new __IplImagePtr[planes.Length];
            for (int i = 0; i < planes.Length; ++i)
            {
                iplImages[i] = planes[i].Internal;
            }

            PInvoke.cvCalcBackProject(iplImages, backProjection.Internal, histogram.Internal);
            CVUtils.CheckLastError();

            for (int i = 0; i < planes.Length; ++i)
            {
                planes[i].Release();
            }

            return(backProjection);
        }
コード例 #7
0
        public CVImage DrawContours()
        {
            CVImage grayscaled = (this.Channels == 1 ? this : this.ToGrayscale());

            __CvMemStoragePtr storage = PInvoke.cvCreateMemStorage(0);
            __CvSeqPtr        first_contour;
            CVImage           result = new CVImage(this.Width, this.Height, CVDepth.Depth8U, 3);

            unsafe
            {
                int num_contours = PInvoke.cvFindContours(
                    grayscaled.Internal,
                    storage,
                    out first_contour,
                    sizeof(__CvContour),
                    CV_RETR.CV_RETR_EXTERNAL,
                    CV_CHAIN.CV_CHAIN_APPROX_SIMPLE,
                    new __CvPoint(0, 0)
                    );

                // Makes an output image and draw contours:
                __CvSeq *cont = first_contour.ToPointer();

                for (; (cont = cont->_cvSequenceFields.__cvTreeNodeFields.h_next.ToPointer()) != null;)
                {
                    PInvoke.cvDrawContours(result.Array, new __CvSeqPtr(cont), new __CvScalar(255, 0, 0), new __CvScalar(0, 0, 0), 0, (int)CVGlobalConsts.CV_FILLED);
                    CVUtils.CheckLastError();
                }
            }

            PInvoke.cvReleaseMemStorage(ref storage);
            CVUtils.CheckLastError();
            return(result);
        }
コード例 #8
0
        public CVConnectedComp CamShift(System.Drawing.Rectangle window, int termCriteria, int maxIterations, double eps)
        {
            __CvConnectedComp cc = new __CvConnectedComp();

            PInvoke.cvCamShift(Internal, new __CvRect(window), PInvoke.cvTermCriteria(termCriteria, maxIterations, eps), ref cc);
            CVUtils.CheckLastError();
            return(new CVConnectedComp(ref cc));
        }
コード例 #9
0
ファイル: CVHistogram.cs プロジェクト: tdck/opencvdotnet
 public void Release()
 {
     if (this.Internal != IntPtr.Zero)
     {
         PInvoke.cvReleaseHist(ref _hist);
         CVUtils.CheckLastError();
     }
 }
コード例 #10
0
        public CVImage Clone()
        {
            CVImage n = new CVImage((__IplImagePtr)IntPtr.Zero);

            n.image = PInvoke.cvCloneImage(this.Internal);
            CVUtils.CheckLastError();
            return(n);
        }
コード例 #11
0
        /// <summary>
        /// Convolves the image with the kernel
        /// </summary>
        public CVImage Filter2D(CVMat kernel, Point anchor)
        {
            CVImage dst = this.Clone();

            PInvoke.cvFilter2D(new __CvArrPtr(this), new __CvArrPtr(dst), new __CvMatPtr(kernel), new __CvPoint(anchor));
            CVUtils.CheckLastError();
            return(dst);
        }
コード例 #12
0
        public CVImage Smooth(SmoothType smoothtype, int param1, int param2, double param3, double param4)
        {
            CVImage dst = this.Clone();

            PInvoke.cvSmooth(new __CvArrPtr(this), new __CvArrPtr(dst), (int)smoothtype, param1, param2, param3, param4);
            CVUtils.CheckLastError();
            return(dst);
        }
コード例 #13
0
        internal CVConnectedComp(ref __CvConnectedComp input)
        {
            area = input.area;

            avgColor = CVUtils.ScalarToColor(input.value);

            rect    = new System.Drawing.Rectangle(input.rect.x, input.rect.y, input.rect.width, input.rect.height);
            contour = input.contour;
        }
コード例 #14
0
ファイル: CVHistogram.cs プロジェクト: tdck/opencvdotnet
 public double this[int idx0]
 {
     get
     {
         float res = PInvoke.cvQueryHistValue_1D(_hist, idx0);
         CVUtils.CheckLastError();
         return(res);
     }
 }
コード例 #15
0
        /// <summary>
        /// Converts input array pixels from one color space to another.
        /// </summary>
        public static CVImage ConvertColorSpace(CVImage image, ColorSpace colorSpace)
        {
            int     numberOfChannels = (colorSpace.ToString().EndsWith("GRAY") ? 1 : 3);
            CVImage dst = new CVImage(image.Width, image.Height, image.Depth, numberOfChannels);

            PInvoke.cvCvtColor(new __CvArrPtr(image), new __CvArrPtr(dst), (int)colorSpace);
            CVUtils.CheckLastError();
            return(dst);
        }
コード例 #16
0
 public void DrawRectangle(System.Drawing.Rectangle rect, System.Drawing.Color color, int thickness, int lineType, int shift)
 {
     PInvoke.cvRectangle(
         image,
         new __CvPoint(rect.Left, rect.Top),
         new __CvPoint(rect.Right, rect.Bottom),
         new __CvScalar(color.R, color.G, color.B, 0), thickness, lineType, shift);
     CVUtils.CheckLastError();
 }
コード例 #17
0
        public void Merge(CVImage blue, CVImage green, CVImage red)
        {
            __IplImagePtr c0 = blue != null ? blue.image : IntPtr.Zero;
            __IplImagePtr c1 = green != null ? green.image : IntPtr.Zero;
            __IplImagePtr c2 = red != null ? red.image : IntPtr.Zero;

            PInvoke.cvMerge(c0, c1, c2, IntPtr.Zero, image);
            CVUtils.CheckLastError();
        }
コード例 #18
0
        public void Resize(int newWidth, int newHeight, CVInterpolation interpolation)
        {
            CVImage newImage = new CVImage(newWidth, newHeight, Depth, Channels);

            PInvoke.cvResize(this.image, newImage.image, (int)interpolation);
            CVUtils.CheckLastError();
            Release();
            this.image       = newImage.image;
            newImage.created = false;
        }
コード例 #19
0
        public void Split(CVImage ch0, CVImage ch1, CVImage ch2, CVImage ch3)
        {
            __IplImagePtr d0 = ch0 != null ? ch0.image : IntPtr.Zero;
            __IplImagePtr d1 = ch1 != null ? ch1.image : IntPtr.Zero;
            __IplImagePtr d2 = ch2 != null ? ch2.image : IntPtr.Zero;
            __IplImagePtr d3 = ch3 != null ? ch3.image : IntPtr.Zero;

            PInvoke.cvSplit(image, d0, d1, d2, d3);
            CVUtils.CheckLastError();
        }
コード例 #20
0
        public void LoadImage(String filename, bool isColor)
        {
            if (!System.IO.File.Exists(filename))
            {
                throw new System.IO.FileNotFoundException(filename);
            }

            Release();

            image = PInvoke.cvLoadImage(filename, isColor ? 1 : 0);
            CVUtils.CheckLastError();
            created = true;
        }
コード例 #21
0
        public CVImage ToGrayscale()
        {
            CVImage gs = new CVImage(Width, Height, Depth, 1);

            System.Drawing.Rectangle prevRoi = this.RegionOfInterest;
            this.ResetROI();
            PInvoke.cvConvertImage(this.Internal, gs.Internal, (int)CVConvertImageFlags.Default);
            CVUtils.CheckLastError();
            this.RegionOfInterest = prevRoi;
            gs.RegionOfInterest   = prevRoi;

            return(gs);
        }
コード例 #22
0
        public static CVImage CannyEdgeDetection(CVImage image, double threshold1, double threshold2, int aperture_size)
        {
            if (image.Channels != 1)
            {
                throw new CVException("Canny edge detection supports only one channel image format.");
            }

            CVImage result = new CVImage(image.Width, image.Height, CVDepth.Depth8U, 1);

            // Native call to openCV canny algorithm:
            PInvoke.cvCanny(new __CvArrPtr(image), new __CvArrPtr(result), threshold1, threshold2, aperture_size);
            CVUtils.CheckLastError();
            return(result);
        }
コード例 #23
0
ファイル: CVCapture.cs プロジェクト: tdck/opencvdotnet
        public void Release()
        {
            if (asImage != null)
            {
                asImage.Release();
                asImage = null;
            }

            if (capture.ptr != IntPtr.Zero)
            {
                PInvoke.cvReleaseCapture(ref capture);
                CVUtils.CheckLastError();
            }
        }
コード例 #24
0
        public void Release()
        {
            if (!created)
            {
                return;
            }
            //created = false;
            __IplImagePtr ptr = image;

            if (ptr.ptr != IntPtr.Zero)
            {
                IntPtr ptrAux = ptr.ptr;
                PInvoke.cvReleaseImage(ref ptrAux);
                CVUtils.CheckLastError();
                image = new __IplImagePtr();
            }
        }
コード例 #25
0
ファイル: CVCapture.cs プロジェクト: tdck/opencvdotnet
        public CVImage QueryFrame()
        {
            if (asImage != null)
            {
                return(asImage.Clone());
            }

            __IplImagePtr frame = PInvoke.cvQueryFrame(capture);

            CVUtils.CheckLastError();
            if (frame.ptr == IntPtr.Zero)
            {
                return(null);
            }
            CVImage newImage = new CVImage(new CVImage(frame));

            return(newImage);
        }
コード例 #26
0
        public CVConnectedComp MeanShift(System.Drawing.Rectangle window, int termCriteria, int maxIterations, double eps)
        {
            System.Drawing.Rectangle realWindow = new System.Drawing.Rectangle(0, 0, Width, Height);
            if (!realWindow.IntersectsWith(window))
            {
                CVConnectedComp cc = new CVConnectedComp(window);
                return(cc);
            }

            realWindow.Intersect(window);

            __CvRect         wnd = new __CvRect(realWindow);
            __CvTermCriteria tc  = PInvoke.cvTermCriteria(termCriteria, maxIterations, eps);

            __CvConnectedComp comp = new __CvConnectedComp();

            PInvoke.cvMeanShift(Internal, wnd, tc, ref comp);
            CVUtils.CheckLastError();
            return(new CVConnectedComp(ref comp));
        }
コード例 #27
0
ファイル: CVHistogram.cs プロジェクト: tdck/opencvdotnet
        protected void CreateHist(int[] binSizes, CVPair[] binRanges)
        {
            this._binSizes  = binSizes;
            this._binRanges = binRanges;



            float[][] ranges = new float[binRanges.Length][];

            // make sure ranges & sizes are of the same dimention.
            System.Diagnostics.Debug.Assert(binSizes.Length == binRanges.Length, "Ranges & sizes must be of the same dimention");

            // create ranges array.
            for (int i = 0; i < binRanges.Length; ++i)
            {
                float[] range = new float[2];
                range[0]  = binRanges[i].First;
                range[1]  = binRanges[i].Second;
                ranges[i] = range;
            }

            this.Internal = PInvoke.cvCreateHist(this._binSizes, (int)CVHistogramType.Array, ranges, true);
            CVUtils.CheckLastError();
        }
コード例 #28
0
 private void Create(int width, int height, CVDepth depth, int channels)
 {
     image = PInvoke.cvCreateImage(new __CvSize(width, height), (int)depth, channels);
     CVUtils.CheckLastError();
     created = true;
 }
コード例 #29
0
 public unsafe CVImage(CVImage clone)
 {
     Create(clone.Width, clone.Height, clone.Depth, clone.Channels);
     PInvoke.cvConvertImage(clone.Array, this.image, clone.Internal.ToPointer()->origin == 1 ? (int)CVConvertImageFlags.Flip : 0);
     CVUtils.CheckLastError();
 }
コード例 #30
0
        public unsafe CVImage(System.Drawing.Bitmap sourceImage)
        {
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
            rect.X      = 0;
            rect.Y      = 0;
            rect.Width  = sourceImage.Width;
            rect.Height = sourceImage.Height;

            System.Drawing.Imaging.BitmapData bData =
                sourceImage.LockBits(rect,
                                     System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                     PixelFormat.Format24bppRgb);

            // New implementation:
            int pixelSizeInBytes = 8;
            int numberOfChannels = 3;

            this.image =
                PInvoke.cvCreateImage(
                    new __CvSize(sourceImage.Width, sourceImage.Height),
                    pixelSizeInBytes,
                    numberOfChannels);
            CVUtils.CheckLastError();

            unsafe
            {
                int   height = sourceImage.Height;
                int   width  = sourceImage.Width;
                byte *pRead  = (byte *)bData.Scan0.ToPointer();
                byte *pWrite = this.Internal.ToPointer()->imageData;

                int nReadStride  = bData.Stride - width * numberOfChannels;
                int nWriteStride = this.Internal.ToPointer()->widthStep - width * numberOfChannels;

                for (int row = 0; row < height; ++row, pRead += nReadStride, pWrite += nWriteStride)
                {
                    for (int col = 0; col < width; ++col, pRead += numberOfChannels, pWrite += numberOfChannels)
                    {
                        pWrite[0] = pRead[0]; // Blue
                        pWrite[1] = pRead[1]; // Green
                        pWrite[2] = pRead[2]; // Red
                    }
                }
            }


            #region Old Implementation
            //__IplImagePtr tempImage = PInvoke.cvCreateImageHeader(new __CvSize(sourceImage.Width, sourceImage.Height), 8, Bitmap.GetPixelFormatSize(sourceImage.PixelFormat) / 8);
            //tempImage.ToPointer()->imageData = (byte*)bData.Scan0.ToPointer();

            //__IplImagePtr[] dst = new __IplImagePtr[4];
            //for (int i = 0; i < 4; ++i)
            //{
            //    dst[i] = IntPtr.Zero;
            //}
            //for (int i = 0; i < tempImage.ToPointer()->nChannels; i++)
            //{
            //    dst[i] = PInvoke.cvCreateImage(new __CvSize(sourceImage.Width, sourceImage.Height), 8, 1);
            //}

            //PInvoke.cvSplit(
            //    tempImage,
            //    dst[0],
            //    dst[1],
            //    dst[2],
            //    dst[3]);

            //image = PInvoke.cvCreateImage(new __CvSize(sourceImage.Width, sourceImage.Height), 8, 3);
            //PInvoke.cvMerge(dst[0], dst[1], dst[2], IntPtr.Zero, image) ;

            //for (int i = 0; i < tempImage.ToPointer()->nChannels; i++)
            //{
            //    PInvoke.cvReleaseImage(ref dst[i]);
            //}
            #endregion

            created = true;
            sourceImage.UnlockBits(bData);
        }