Tuesday, 18 March 2014

Project 1. Creating your own PaintBox!

Hi guys!
Lets take a look at how a simple Paint application can be implemented using OpenCV.
I shall combine the concepts of tutorial #1(MouseCallback) and tutorial #8, (circle).
The code is quite easy and self explanatory, provided, you are thorough with all the tutorials until now.
If you have any doubts, feel free to comment.
#include
#include
#include
#include
using namespace std;
using namespace cv;

Mat img;
Point pt;

bool lup=false, ldown=false;

static void mouse_callback(int event, int x, int y,int, void *){
if(event==EVENT_LBUTTONDOWN){
 ldown=true;
 pt.x=x;
 pt.y=y;
 circle(img,pt,4,CV_RGB(255,0,0),-3);
}

if(ldown==true && lup==false){
 pt.x=x;
 pt.y=y;
 Mat local_img=img.clone();
 circle(img,pt,4,CV_RGB(255,0,0),-3);
 imshow("PaintBox",local_img);
}

if(event==EVENT_LBUTTONUP){
 lup=true;
}
if(ldown==true && lup==true){
 ldown=false;
 lup=false;
}
}
int main()
{
 img=imread("images/white.png");
 namedWindow("PaintBox");
 imshow("PaintBox",img);
 setMouseCallback("PaintBox",mouse_callback);
 while(char(waitKey(1))!='q'){}
 return 0;
}



No comments:

Post a Comment