OpenCVLib2.SkinDetect.skin_hsv C# (CSharp) Method

skin_hsv() public method

public skin_hsv ( IplImage image ) : IplImage
image IplImage
return IplImage
        public IplImage skin_hsv(IplImage image)
        {
            int xi, x, y, p;
            IplImage img_hsv;
            img_hsv = cxcore.CvCreateImage(cxcore.CvGetSize(ref image), 8, 3);
            cv.CvCvtColor(ref image, ref img_hsv, cvtypes.CV_BGR2HSV);

            num[,] bmpdata;
            bmpdata = new num[image.height, image.width];

            byte[] dataIn = img_hsv.ImageDataUChar;

            for (y = 0; y < image.height; y++)
            {
                for (xi = 0, x = 0; xi < image.widthStep; xi += 3, x++)
                {
                    //column position
                    p = y * image.widthStep + xi;

                    //ambil pixel data
                    bmpdata[y, x].H = dataIn[p];
                    bmpdata[y, x].S = dataIn[p + 1];
                    bmpdata[y, x].V = dataIn[p + 2];
                }
            }

            for (y = 0; y < image.height; y++)
            {
                for (x = 0; x < image.width; x++)
                {
                    if (bmpdata[y, x].H <= 19 && bmpdata[y, x].S >= 48) //jika kondisi cocok maka jgn d hitamkan
                        bmpdata[y, x].H += 0;
                    else
                        bmpdata[y, x].H = bmpdata[y, x].S = bmpdata[y, x].V = 0;
                }
            }

            for (y = 0; y < image.height; y++)
            {
                for (xi = 0, x = 0; xi < image.widthStep; xi += 3, x++)
                {
                    //column position
                    p = y * image.widthStep + xi;

                    //ambil pixel data
                    dataIn[p] = bmpdata[y, x].H;
                    dataIn[p + 1] = bmpdata[y, x].S;
                    dataIn[p + 2] = bmpdata[y, x].V;
                }
            }

            img_hsv.ImageDataUChar = dataIn;

            IplImage res = cxcore.CvCreateImage(cxcore.CvGetSize(ref image), 8, 3);
            cv.CvCvtColor(ref img_hsv, ref res, cvtypes.CV_HSV2BGR);

            cxcore.CvReleaseImage(ref img_hsv);
            return res;
        }

Usage Example

        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();
            }
        }
All Usage Examples Of OpenCVLib2.SkinDetect::skin_hsv