Java – HSV color range detection

colorsimage processingjavaopencv

I am new to image detection techniques. I am using java openCV to detect colors in an image. I am able to detect yellow and red colors. I got there value from hit and trial method, these are working fine. But I want to detect green , orange and blue color as well from the hand image below.

enter image description here

Here is a part of my code .

public class ObjectPositionDetect {


    static int hueLowerR = 160;              // for red
    static int hueUpperR = 180;


//    static int hueLowerR = 20;                  // for yellow
//    static int hueUpperR = 31;




    public static void main(String[] args) {
        IplImage orgImg = cvLoadImage("3.JPG");
        IplImage thresholdImage = hsvThreshold(orgImg);
        cvSaveImage("test.jpg", thresholdImage);
        Dimension position = getCoordinates(thresholdImage);
        System.out.println("Dimension of original Image : " + thresholdImage.width() + " , " + thresholdImage.height());
        System.out.println("Position of red spot    : x : " + position.width + " , y : " + position.height);
    }

    static Dimension getCoordinates(IplImage thresholdImage) {
        int posX = 0;
        int posY = 0;
        CvMoments moments = new CvMoments();
        cvMoments(thresholdImage, moments, 1);
        // cv Spatial moment : Mji=sumx,y(I(x,y)•xj•yi)
        // where I(x,y) is the intensity of the pixel (x, y).
        double momX10 = cvGetSpatialMoment(moments, 1, 0); // (x,y)
        double momY01 = cvGetSpatialMoment(moments, 0, 1);// (x,y)
        double area = cvGetCentralMoment(moments, 0, 0);
        System.out.println("this is area "+area);
        posX = (int) (momX10 / area);
        posY = (int) (momY01 / area);
        return new Dimension(posX, posY);
    }
    public static CvScalar CV_RGB(double r, double g, double b) {
    return cvScalar(b, g, r, 0);
    //    return cvScalar(r, g, b, 0);
}

    static IplImage hsvThreshold(IplImage orgImg) {
        // 8-bit, 3- color =(RGB)
        IplImage imgHSV = cvCreateImage(cvGetSize(orgImg), 8, 3);  // creating a copy of an image
        //cvSaveImage("monochromatic.jpg", imgHSV);
        //System.out.println(cvGetSize(orgImg));
        cvCvtColor(orgImg, imgHSV, CV_BGR2HSV);
        // 8-bit 1- color = monochrome
        IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);

        // cvScalar : ( H , S , V, A)


        cvInRangeS(imgHSV, cvScalar(hueLowerR, 100, 100, 0), cvScalar(hueUpperR, 255, 255, 0), imgThreshold);

       // cvInRangeS(imgHSV, cvScalar(160, 218, 0, 0), cvScalar(180, 220 , 0, 0), imgThreshold);


        cvReleaseImage(imgHSV);
        cvSmooth(imgThreshold, imgThreshold, CV_MEDIAN, 13);
         cvSaveImage("monochromatic.jpg", imgThreshold);
        // save
        return imgThreshold; 
    }
}

Kindly tell me how can I get the HSV ranges for Blue , Green and Orange colors or Just tell me the ranges for these required colors. Thanks

Best Answer

you can find a color wheel easily at internet. See the image below which tells you the Hue range of various colors. The range in color wheel given below is 0 to 360 but in openCV, the range is 0 to 180. So, just divide the values by 2, and you will get the value for openCV.

image