如何在单击框内的任何位置时获取边界框的尺寸?

时间:2016-06-22 08:36:47

标签: c++ eclipse opencv image-processing computer-vision

我有DPM代码,可以检测帧中存在的所有人,并在它们之间绘制一个边界框。我有一些关于获得点击鼠标位置的代码。 如果我点击任何一个框内的任何地方,我想拍摄检测到的人物框并获得边界框的尺寸。

请帮帮我,看看代码。

void CallBackFunc(int event, int x, int y, int flags, void* userdata){

    if ( event == EVENT_LBUTTONDOWN ){
    cout << "Left button clicked and it's position is:  (" << x << ", " << y << ")" << endl;

    Rect retrieved_rectangle;
    int width = retrieved_rectangle.width;
    int height = retrieved_rectangle.height;
    cout << "Width: "  << width << endl;
    cout << "Height: "  << height << endl;
  }
}

int main()
{

    int k;
    cv::Ptr<DPMDetector> detector = \
    DPMDetector::create(vector<string>(1, "inriaperson.xml"));
    Mat img = imread("C:\\Users\\ashutosh\\Downloads\\Documents\\Experiments\\PL_Pizza\\0001.jpg");
    Scalar color(0, 255, 255); // yellow
    double t = (double) getTickCount();
    vector<DPMDetector::ObjectDetection> ds;
    Mat frame;
    frame = img.clone();
    detector->detect(img, ds);
    // cout << "DS SIZE:" << ds.size() << endl;
    // compute frame per second
    t = ((double) getTickCount() - t)/getTickFrequency();
    //elapsed time
    string text = format("%0.1f fps", 1.0/t);
    //  string text1 = ds[i].score;

    for(unsigned int i = 0; i < ds.size(); i++)
    {
    //  cout << ds[i].rect << endl;
    // cout << ds[i].score << endl;
    //  rectangle(frame, ds[i].rect, color, 2);
    int id = ds[i].classID;

    int x1,y1;
    Rect temp=ds[i].rect;
    Point pt;
    x1=temp.x;
    y1=temp.y;
    pt.x=x1;
    pt.y=y1;
    cerr << ds[i].score << "\t" << ds[i].rect << endl;
    rectangle(frame, ds[i].rect, Scalar(0, 255, 255), 1);
    cv::Scalar color(255, 0, 0);
    int fontFace = cv::FONT_HERSHEY_SCRIPT_SIMPLEX;
    double fontScale = 2.5;
    int thickness = 2;
    ostringstream score1;
    score1 << ds[i].score;
    putText(frame,score1.str(),pt,FONT_HERSHEY_COMPLEX_SMALL,1,cvScalar(0,0,0),1,CV_AA);

    }
    Scalar textColor(0,0,250);
    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
    compression_params.push_back(95);
    imshow("detect", frame);
    setMouseCallback("detect", CallBackFunc, NULL);
    waitKey(0);
    return 0;
}

0 个答案:

没有答案