我想跟踪图片中的颜色。我使用以下代码(javaCV):
//Load initial image.
iplRGB = cvLoadImage(imageFile, CV_LOAD_IMAGE_UNCHANGED);
//Prepare for HSV
iplHSV = cvCreateImage(iplRGB.cvSize(), iplRGB.depth(), iplRGB.nChannels());
//Transform RGB to HSV
cvCvtColor(iplRGB, iplHSV, CV_BGR2HSV);
//Define a region of interest.
//minRow = 0; maxRow = iplHSV.height();
//minCol = 0; maxCol = iplHSV.width();
minRow = 197; minCol = 0; maxRow = 210; maxCol = 70;
//Print each HSV for each pixel of the region.
for (int y = minRow; y < maxRow; y++){
for (int x = minCol; x < maxCol; x++) {
CvScalar pixelHsv = cvGet2D(iplHSV, y, x);
double h = pixelHsv.val(0);
double s = pixelHsv.val(1);
double v = pixelHsv.val(2);
String line = y + "," + x + "," + h + "," + s + "," + v;
System.out.println(line);
}
}
我可以很容易地从输出中找出HUE和SAT的最小值和最大值。让我们调用minHue,minSat,maxHue和maxSat(不要花哨!)。然后我执行这段代码:
iplMask = cvCreateImage(iplHSV.cvSize(), iplHSV.depth(), 1);
CvScalar min = cvScalar(minHue, minSat, 0, 0);
CvScalar max = cvScalar(maxHue, maxSat, 255 ,0);
cvInRangeS(iplHSV, min, max, iplMask);
当我展示iplMask时,我不应该看到感兴趣的区域完全是白色的吗?我不知道,我看到轮廓是白色的,但矩形的内部是黑色的。我必须弄乱一些东西,但我不明白。
我知道Hue在[0..179]中使用OpenCV而Sat和Val在[0..255],但由于我使用openCV显示的值,我认为我不需要重新缩放...
无论如何,我迷路了。有人可以解释一下吗?感谢。