java - HSV 颜色范围检测

标签 java opencv image-processing colors

我不熟悉图像检测技术。我正在使用 java openCV 来检测图像中的颜色。我能够检测到黄色和红色。我从命中和试用方法中获得了值(value),这些方法工作正常。但我还想从下面的手图像中检测绿色、橙色和蓝色。

enter image description here

这是我的部分代码。

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; 
    }
}

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

最佳答案

您可以在互联网上轻松找到色轮。请参阅下图,它告诉您各种颜色的色相范围。下面给出的色轮范围是 0 到 360,但在 openCV 中,范围是 0 到 180。所以,只需将这些值除以 2,就可以得到 openCV 的值。

image

关于java - HSV 颜色范围检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23433792/

相关文章:

Java/Guava 使用 'get' 前缀的约定?

java - 为什么不在 mysql 中创建模式?

python - 从 cv2.HoughLines 获得 miximum 票数的 rho 和 theta

java - 在哪个行号找到正则表达式匹配项?

java.lang.RuntimeException : Unable to invoke no-args constructor for retrofit2. 调用

c++ - CMake 链接器无法识别头文件

javascript - "emcc"命令无法识别

PHP - 获取站点的图标并在必要时将其转换为 png

java - 如何在分水岭的分割中添加坐标?

numpy - Pytorch 类型错误 : eq() received an invalid combination of arguments