对于相同间隔的HSV值的不同识别

时间:2015-05-20 15:33:18

标签: opencv video blob tracking

我正在使用OpenCV进行阈值处理和跟踪机器人。在第一时刻,我找到了正确的值来跟踪他(使用图像进行测试),但是当我使用具有相同颜色属性的视频时(蓝色,在我的情况下),它不起作用。

视频如下:

https://youtu.be/AJC4NNGnF5E

由于声誉不佳,我无法发布图片,但这是成功的案例。跟踪他的代码,首先是图片,然后是视频,也是如此:

图像:

image = imread("test_MultObj_2.jpg", CV_RGB2HSV);         // Read the file

if(! image.data )                                      // Check for invalid input
{
    cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}

// Converted values to HSV
cvtColor(image,hsv,CV_BGR2HSV);

// Valores Mágicos - Azul! :D
inRange(hsv, Scalar(120,0,70),
             Scalar(210,255,230), hsv_thres);

// Plot archives treated and untreated files
// cv::imshow("image",image);
// cv::imshow("hsv",hsv);
// cv::imshow("threshold",hsv_thres);

// Save files as jpg in the same folder as the programms
imwrite("origimage.jpg", image);

hsv_thres.copyTo(saveHSV);

// Finds the desired contours of the binary image
// Caution: the hsv_thres image is altered in the process
findContours( hsv_thres, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

// Finds the contour with biggest area
// TO FIX: FEHLERHAFT! The image may have objects with the same area of it but with bigger areas!
double max_area = 0, area;
int pos_max;

// Find the biggest area of the contours
for( int i = 0; i< contours.size(); i++ )
{
    area = contourArea(contours[i], false); 
    if(area>max_area)
    {
        max_area = area;

        pos_max = i;
    }
}

// Find the moments of the robot (Assumption: The robot is the biggest blob in the image)
Moments mu;
mu = moments( contours[pos_max], false );

//  Get the mass centers:
Point2f mc;
mc = Point2f(mu.m10/mu.m00,mu.m01/mu.m00);

// big gray circle.
cv::circle(image, mc, 20, CV_RGB(255, 0, 0), -1, CV_AA);
// imshow("asdasdsa",saveHSV);

 cv::namedWindow("test_window");
 cv::imshow("test_window", image);

/*
 /// Draw contours
 Mat drawing = Mat::zeros( hsv_thres.size(), CV_8UC3 );
 for( int i = 0; i< contours.size(); i++ )
 {
   Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
   drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
 }

/// Show in a window
namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
*/

waitKey(0);

return 0;
}

视频:

    // Load the video of the robot
VideoCapture capture("C:\\robotcap.mp4");
if(!capture.isOpened()){
    std::cout<<"cannot read video!\n";
    return -1;
}

while(1){ // Open the video and edit the current frame

    // Copy webcam stream to image
    capture.read(image);

    // Check if at end
    if (image.empty()) break;

    // Converted values to HSV
    cvtColor(image,hsv,CV_BGR2HSV);     

    // Valores Mágicos - Azul! :D
    inRange(hsv, Scalar(120,0,70),
                 Scalar(210,255,230), hsv_thres);

    // Find the desired contours of the binary image
    // Caution: the hsv_thres image is altered in the process
    hsv_thres.copyTo(saveHSV);
    findContours( hsv_thres, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    // Find the contour with biggest area
    // TO FIX: FEHLERHAFT! The image may have objects with the same color but with bigger areas!
    double max_area = 0, area;
    int pos_max;

    // Find the biggest area of the contours
    for( int i = 0; i< contours.size(); i++ )
    {
        area = contourArea(contours[i], false); 
        if(area>max_area)
        {
            max_area = area;

            pos_max = i;
        }
    }

    // Find the moments of the robot (Assumption: The robot is the biggest blob in the image)
    Moments mu;
    mu = moments( contours[pos_max], false );

    //  Get the mass centers
    Point2f mc;
    mc = Point2f(mu.m10/mu.m00,mu.m01/mu.m00);

    // big red circle
    cv::circle(image, mc, 20, CV_RGB(255, 0, 0), -1, CV_AA);

     cv::namedWindow("test_window");
     cv::imshow("test_window", hsv_thres);

waitKey(33);

}

return 0;
}

在这种情况下我觉得奇怪的是,我对两个媒体使用了相同的间隔。

感谢您的帮助,

PS:抱歉英语不好。我不是母语人士。 :)

0 个答案:

没有答案