2016-06-16 5 views
0

나는이 윈도우 화면을 짓고 있어요 : 내가 플레이 사각형 내부의 제스처 터치를 결정하는 노란색 마커를 사용하려면 내가 녹색 마커 내부의 제스처 터치를 결정 사각형을 종료 할OpenCV에서 두 가지 색상을 추적하는 방법은 무엇입니까?

enter image description here

. 그러나, 나는이 코드를 썼다 :

//Capture a temporary image from the camera 
Mat imgTmp; 
cap.read(imgTmp); 

//Create a black image with the size as the camera output 
Mat imgLines = Mat::zeros(imgTmp.size(), CV_8UC3);; 

while (true) 
{ 
    Mat frame; 

    bool bSuccess = cap.read(frame); // read a new frame from video 

    if (!bSuccess) //if not success, break loop 
    { 
     cout << "Cannot read a frame from video stream" << endl; 
     break; 
    } 

    //tracking colors 
    Mat imgHSV; 

    cvtColor(frame, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV 

    Mat imgThresholded; 

    inRange(imgHSV, Scalar(ylowH, ylowS, ylowV), Scalar(yhighH, yhighS, yhighV), imgThresholded); //Threshold the image 
    inRange(imgHSV, Scalar(glowH, glowS, glowV), Scalar(ghighH, ghighS, ghighV), imgThresholded); 

    //morphological opening (removes small objects from the foreground) 
    erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
    dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 

    //morphological closing (removes small holes from the foreground) 
    dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
    erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 

    //Calculate the moments of the thresholded image 
    Moments oMoments = moments(imgThresholded); 

    double dM01 = oMoments.m01; 
    double dM10 = oMoments.m10; 
    double dArea = oMoments.m00; 

    // if the area <= 10000, I consider that the there are no object in the image and it's because of the noise, the area is not zero 
    if (dArea > 10000) 
    { 
     //calculate the position of the ball 
     int posX = dM10/dArea; 
     int posY = dM01/dArea; 

을하지만, 녹색과 노란색 컬러 모두보기의 카메라 필드 안에있는 동안 만 녹색을 추적한다. 어떻게 여러 색상을 추적하고 화면에서 특정 좌표 (마우스 기능과 같은)에서 상호 작용을 발견?

답변

0

cv::Mat 개체를 사용하여 이진 임계 값 이미지를 저장하기 때문에 초록색 개체 만 추적합니다. 코드에서 노란색 값을 사용하여 이미지를 임계하고 cv::Mat imgThresholded에 저장했습니다. 그런 다음 원본 이미지를 초록색으로 경계 처리하고 같은 변수에 저장합니다. 이 연산은 이전 임계 값의 정보보다 우선합니다. 형태학적인 작업에서도 마찬가지입니다. 해결하려면 물론, 당신은 또한 별도로 두 색상에 대한 이미지의 순간을 각각의 올바른 바이너리 이미지 행렬에서 작동하는 형태 학적 연산을 변경하고 계산해야하는 두 개의 행렬

Mat imgThreshYellow 
Mat imgThreshGreen 

inRange(imgHSV, Scalar(ylowH, ylowS, ylowV), Scalar(yhighH, yhighS, yhighV), imgThreshYellow); //Threshold the image 
inRange(imgHSV, Scalar(glowH, glowS, glowV), Scalar(ghighH, ghighS, ghighV), imgThreshGreen); 

를 사용합니다.

+0

감사합니다. 도움이되었습니다. – robstat7

관련 문제