#include "opencv2/opencv.hpp"
include <iostream>
include <termios.h>
using namespace std; using namespace cv;
int main(){ VideoCapture cap(0); if(!cap.isOpened()){ cout << "Error opening video stream or file" << endl; return -1;
}
while(1){
Mat frame;
cap >> frame;
if (frame.empty())
break;
Mat hsv;
cvtColor(frame, hsv, COLOR_BGR2HSV);
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 140, 140), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 140, 140), Scalar(180, 255, 255), mask2);
Mat mask = mask1 | mask2;
Moments oMoments = moments(mask);
double moment01 = oMoments.m01;
double moment10 = oMoments.m10;
double area = oMoments.m00;
int posX = moment10/area;
int posY = moment01/area;
printf("position (%d,%d)\n", posX, posY);
imshow("Mask", mask);
char c=(char)waitKey(25);
if(c==27)
break;
}
cap.release();
destroyAllWindows();
return 0;
}