我还是OpenCV的新手,我正在尝试检测多种颜色的对象以及对象的中心。我需要检测的颜色是红色,蓝色和黄色。我的程序能够检测到不同的颜色,但它无法检测到相同颜色的质心。具有更大像素的对象将优先考虑。我有什么想念,或者我的代码可以在哪些地方改进?
的main.cpp
inRange(imgHSV, Scalar(redLowH,redLowS,redLowV), Scalar(redHighH, redHighS, redHighV), imgThresred); //Threshold the image
inRange(imgHSV, Scalar(blueLowH,blueLowS,blueLowV), Scalar(blueHighH, blueHighS, blueHighV), imgThresblue); //Threshold the image
inRange(imgHSV, Scalar(yellowLowH,yellowLowS,yellowLowV), Scalar(yellowHighH, yellowHighS, yellowHighV), imgThresyellow); //Threshold the image
Moments rMoments = moments(imgThresred);
double dM01_r = rMoments.m01;
double dM10_r = rMoments.m10;
double dArea_r = rMoments.m00;
//Calculate area of Red Object
if (dArea_r > 10000)
{
//calculate the position of the ball
posX_r = dM10_r / dArea_r;
posY_r = dM01_r / dArea_r;
//cout<<"The red object : X coodinate is "<< posX_r <<", Y coodinate is "<< posY_r <<endl;
if (iLastX_r >= 0 && iLastY_r >= 0 && posX_r >= 0 && posY_r >= 0)
{
//Draw a red line from the previous point to the current point
line(imgLines_r, Point(posX_r, posY_r), Point(iLastX_r, iLastY_r), Scalar(0,0,255), 10);
}
iLastX_r = posX_r;
iLastY_r = posY_r;
}
imgOriginal = imgOriginal + imgLines_r + imgLines_b + imgLines_y;
//Check Object region in the image
if (posX_r < 100)
{
cout<<"left side"<<endl;
}else
{
cout<<"no"<<endl;
}