วันศุกร์ที่ 16 ธันวาคม พ.ศ. 2559

OPENCV : 6. Mouse input and special key




#include <iostream>

#include "opencv2/highgui/highgui.hpp"

#include "opencv2/imgproc/imgproc.hpp"



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


/*   Example 1

if (event == cv::EVENT_LBUTTONDOWN)
{
std::cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_RBUTTONDOWN)
{
std::cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_MBUTTONDOWN)
{
std::cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_MOUSEMOVE)
{
std::cout << "Mouse move over the window - position (" << x << ", " << y << ")" << std::endl;

}

*/

if (flags == (cv::EVENT_FLAG_CTRLKEY + cv::EVENT_FLAG_LBUTTON))
{
std::cout << "Left mouse button is clicked while pressing CTRL key - position (" << x << ", " << y << ")" << std::endl;
}
else if (flags == (cv::EVENT_FLAG_RBUTTON + cv::EVENT_FLAG_SHIFTKEY))
{
std::cout << "Right mouse button is clicked while pressing SHIFT key - position (" << x << ", " << y << ")" << std::endl;
}
else if (event == cv::EVENT_MOUSEMOVE && flags == cv::EVENT_FLAG_ALTKEY)
{
std::cout << "Mouse is moved over the window while pressing ALT key - position (" << x << ", " << y << ")" << std::endl;
}



}

int main(int argc, char** argv)
{
cv::namedWindow("Original Image");



// Set callback function bind with named window
cv::setMouseCallback("Original Image", CallBackFunc, NULL);



cv::VideoCapture videoCapture("video.mp4");

cv::Mat frame;

cv::Mat hsvImage;

cv::Mat threshold;

int countFrame = 0;

int numberOfAllFrame = videoCapture.get(CV_CAP_PROP_FRAME_COUNT);

std::cout << "All frame = " << numberOfAllFrame << std::endl;




while (1)
{


if (countFrame == numberOfAllFrame)
{
countFrame = 0;

videoCapture.set(CV_CAP_PROP_POS_FRAMES , 0);
}


videoCapture >> frame;


cv::imshow("Original Image", frame);


cv::waitKey(20);

countFrame++;


}

}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น