Tuesday, 18 March 2014

Project 1.1. PaintBox 2.0

Lets create PaintBox version 2.0.
In this version, I have introduced trackbars for changing colors and size of the brush(:P).
Again, this code is self-explanatory.
You just need to know the concept of trackbars!

#include
#include
#include
#include
using namespace std;
using namespace cv;
Mat img;
Point pt;
int red,green,blue;
int slider=0, slider_max=255;
int size_min=4,size_max=40,size;
bool lup=false, ldown=false;
void on_trackbar_r(int pos, void *){
 red=pos;
}

void on_trackbar_g(int pos, void *){
 green=pos;
}

void on_trackbar_b(int pos, void *){
 blue=pos;
}

void on_trackbar_size(int pos, void *){
 size=pos;
}
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,size,CV_RGB(red,green,blue),-3);
}

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

if(event==EVENT_LBUTTONUP){
 lup=true;
}
if(ldown==true && lup==true){
 ldown=false;
 lup=false;
}
}
int main()
{

 img=Mat(550,1300,CV_8UC3,Scalar(255,255,255));
//Here, I've created a white image of 550x1300 size
// which has 3 channels(R,G,B)
// each R,G,B color represented in Unsigned Char in 8 Bits
//So the strength of each channel varies from 0 to 255.
// color of the image is white(255,255,255)

 namedWindow("PaintBox");
 imshow("PaintBox",img);
 createTrackbar("Size","PaintBox",&size_min,size_max,on_trackbar_size);
 createTrackbar("Red","PaintBox",&slider,slider_max,on_trackbar_r);
 createTrackbar("Green","PaintBox",&slider,slider_max,on_trackbar_g);
 createTrackbar("Blue","PaintBox",&slider,slider_max,on_trackbar_b);
 setMouseCallback("PaintBox",mouse_callback);
 while(char(waitKey(1))!='q'){}
 return 0;
}

---------------------------------------------------------------------------

The 'Blue' trackbar is not visible as the image was too large for the Cropping Program to handle.

Note: For an eraser, just set the RGB values to (255,255,255).

Also, I just figured out that you can even use this as an image editor.

Just set img to the image you wish to edit. e.g.

img=imread("images/attitude.jpg");



And now, that's my fb cover pic!

No comments:

Post a Comment