【发布时间】:2014-06-19 11:19:07
【问题描述】:
我是图像检测技术的新手。我正在使用 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;
}
}
请告诉我如何获得蓝色、绿色和橙色颜色的 HSV 范围,或者告诉我这些所需颜色的范围。谢谢
【问题讨论】:
-
在 H、S 和 V 值上使用跟踪栏,并使用
inRange(..)使用跟踪栏值对图像进行阈值处理。您实际上可以在您的设置中找到任何颜色。
标签: java opencv image-processing colors