skin_hsv() public method

public skin_hsv ( IplImage image ) : IplImage
image IplImage
return IplImage
        private void timerGrab_Tick(object sender, EventArgs e)
        {
            frame = highgui.CvQueryFrame(ref videoCapture);

            if (frame.ptr == IntPtr.Zero)
            {
                timerGrab.Stop();
                MessageBox.Show("Invalid Frame");
                return;
            }

            imgMain = cxcore.CvCreateImage(cxcore.CvGetSize(ref frame), 8, 3);

            if (reset)
            {
                showROI     = false;
                initialized = false;
                resetting();
            }

            cxcore.CvCopy(ref frame, ref imgMain);
            cxcore.CvFlip(ref imgMain, 0);

            #region ROI
            if (showROI && initialized)
            {
                cxcore.CvRectangle(ref imgMain, new CvPoint(roiX, roiY), new CvPoint(roiX + roiW, roiY + roiH), cxcore.CV_RGB(255, 0, 125), 1, 8, 0);
                imgCrop = cxcore.CvCreateImage(new CvSize(roiW, roiH), 8, 3);

                #region skinHSV/RGB
                if (showSkinHSV || showSkinRGB)
                {
                    imgSkin = new IplImage();
                    imgSkin = cxcore.CvCreateImage(cxcore.CvGetSize(ref frame), 8, 3);
                    if (showSkinHSV)
                    {
                        imgSkin = skinDet.skin_hsv(imgMain);
                    }
                    else if (showSkinRGB)
                    {
                        imgSkin = skinDet.skin_rgb(imgMain);
                    }
                    cxcore.CvSetImageROI(ref imgSkin, new CvRect(roiX, roiY, roiW, roiH));
                    cxcore.CvCopy(ref imgSkin, ref imgCrop);
                    cxcore.CvReleaseImage(ref imgSkin);

                    //noise removal
                    cv.CvDilate(ref imgCrop, ref imgCrop, dlgParam.GetP(0).i);
                    cv.CvErode(ref imgCrop, ref imgCrop, dlgParam.GetP(1).i);
                    for (int i = 0; i < dlgParam.GetP(2).i; i++)
                    {
                        cv.CvSmooth(ref imgCrop, ref imgCrop);
                    }
                }
                #endregion

                #region show threshold
                if (showThres || showEdge)
                {
                    imgGray = cxcore.CvCreateImage(cxcore.CvGetSize(ref imgCrop), 8, 1);
                    imgBin  = cxcore.CvCreateImage(cxcore.CvGetSize(ref imgCrop), 8, 1);
                    imgMot  = cxcore.CvCreateImage(cxcore.CvGetSize(ref imgCrop), 8, 1);

                    cv.CvCvtColor(ref imgCrop, ref imgGray, cvtypes.CV_BGR2GRAY);

                    cv.CvThreshold(ref imgGray, ref imgMot, 0, 255, cv.CV_THRESH_BINARY_INV);
                    abs.Absolute(imgMot);

                    if (showThres)
                    {
                        cv.CvThreshold(ref imgGray, ref imgBin, 0, 255, cv.CV_THRESH_BINARY_INV);
                    }
                    else if (showEdge)
                    {
                        edge_dlg();
                        cv.CvCanny(ref imgGray, ref imgBin, dlgCanny.GetP(0).i, dlgCanny.GetP(1).i);
                    }

                    highgui.CvShowImage("Crop", ref imgBin);

                    #region matching
                    if (match)
                    {
                        if (adaBlackPix(imgBin))
                        {
                            hasil = (int)KNN.classify(ref imgBin, false);
                        }
                        else
                        {
                            hasil = 19;
                        }
                        WriteLine(Signs[hasil], false, false);

                        match       = false;
                        show_letter = true;
                    }
                    #endregion

                    cxcore.CvReleaseImage(ref imgGray);
                    cxcore.CvReleaseImage(ref imgCrop);
                    cxcore.CvReleaseImage(ref imgBin);
                    cxcore.CvReleaseImage(ref imgMot);
                }
                else
                {
                    highgui.CvShowImage("Crop", ref imgCrop);
                    cxcore.CvReleaseImage(ref imgCrop);
                }
                #endregion
            }
            else if (!initialized && !showROI)
            {
                imgMain = hc.cariHaar(imgMain);
            }
            else if (!initialized) //initialize windows
            {
                initialize();
            }
            #endregion

            if (show_letter)
            {
                CvFont font = new CvFont();
                cxcore.CvInitFont(ref font, cxcore.CV_FONT_HERSHEY_SIMPLEX, 5, 5, 0, 10, cxcore.CV_AA);
                cxcore.CvPutText(ref imgMain, Signs[hasil], new CvPoint(50, 200), ref font, new CvScalar(0, 255, 0));
            }

            picBoxMain.Image = highgui.ToBitmap(imgMain, false);

            cxcore.CvReleaseImage(ref imgMain);

            fps++;

            if ((openx != 0 && openy != 0 && closex != 0 && closey != 0) && !showROI)
            {
                euclidean();
            }
        }
Esempio n. 2
0
        public IplImage cariHaar(IplImage image)
        {
            cxcore.CvFlip(ref image, 1);

            imgSkin = new IplImage();
            imgSkin = cxcore.CvCreateImage(cxcore.CvGetSize(ref image), 8, 3);
            imgSkin = skinDet.skin_hsv(image);

            IplImage gray = cxcore.CvCreateImage(new CvSize(imgSkin.width, imgSkin.height), (int)cxtypes.IPL_DEPTH_8U, 1);

            cv.CvCvtColor(ref imgSkin, ref gray, cvtypes.CV_BGR2GRAY);

            IplImage     small_image = imgSkin;
            CvMemStorage storage = cxcore.CvCreateMemStorage(0);
            CvSeq        handOpen, handClose;
            int          i, scale = 1;
            bool         do_pyramids = true;

            #region percepat proses
            if (do_pyramids)
            {
                small_image = cxcore.CvCreateImage(new CvSize(imgSkin.width / 2, imgSkin.height / 2), (int)cxtypes.IPL_DEPTH_8U, 3);
                cv.CvPyrDown(ref imgSkin, ref small_image, (int)CvFilter.CV_GAUSSIAN_5x5);
                scale = 2;
            }
            #endregion

            #region open hand
            IntPtr ptrO = cxcore.CvLoad("..\\..\\Training\\handOpen.xml");
            cascadeO     = (CvHaarClassifierCascade)cvconvert.PtrToType(ptrO, typeof(CvHaarClassifierCascade));
            cascadeO.ptr = ptrO;
            handOpen     = cv.CvHaarDetectObjects(ref small_image, ref cascadeO, ref storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, new CvSize(0, 0));
            if (handOpen.total != 0)
            {
                for (i = 0; i < handOpen.total; i++)
                {
                    ho_rect = (CvRect)cvconvert.PtrToType(cxcore.CvGetSeqElem(ref handOpen, i), typeof(CvRect));
                    cxcore.CvRectangle(ref image, new CvPoint(ho_rect.x * scale - 10, ho_rect.y * scale - 10),
                                       new CvPoint((ho_rect.x + ho_rect.width) * scale + 10, (ho_rect.y + ho_rect.height) * scale + 10),
                                       cxcore.CV_RGB(255, 0, 0), 1, 8, 0);
                }
                form.closex = 0;
                form.closey = 0;
                form.openx  = image.width - ((ho_rect.x * scale) + ((ho_rect.width * scale) / 2));
                form.openy  = ho_rect.y * scale + ((ho_rect.height * scale) / 2);

                form.roiX = 640 - (ho_rect.x * scale - 10) - (ho_rect.width * scale + 10);
                form.roiY = ho_rect.y * scale - 10;
                form.roiW = ho_rect.width * scale + 10;
                form.roiH = ho_rect.height * scale + 10;
            }
            #endregion

            #region close hand
            if (handOpen.total == 0)
            {
                IntPtr ptrC = cxcore.CvLoad("..\\..\\Training\\handClose.xml");
                cascadeC     = (CvHaarClassifierCascade)cvconvert.PtrToType(ptrC, typeof(CvHaarClassifierCascade));
                cascadeC.ptr = ptrC;
                handClose    = cv.CvHaarDetectObjects(ref small_image, ref cascadeC, ref storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, new CvSize(0, 0));
                if (handClose.total != 0)
                {
                    for (i = 0; i < handClose.total; i++)
                    {
                        hc_rect = (CvRect)cvconvert.PtrToType(cxcore.CvGetSeqElem(ref handClose, i), typeof(CvRect));
                        cxcore.CvRectangle(ref image, new CvPoint(hc_rect.x * scale, hc_rect.y * scale),
                                           new CvPoint((hc_rect.x + hc_rect.width) * scale, (hc_rect.y + hc_rect.height) * scale),
                                           cxcore.CV_RGB(0, 0, 255), 1, 8, 0);
                    }
                    form.closex = image.width - ((hc_rect.x * scale) + ((hc_rect.width * scale) / 2));
                    form.closey = hc_rect.y * scale + ((hc_rect.height * scale) / 2);
                }
            }
            #endregion

            cxcore.CvReleaseMemStorage(ref storage);
            cv.CvReleaseHaarClassifierCascade(ref cascadeO);
            if (handOpen.total == 0)
            {
                cv.CvReleaseHaarClassifierCascade(ref cascadeC);
            }
            cxcore.CvReleaseImage(ref gray);
            cxcore.CvReleaseImage(ref small_image);
            cxcore.CvReleaseImage(ref imgSkin);
            cxcore.CvFlip(ref image, 1);
            return(image);
        }