HSV颜色范围检测

far*_*don 3 java opencv colors image-processing

我是图像检测技术的新手.我正在使用java openCV来检测图像中的颜色.我能够检测出黄色和红色.我从命中和试用方法得到了价值,这些都很好.但我想从下面的手形图像中检测绿色,橙色和蓝色.

在此输入图像描述

这是我的代码的一部分.

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; 
    }
}
Run Code Online (Sandbox Code Playgroud)

请告诉我如何获得蓝色,绿色和橙色的HSV范围或者告诉我这些所需颜色的范围.谢谢

skm*_*skm 9

你可以在互联网上轻松找到色轮.请参见下图,其中显示了各种颜色的Hue范围.下面给出的色轮范围是0到360,但在openCV中,范围是0到180.因此,只需将值除以2,您将得到openCV的值.

图片