6

我是 OpenCV 的新手,我想在视频/图像中选择一个特定区域进行检测。就我而言,我想检测只在路上而不是在停车场的汽车。

4

3 回答 3

10

好吧,选择汽车需要使用训练数据。但是选择 ROI(感兴趣区域)相当简单:

考虑img = cv2.imread(image)

在这种情况下,您可以在代码中的某处指定一个区域:

sub_image = img[y:y+h, x:x+w]

一旦您指定了值,这将获得 ROI,当然,不使用“x”或“y”,其中 h 是高度,w 是宽度。请记住,图像只是二维矩阵。

用于CascadeClassifier()从图像中选择汽车。文档可在此处找到。OpenCV 包含可用于以 XML 文件形式进行分类的训练数据。

于 2013-04-24T06:02:28.900 回答
7

如果您想手动选择感兴趣区域 (ROI) 对其进行一些处理,那么您可以尝试使用鼠标单击事件来选择 ROI 的起点和终点。

一旦有了起点和终点,您就可以使用它从选定区域检索图像。

可以在图像或捕获视频帧上完成。

bool roi_captured = false;
Point pt1, pt2;
Mat cap_img;
//Callback for mousclick event, the x-y coordinate of mouse button-up and button-down 
//are stored in two points pt1, pt2.
void mouse_click(int event, int x, int y, int flags, void *param)
{

    switch(event)
    {
        case CV_EVENT_LBUTTONDOWN:
        {
            std::cout<<"Mouse Pressed"<<std::endl;

            if(!roi_capture)
            {
                pt1.x = x;
                pt1.y = y;
            }
            else
            {
                std::cout<<"ROI Already Acquired"<<std::endl;
            }
        break;
        }
        case CV_EVENT_LBUTTONUP:
        {
          if(!got_roi)
        {
            Mat cl;
            std::cout<<"Mouse LBUTTON Released"<<std::endl;

            pt2.x = x;
            pt2.y = y;
            cl = cap_img.clone();
            Mat roi(cl, Rect(pt1, pt2));
            Mat prev_imgT = roi.clone();
            std::cout<<"PT1"<<pt1.x<<", "<<pt1.y<<std::endl;
            std::cout<<"PT2"<<pt2.x<<","<<pt2.y<<std::endl;

            imshow("Clone",cl);

            got_roi = true;
        }
        else
        {
            std::cout<<"ROI Already Acquired"<<std::endl;
        }
        break;  
        }

    }

}
//In main open video and wait for roi event to complete by the use.
// You capture roi in pt1 and pt2 you can use the same coordinates for processing // //subsequent frame
int main(int argc, char *argv[])
{
    int frame_num = 0;
    int non_decode_frame =0;
    int count = 1, idx =0;
    int frame_pos =0;

    std::cout<<"Video File "<<argv[1]<<std::endl;

    cv::VideoCapture input_video(argv[1]);

    namedWindow("My_Win",1);

    cvSetMouseCallback("My_Win", mouse_click, 0);

    sleep(1);

    while(input_video.grab())
    {
        cap_img.release();

        if(input_video.retrieve(cap_img))
        {
            imshow("My_Win", cap_img);
            if(!got_roi)
            {
                            //Wait here till user select the desire ROI
                waitKey(0);
            }
            else
            {
                std::cout<<"Got ROI disp prev and curr image"<<std::endl;
                std::cout<<"PT1"<<pt1.x<<" "<<pt1.y<<std::endl;
                std::cout<<"PT2"<<pt2.x<<" "<<pt2.y<<std::endl;

                Mat curr_img_t1;
                Mat roi2(cap_img,Rect(pt1, pt2));
                Mat curr_imgT = roi2.clone();
                cvtColor(curr_imgT, curr_img_t1, CV_RGB2GRAY);

                    imshow("curr_img", curr_img);

            // Do remaining processing here on capture roi for every frame
                               waitKey(1);
                        }
                  }
}
}
于 2013-04-24T08:04:00.663 回答
0

您没有使用您正在编写的编程语言进行标记。无论如何,我用python回答你。(如果需要,您可以轻松地将其转换为 C++)

def mouse_drawing(event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONDOWN:
        car = img[y: y + carheight, x: x + carwidth]
        cv2.imwrite("car", car)

cv2.namedWindow("my_img")
cv2.setMouseCallback("my_img", mouse_drawing)

while True:
    cv2.imshow("my_img", img)
    key = cv2.waitKey(1)
    if key == 27:
        break

正如在其他答案中所说,如果您想自动找到汽车,那将是另一个问题,并且与训练数据和其他事情有关。

于 2020-03-17T20:52:19.933 回答