I am still new to OpenCV, i am trying to detect object of multiple colour as well as center of the object. The color i need to detect is Red, Blue and Yellow. My program able to detect different colour, but it can't detect centroid of the same colour. The object with the bigger pixel will take the priority. Is there anythings i miss or where can i improve in my code?
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;
}