I want to draw something with opencv c++ using my wacom graphic device. I've written some code that works great with the mouse, but with the wacom graphic device it is only working sometimes and very slow. The problem is the EVENT_LBUTTONDOWN, which is only detected after a considerable time delay. Here is my code:
#include <iostream>
#include <list>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
const cv::Vec3b color_black{0, 0, 0};
const cv::Vec3b color_azure{255, 127, 0};
int width = 800;
int height = 600;
cv::Mat image = cv::Mat(cv::Size(width, height), CV_8UC3, color_black);
std::list<cv::Point> polygon;
bool isMouseDown = false;
void repaint(){
cv::rectangle(image, cv::Rect(0, 0, width, height), color_black, 1);
std::list<cv::Point>::iterator it = polygon.begin();
if(it== polygon.end()){
return;
}
cv::Point prev = *it;
it++;
while(it!=polygon.end() ){
cv::line(image, prev, *it, color_azure, 2);
prev = *it;
it++;
}
//show the image
imshow("My Window", image);
}
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if ( event == EVENT_LBUTTONDOWN )
{
isMouseDown = true;
polygon.clear();
repaint();
}
else if ( event == EVENT_MOUSEMOVE )
{
if(isMouseDown){
polygon.push_back(cv::Point(x,y));
repaint();
}
}
else if (event == EVENT_LBUTTONUP){
isMouseDown = false;
}
}
int main(int argc, char** argv)
{
// Read image from file
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);
//show the image
imshow("My Window", image);
// Wait until user press some key
waitKey(0);
return 0;
}