gpt4 book ai didi

java - HSV 颜色范围检测

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:33:30 27 4
gpt4 key购买 nike

我不熟悉图像检测技术。我正在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com