how can read a video from file, select ROI in first frame with rectangular and track obj?
it's my code. my first video frame loaded and I select a ROI. but when I choose ROI I cant see the rectangular. it' my firs problem. after that I don't know how track it with camshift algorithm.
please help me.
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/video/tracking.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
bool backprojMode = false;
bool selectObject = false;
int trackObject = 0;
bool showHist = true;
Rect selection;
int vmin = 10, vmax = 256, smin = 30;
Point pt1, pt2;
Mat cap_img;
int drag = 0;
void mouse_click(int event, int x, int y, int flags, void* param);
//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;
//cout << "Video File " << file << std::endl;
VideoCapture input_video("C:/Users/a/Desktop/PHD_TERM2/OPEN_CV/HW/2gui/gui2/data/trt1.avi");
//waitKey(30);
while (input_video.grab())
{
cap_img.release();
if (input_video.retrieve(cap_img))
{
namedWindow("My_Win", 1);
rectangle(cap_img, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);
imshow("My_Win", cap_img);
setMouseCallback("My_Win", mouse_click, 0);
//Wait here till user select the desire ROI
waitKey();
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);
rectangle(curr_imgT, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);
imshow("curr_img", curr_imgT);
// Do remaining processing here on capture roi for every frame
waitKey(1);
}
}
}
//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)
{
if (event == CV_EVENT_LBUTTONDOWN && !drag && !selectObject)
{
cout << "Mouse Pressed" << std::endl;
selectObject = true;
pt1.x = x;
pt1.y = y;
drag = 1;
}
if (event == CV_EVENT_LBUTTONUP && drag && !selectObject)
{
Mat cl;
std::cout << "Mouse LBUTTON Released" << std::endl;
pt2.x = x;
pt2.y = y;
cout << "PT1" << pt1.x << ", " << pt1.y << std::endl;
cout << "PT2" << pt2.x << "," << pt2.y << std::endl;
rectangle(cap_img, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);
imshow("Clone", cl);
selectObject = true;
drag = 0;
trackObject = -1;
}
if (event == CV_EVENT_MOUSEMOVE && drag && !selectObject)
{
Mat img1 = cap_img.clone();
pt2.x = x;
pt2.y = y;
rectangle(img1, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);
imshow("Select ROI", img1);
}
}