示例#1
0
        private void UpdateContoursImage(PXCMBlobData.IBlob blob, int index)
        {
            // 輪郭を表示する
            var numOfContours = blob.QueryNumberOfContours();

            for (int i = 0; i < numOfContours; ++i)
            {
                // 輪郭の点の数を取得する
                var size = blob.QueryContourSize(i);
                if (size <= 0)
                {
                    continue;
                }

                // 輪郭の点を取得する
                PXCMPointI32[] points;
                var            sts = blob.QueryContourPoints(i, out points);
                if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    continue;
                }

                // 輪郭の点を描画する
                drawContour(points, index);
            }
        }
示例#2
0
文件: blob.cs 项目: zeos/Somaphone
        public void Evaluate(int SpreadMax)
        {
            if( (FContext.SliceCount > 0) && (FContext[0].Configured) )
            {
                processConfig();

                // Iterate over blobs from right to left
                try
                {
               		Int32 iBlobsNum = FContext[0].blobData.QueryNumberOfBlobs();

               		FCenter.SliceCount = iBlobsNum;
               		FOuterPoints.SliceCount = iBlobsNum;
               		FInnerPoints.SliceCount = iBlobsNum;
               		FClosest.SliceCount = iBlobsNum;
               		FBlobsCount[0] = iBlobsNum;
               		var ImageSize =  FContext[0].DepthImageSize;
               		FImage.SliceCount = iBlobsNum;

                    PXCMBlobData.IBlob[] blobList = new PXCMBlobData.IBlob[iBlobsNum];

               		for (int i = 0; i < iBlobsNum; i++)
               		{
               			FContext[0].blobData.QueryBlob(i,
               			                               PXCMBlobData.SegmentationImageType.SEGMENTATION_IMAGE_DEPTH,
               			                               PXCMBlobData.AccessOrderType.ACCESS_ORDER_NEAR_TO_FAR,
               			                               out blobList[i]);

               			// handle extracted blob data
                        Int32 nContours = blobList[i].QueryNumberOfContours();
                   		if (nContours > 0)
                        {

                   			//contourPts
                            for (int k = 0; k < nContours; ++k)
                            {
                                PXCMBlobData.IContour ContourData;
                                blobList[i].QueryContour(k, out ContourData);

                                int contourSize = ContourData.QuerySize();

                                PXCMPointI32[] ContourPoints = new PXCMPointI32[contourSize];

                                ContourData.QueryPoints(out ContourPoints);

                               	if(ContourPoints.Length > 0)
                               	{
                               		if( (ContourData.IsOuter() == true)  )
                                   	{
                                   		FOuterPoints[i].SliceCount = ContourPoints.Length;
                                   		for(int index = 0; index < ContourPoints.Length; index++) FOuterPoints[i][index] = new Vector2D(ContourPoints[index].x / (float)ImageSize.x * 2.0f - 1.0f, 1.0f - ContourPoints[index].y / (float)ImageSize.y * 2.0f);
                                   		//Parallel.For (0, ContourPoints.Length, index => { FOuterPoints[i][index] = new Vector2D(ContourPoints[index].x / (float)ImageSize.x * 2.0f - 1.0f, 1.0f - ContourPoints[index].y / (float)ImageSize.y * 2.0f); );
                                   	}
                               		/*
                                   	else
                                   	{
                                   		FInnerPoints[i].SliceCount = ContourPoints.Length;
                                   		for (int p = 0; p < ContourPoints.Length; p++)
                                        {
                                   			Parallel.For (0, ContourPoints.Length,
                                   		              	  index => { FInnerPoints[i][index] = new Vector2D(ContourPoints[index].x / (float)ImageSize.x * 2.0f - 1.0f, 1.0f - ContourPoints[index].y / (float)ImageSize.y * 2.0f); }
                                   		             );

                                        }
                                   	}
                               		*/
                               	}

                            }

                            PXCMImage BlobImage;
                            if (FContext[0].blobConfig.IsSegmentationImageEnabled() && blobList[i].QuerySegmentationImage(out BlobImage) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
                            {

                                PXCMImage.ImageData BlobImageData;
                                if (BlobImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_Y8, out BlobImageData) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
                                {
                                    FImage[i] = new RSSDKImage();
                                    FImage[i].Source = BlobImage;
                                    FImage[i].Data = BlobImage.GetY8Pixels();
                                    FImage[i].Source = BlobImage;
                                    FImage[i].Width = BlobImage.info.width;
                                    FImage[i].Height = BlobImage.info.height;
                                    FImage[i].Format = PXCMImage.PixelFormat.PIXEL_FORMAT_Y8;

                                    BlobImage.ReleaseAccess(BlobImageData);

                                }

                   			}

                   		}

                   		FCenter[i] 	= blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_CENTER).ToVector3D();
                   		FClosest[i] = blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_CLOSEST).ToVector3D();

               		}

               	}
                catch (Exception)
                {
                    //Flogger.Log();
                }
            }
        }
        /* Displaying Mask Images */
        private unsafe void DisplayPicture(PXCMImage depth, PXCMBlobData blobData)
        {
            if (depth == null)
                return;

            PXCMImage image = depth;
            PXCMImage.ImageInfo info = image.QueryInfo();

            int numOfBlobs = blobData.QueryNumberOfBlobs();

            if (_maxBlobToShow > numOfBlobs)
            {
                _maxBlobToShow = numOfBlobs;
            }

            PXCMBlobData.IBlob[] blobList = new PXCMBlobData.IBlob[_maxBlobToShow];
            PXCMPointI32[][] pointOuter = new PXCMPointI32[_maxBlobToShow][];
            PXCMPointI32[][] pointInner = new PXCMPointI32[_maxBlobToShow][];

            Bitmap picture = new Bitmap(image.info.width, image.info.height, PixelFormat.Format32bppRgb);

            PXCMImage.ImageData bdata;
            pxcmStatus results = pxcmStatus.PXCM_STATUS_NO_ERROR;
            PXCMBlobData.AccessOrderType accessOrder = PXCMBlobData.AccessOrderType.ACCESS_ORDER_LARGE_TO_SMALL;
            int accessOrderBy = form.GetAccessOrder();

            switch (accessOrderBy)
            {
                case 1:
                    accessOrder = PXCMBlobData.AccessOrderType.ACCESS_ORDER_NEAR_TO_FAR;
                    break;
                case 2:
                    accessOrder = PXCMBlobData.AccessOrderType.ACCESS_ORDER_RIGHT_TO_LEFT;
                    break;
                case 0:
                default:
                    accessOrder = PXCMBlobData.AccessOrderType.ACCESS_ORDER_LARGE_TO_SMALL;
                    break;
            }

            Rectangle rect = new Rectangle(0, 0, image.info.width, image.info.height);
            BitmapData bitmapdata = picture.LockBits(rect, ImageLockMode.ReadWrite, picture.PixelFormat);

            for (int j = 0; j < _maxBlobToShow; j++)
            {
                byte tmp1 = (Byte)(255 - (255 / (_maxBlobToShow) * j));
                results = blobData.QueryBlobByAccessOrder(j, accessOrder, out blobList[j]);
                if (results == pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    bool isSegmentationImage = true;
                    results = blobList[j].QuerySegmentationImage(out image);
                    if (results != pxcmStatus.PXCM_STATUS_NO_ERROR)
                    {
                        PXCMImage.ImageInfo imgInfo = new PXCMImage.ImageInfo();
                        imgInfo.width = 640;
                        imgInfo.height = 480;
                        imgInfo.format = PXCMImage.PixelFormat.PIXEL_FORMAT_Y8;
                        PXCMSession session = PXCMSession.CreateInstance();
                        if (session != null)
                        {
                            image = session.CreateImage(info);
                            if (image == null) return;
                        }
                        image.AcquireAccess(PXCMImage.Access.ACCESS_WRITE, PXCMImage.PixelFormat.PIXEL_FORMAT_Y8, out bdata);
                        byte* numPtr = (byte*)bitmapdata.Scan0; //dst
                        byte* numPtr2 = (byte*)bdata.planes[0]; //row
                        int imagesize = image.info.width * image.info.height;

                        byte tmp;
                        for (int i = 0; i < imagesize; i++, numPtr += 4, numPtr2++)
                        {
                            tmp = (byte)(0);
                            numPtr[0] = tmp;
                            numPtr[1] = tmp;
                            numPtr[2] = tmp;
                            numPtr[3] = tmp;
                        }

                        image.ReleaseAccess(bdata);
                        isSegmentationImage = false;
                    }

                    results = image.AcquireAccess(PXCMImage.Access.ACCESS_READ_WRITE, PXCMImage.PixelFormat.PIXEL_FORMAT_Y8, out bdata);

                    if (form.GetBlobState() && isSegmentationImage == true)
                    {
                        byte* numPtr = (byte*)bitmapdata.Scan0; //dst
                        byte* numPtr2 = (byte*)bdata.planes[0]; //row
                        int imagesize = image.info.width * image.info.height;

                        for (int i = 0; i < imagesize; i++, numPtr += 4, numPtr2++)
                        {
                            byte tmp = (Byte)(numPtr2[0] == 0 ? 0 : tmp1);
                            tmp |= numPtr[0];
                            numPtr[0] = tmp;
                            numPtr[1] = tmp;
                            numPtr[2] = tmp;
                            numPtr[3] = 0xff;
                        }
                    }

                    if ((form.GetContourState()))
                    {
                        int contourNumber = blobList[j].QueryNumberOfContours();
                        if (contourNumber > 0)
                        {
                            for (int k = 0; k < contourNumber; ++k)
                            {
                                int contourSize = blobList[j].QueryContourSize(k);
                                if (blobList[j].IsContourOuter(k) == true)
                                    blobList[j].QueryContourPoints(k, out pointOuter[j]);
                                else
                                {
                                    blobList[j].QueryContourPoints(k, out pointInner[j]);
                                }
                            }

                            if (results == pxcmStatus.PXCM_STATUS_NO_ERROR && form.GetBlobState() == false)
                            {
                                byte* numPtr = (byte*)bitmapdata.Scan0; //dst
                                byte* numPtr2 = (byte*)bdata.planes[0]; //row
                                int imagesize = image.info.width * image.info.height;

                                byte tmp;
                                for (int i = 0; i < imagesize; i++, numPtr += 4, numPtr2++)
                                {
                                    tmp = (byte)(0);
                                    numPtr[0] = tmp;
                                    numPtr[1] = tmp;
                                    numPtr[2] = tmp;
                                    numPtr[3] = tmp;
                                }

                            }
                        }
                    }
                    image.ReleaseAccess(bdata);
                    image.Dispose();
                }
            }
            picture.UnlockBits(bitmapdata);
            form.DisplayBitmap(picture);

            ///////// that is my polygon zone

            Bitmap imageInstance2 = picture;
            i++;

            Bitmap croppedImage = null;
            if (contourMostRight.x > 0) {
                int rectWidth =  (int)(contourMostRight.x - contourMostLeft.x);
                int rectHeight = (int)(contourMostTop.y - contourMostBottom.y);
                Rectangle sourceRectangle = new Rectangle(new Point((int)contourMostLeft.x,
                                                            (int)contourMostBottom.y),
                                                                   new Size(rectWidth, rectHeight));

                croppedImage = CropImage(imageInstance2, sourceRectangle);

            }

            String[] origArray = {
                "d:\\origG.jpeg",
                "d:\\origX.jpeg",
                "d:\\origY.jpeg"
            };

            for (int i = 0; i < origArray.Length; i++)
            {
                Bitmap orig = null;
            if (File.Exists(origArray[i]))
                orig = new Bitmap(@origArray[i]);

            if (orig != null && croppedImage != null)
            {
                float diff = 0;
                orig            = ScaleImage(orig,          150, 100);
                croppedImage    = ScaleImage(croppedImage,  150, 100);
                bool isImageBlank = true;

                for (int y = 0; y < orig.Height; y++)
                {
                    for (int x = 0; x < orig.Width; x++)
                    {
                        if(croppedImage.GetPixel(x, y).R > 1 && croppedImage.GetPixel(x, y).B > 1
                            && croppedImage.GetPixel(x, y).G > 1)
                        {
                            isImageBlank = false;
                            break;
                        }
                    }
                }

                if (!isImageBlank && orig.Size.Width == croppedImage.Size.Width)
                {
                    for (int y = 0; y < orig.Height; y++)
                    {
                        for (int x = 0; x < orig.Width; x++)
                        {
                            diff += (float)Math.Abs(orig.GetPixel(x, y).R -
                                                    croppedImage.GetPixel(x, y).R) / 255;
                            diff += (float)Math.Abs(orig.GetPixel(x, y).G -
                                                    croppedImage.GetPixel(x, y).G) / 255;
                            diff += (float)Math.Abs(orig.GetPixel(x, y).B -
                                                    croppedImage.GetPixel(x, y).B) / 255;
                        }
                    }
                }
                float percentMatch = 100 * diff / (orig.Width * orig.Height * 2);
                Console.WriteLine("diff: {0} %", percentMatch);

                if (percentMatch >= 90){
                    Console.WriteLine(origArray[i].ToString());
                        Environment.Exit(0);
                }
            }
               }
            /*
            if (croppedImage != null)
            {
                croppedImage.Save("d:\\cropedImage.jpeg",   System.Drawing.Imaging.ImageFormat.Jpeg);
             //   croppedImage.Save("d:\\origG.jpeg",         System.Drawing.Imaging.ImageFormat.Jpeg);

            }*/

            if (i == 100)
                Environment.Exit(0);

            picture.Dispose();

            for (int i = 0; i < _maxBlobToShow; i++)
            {
                if (form.GetContourState())
                {
                    if (pointOuter[i] != null && pointOuter[i].Length > 0)
                        form.DisplayContour(pointOuter[i], i);
                    if (pointInner[i] != null && pointInner[i].Length > 0)
                        form.DisplayContour(pointInner[i], i);
                }

                if (form.GetBlobDataPointsState())
                {
                    form.DisplayBlobDataPoints(blobList[i], i + 1);
                }

                PXCMPoint3DF32 point = blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_CENTER);
                contourMostRight         = blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_LEFT_MOST);
                contourMostLeft        = blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_RIGHT_MOST);
                contourMostBottom          = blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_TOP_MOST);
                contourMostTop       = blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_BOTTOM_MOST);

                form.DisplayBlobNumber(point, i + 1);

            }
        }
示例#4
0
文件: blob.cs 项目: zeos/Somaphone
        public void Evaluate(int SpreadMax)
        {
            if( (FSenseWrap.SliceCount > 0) && (FSenseWrap[0].Configured) )
            {
                var ImageSize = FSenseWrap[0].sm.captureManager.QueryImageSize(PXCMCapture.StreamType.STREAM_TYPE_DEPTH);

                // Iterate over blobs from right to left
                try
                {
               		Int32 iBlobsNum = FSenseWrap[0].blobData.QueryNumberOfBlobs();
               		FCenter.SliceCount = iBlobsNum;
               		FPoints.SliceCount = iBlobsNum;

                    PXCMBlobData.IBlob[] blobList = new PXCMBlobData.IBlob[iBlobsNum];
                    PXCMPointI32[][] pointOuter = new PXCMPointI32[iBlobsNum][];
               		 	PXCMPointI32[][] pointInner = new PXCMPointI32[iBlobsNum][];

               		for (int i = 0; i < iBlobsNum; i++)
               		{

               			FSenseWrap[0].blobData.QueryBlobByAccessOrder(i,PXCMBlobData.AccessOrderType.ACCESS_ORDER_RIGHT_TO_LEFT, out blobList[i]);

                        // handle extracted blob data
                        Int32 nContours = blobList[i].QueryNumberOfContours();
                   		if (nContours > 0)
                        {
                   			//contourPts
                            for (int k = 0; k < nContours; ++k)
                            {
                                int contourSize = blobList[i].QueryContourSize(k);

                                if(blobList[i].IsContourOuter(k) == true)
                                {
                                    blobList[i].QueryContourPoints(k, out pointOuter[i]);
                                }
                                else
                                {
                                //    blobList[i].QueryContourPoints(k, out pointInner[i]);
                                }

                            }

                            //Convert to vector2D, map to vvvv!!!
                            if (pointOuter[i] != null && pointOuter[i].Length > 0)
                            {
                                FPoints[i].SliceCount = pointOuter[i].Length;
                                for (int p = 0; p < pointOuter[i].Length; p++)
                                {
                                    var pt = pointOuter[i][p].ToVector2D();
                                    pt.x = pt.x / (float)ImageSize.width * 2.0f - 1.0f;
                                    pt.y = 1.0f - pt.y / (float)ImageSize.height * 2.0f;

                                    FPoints[i][p] = pt;

                                }
                            }
                   		}

                   		FCenter[i] = blobList[i].QueryExtremityPoint(PXCMBlobData.ExtremityType.EXTREMITY_CENTER).ToVector3D() / 100;

               		}

               	}
                catch (Exception)
                {
                    //Flogger.Log();
                }
            }
        }