How to use setMouseCallback to draw random line? - c++

I've studied Opencv's setMouseCallback function.
I understand there are several events, list below:
CV_EVENT_MOUSEMOVE 0,
CV_EVENT_LBUTTONDOWN 1,
CV_EVENT_RBUTTONDOWN 2,
CV_EVENT_MBUTTONDOWN 3,
CV_EVENT_LBUTTONUP 4,
CV_EVENT_RBUTTONUP 5,
CV_EVENT_MBUTTONUP 6,
CV_EVENT_LBUTTONDBLCLK 7,
CV_EVENT_RBUTTONDBLCLK 8,
CV_EVENT_MBUTTONDBLCLK 9,
CV_EVENT_FLAG_LBUTTON 1,
CV_EVENT_FLAG_RBUTTON 2,
CV_EVENT_FLAG_MBUTTON 4,
CV_EVENT_FLAG_CTRLKEY 8,
CV_EVENT_FLAG_SHIFTKEY 16,
CV_EVENT_FLAG_ALTKEY 32,
and with the coordinates I get from these events, I can draw, for example: straight line, circle, ellipse...etc.
But instead of straight line, I want to draw random line, just like the "pencil" or "brush" function in Microsoft Paint.
The CV_EVENT_MOUSEMOVE does return all the coordinates as my mouse move through the image, but I don't know how to combine it with CV_EVENT_LBUTTONDOWN to represent "Start drawing line" and CV_EVENT_LBUTTONUP to represent "Finish drawing line"?
Does anyone know how to achieve my requirement with setMouseCallback?

I figured it out myself,
bool trigger;
Mat img;
void onMouse(int event, int x, int y, int flag, int param)
{
if(event == CV_EVENT_LBUTTONDOWN)
{
img = Mat::zeros(320, 240, CV_8UC3);
trigger = true;
}
if(event == CV_EVENT_LBUTTONUP)
{
trigger = false;
}
if(event == CV_EVENT_MOUSEMOVE)
{
if(trigger == true)
{
line(img, cvPoint(x, y), cvPoint(x, y), Scalar(0, 0, 255), 3, CV_AA, 0);
imshow("Drawing", img);
}
}
}
int main(int argc, char *argv[])
{
.
.
.
cvsetMouseCallback("Origin img", onMouse, NULL);
.
.
.
}

Related

Create a button in OpenCV GUI without QT [duplicate]

I am working on a game-project using OpenCV. Now I have to make a simple GUI: a window with one button, using HighGui only.
I'm not sure but I think I'm supposed to use something like this:
cvNamedWindow( "NameWindow" , CV_WINDOW_AUTOSIZE);
Any help is much appreciated.
OpenCV does not provide a button, but you can easily use a colored rectangle, and check if the clicked point on the image is inside this rectangle.
Remember that OpenCV HighGui is very simple and is meant only for debugging purposes. You may want to use a full featured graphic library as Qt, or similar.
However, this is a small example that shows a (green) image, and a button on top:
Clicking the button will print "Clicked" on stdout:
Code:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat3b canvas;
string buttonText("Click me!");
string winName = "My cool GUI v0.1";
Rect button;
void callBackFunc(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
if (button.contains(Point(x, y)))
{
cout << "Clicked!" << endl;
rectangle(canvas(button), button, Scalar(0,0,255), 2);
}
}
if (event == EVENT_LBUTTONUP)
{
rectangle(canvas, button, Scalar(200, 200, 200), 2);
}
imshow(winName, canvas);
waitKey(1);
}
int main()
{
// An image
Mat3b img(300, 300, Vec3b(0, 255, 0));
// Your button
button = Rect(0,0,img.cols, 50);
// The canvas
canvas = Mat3b(img.rows + button.height, img.cols, Vec3b(0,0,0));
// Draw the button
canvas(button) = Vec3b(200,200,200);
putText(canvas(button), buttonText, Point(button.width*0.35, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0,0,0));
// Draw the image
img.copyTo(canvas(Rect(0, button.height, img.cols, img.rows)));
// Setup callback function
namedWindow(winName);
setMouseCallback(winName, callBackFunc);
imshow(winName, canvas);
waitKey();
return 0;
}
You can now create buttons and other useful tools on OpenCV windows. The page below shows a couple of useful examples.
https://docs.opencv.org/master/dc/d46/group__highgui__qt.html
The gist of it is:
#include <opencv2/highgui.hpp>
void myButtonName_callback(int state, void*userData) {
// do something
printf("Button pressed\r\n");
}
createButton("myButtonName",myButtonName_callback,NULL,CV_PUSH_BUTTON,1);
you are aware that openCV is not a GUI library, but an image processing lib?
it ships with highgui: http://docs.opencv.org/2.4/modules/highgui/doc/highgui.html
for those cases where you really have no other options, but need to create a window for displaying stuff.
While OpenCV was designed for use in full-scale applications and can be used within functionally rich UI frameworks (such as Qt*, WinForms*, or Cocoa*) or without any UI at all, sometimes there it is required to try functionality quickly and visualize the results. This is what the HighGUI module has been designed for.
see OpenCV and creating GUIs
edit: "this doesn't answer the question": -> more help..
you can't.
or that is, if you know your underlying window manager, you can.
i.e. if you'r on windows, you could get the window handle, and dynamically add more controls..
if not, you need to know what platform you'r on, and how to do it in that.
I wouldn't dare to try and put this in a simple answer
#Miki, why I cannot use my buttons alternately? How to fix it? I mean I want to use them at the same time.
EDIT: I fixed it myself. No need of help. :)
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Mat3b canvas;
string buttonText("Nacisnij guzik!");
string buttonText2("Nacisnij guzik NR2!");
string winName = "PokerGui";
int a = 0;//mozna pozniej usunac, potrzebne tylko czy button reaguje jak nalezy
Rect button, button2;
void callBackFunc(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
if (button.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza
{
a = a + 7;
cout << "Nacisnales guzik!\n" << endl;
printf("liczba = %i\n", a);
rectangle(canvas(button), button, Scalar(0, 0, 255), 2);
}
else if (button2.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza
{
//a = a + 7;
cout << "Nacisnales guzik NR2!\n" << endl;
//printf("liczba = %i\n", a);
rectangle(canvas(button2), button, Scalar(0, 0, 255), 2);
}
}
//if (event == EVENT_LBUTTONUP)
//{
//rectangle(canvas, button, Scalar(200, 200, 200), 2);
//}
imshow(winName, canvas);
waitKey(1);
}
void callBackFunc2(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
if (button2.contains(Point(x, y)))//ponizej to co ma sie wykonac po nacisnieciu klawisza
{
//a = a + 7;
cout << "Nacisnales guzik NR2!\n" << endl;
//printf("liczba = %i\n", a);
rectangle(canvas(button2), button, Scalar(0, 0, 255), 2);
}
}
//if (event == EVENT_LBUTTONUP)
//{
//rectangle(canvas, button, Scalar(200, 200, 200), 2);
//}
imshow(winName, canvas);
waitKey(1);
}
int main()
{
// An image
Mat3b img(300, 300, Vec3b(0, 255, 0));
// Your button
button = Rect(0, 0, img.cols, 50);
button2 = Rect(0, 60, img.cols, 50);
// The canvas
canvas = Mat3b(img.rows + button.height, img.cols, Vec3b(0, 0, 0));
// Draw the button
canvas(button) = Vec3b(200, 200, 200);
canvas(button2) = Vec3b(200, 200, 200);
putText(canvas(button), buttonText, Point(button.width*0.35, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 0));
putText(canvas(button2), buttonText2, Point(button.width*0.25, button.height*0.7), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 0));
// Draw the image
//img.copyTo(canvas(Rect(0, button.height, img.cols, img.rows)));
// Setup callback function
namedWindow(winName);
setMouseCallback(winName, callBackFunc);
//setMouseCallback(winName, callBackFunc2);
imshow(winName, canvas);
waitKey();
return 0;
}

OpenCV stop rendering while mouse moving

I have a simple window that contains simple black image with small solid circle inside it. I have wrote a simple code to be able to drag and drop this circle. I could do it correctly. Inside the mouse_event function:
void on_mouse_event(int event_type, int x, int y, int flags, void*){
if (event_type == cv::EVENT_RBUTTONDOWN){
//Catch the circle
}
else if (event_type == cv::EVENT_MOUSEMOVE){
//Release the point
}
else if (event_type == cv::EVENT_MOUSEMOVE){
//Change circle position according to curser moving
//re draw the circle again
//show the new image
}
}
The main function:
while (true){
//show image code (simple cv::imshow);
if (cv::waitKey(1) == 27){
break;
}
}
The problem is that if I drag the circle and start to move fast, the image will not change till I stop. However, if I go slowly it will change according to the move. What is the reason of this problem?
P.S I am not in doubt of slow hardware at all. I am working on workstation and I am mentoring the processor utilization and just one of its 8 core reach around 50% and the memory is almost free.
I am using Windows 10 if it helps.
you could test the following code.(adapted from opencv_annotation.cpp)
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
// Function prototypes
void on_mouse(int, int, int, int, void*);
// Public parameters
Mat image(600, 800, CV_8UC3, Scalar(220, 220, 220));
Mat current_view;
int circle_center_x = image.cols / 2, circle_center_y = image.rows / 2, radius = 40;
bool dragging = false;
const string window_name = "OpenCV Mouse Event Demo";
void on_mouse(int event, int x, int y, int, void *)
{
// Action when left button is clicked
if (event == EVENT_LBUTTONDOWN & (abs(circle_center_x - x) < 20) & (abs(circle_center_y - y) < 20))
{
dragging = true;
}
if (event == EVENT_LBUTTONUP)
{
dragging = false;
}
// Action when mouse is moving
if ((event == EVENT_MOUSEMOVE) && dragging)
{
image.copyTo(current_view);
circle_center_x = x;
circle_center_y = y;
circle(current_view, Point(circle_center_x, circle_center_y), radius, Scalar(255, 0, 0), 5);
imshow(window_name, current_view);
}
}
int main(int argc, const char** argv)
{
// Init window interface and couple mouse actions
namedWindow(window_name, WINDOW_AUTOSIZE);
setMouseCallback(window_name, on_mouse);
image.copyTo(current_view);
circle(current_view, Point(circle_center_x, circle_center_y), radius, Scalar(255, 0, 0), 5);
imshow(window_name, current_view);
int key_pressed = 0;
do
{
// Keys for processing
// Based on the universal ASCII code of the keystroke: http://www.asciitable.com/
// <SPACE> = 32 add circle to current image
// <ESC> = 27 exit program
key_pressed = 0xFF & waitKey(0);
if (key_pressed==32)
{
// draw a circle on the image
circle(image, Point(circle_center_x, circle_center_y), radius, Scalar(0, 0, 255), -1);
image.copyTo(current_view);
circle(current_view, Point(circle_center_x, circle_center_y), radius, Scalar(255, 0, 0), 5);
imshow(window_name, current_view);
}
}
// Continue as long as the <ESC> key has not been pressed
while (key_pressed != 27);
// Close down the window
destroyWindow(window_name);
return 0;
}

How to check collision on 2D map in SFML?

I have one RectangleShape for wall and I draw this one object for every "1" on my 2D map:
int map[5][8] =
{
{ 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1 },
{ 1, 2, 1, 0, 0, 0, 3, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1 }
};
sf::RectangleShape player;
player.setSize(sf::Vector2f(32,32));
sf::RectangleShape wall;
wall.setSize(sf::Vector2f(32,32));
wall.setFillColor(sf::Color(40, 199, 40));
for (int mapX= 0; mapX< 8; mapX++)
{
for (int mapY= 0; mapY< 5; mapY++)
{
if (map[mapY][mapX] == 1)
{
wall.setPosition(mapX * 32, mapY * 32);
window.draw(wall);
}
}
}
And I checking for collision with code listed down, but this works for only last drawn wall. How to fix this? Theres only one object wall and I want to check collision for all drawned walls for my 2D array map.
if (event.type == sf::Event::TextEntered)
{
fPlayerY_t = fPlayerY;
fPlayerX_t = fPlayerX;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) fPlayerY_t = fPlayerY - 32;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) fPlayerY_t = fPlayerY + 32;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) fPlayerX_t = fPlayerX + 32;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) fPlayerX_t = fPlayerX - 32;
// temp pos
player.setPosition(fPlayerX_t, fPlayerY_t);
// collision with wall
if (player.getPosition().x == wall.getPosition().x &&
player.getPosition().y == wall.getPosition().y)
{
// back to prev position
player.setPosition(fPlayerX, fPlayerY);
}
else
{
// No collision
fPlayerX = fPlayerX_t;
fPlayerY = fPlayerY_t;
}
}
Convert the coordinates of the player in array coordinates then check if map[x][y] == 1.
So do not have to loop through your entire map every game loop.
To convert into array coordinates, use int conversion :
int playerTileX = (int)(playerWorldX / 32);
int playerTileY = (int)(playerWorldY / 32);
if(map[playerTileX ][playerTileY ] == 1)
{
// collision.
}
else
{
// no collision.
}
What I dislike with this solution : it only checks for tiles collision, you have to be sure every wall is the same size (here 32*32).

Failed to draw multiple lines on an image using mouse click

I have learnt how to draw a line on an image in OpenCV using line( frame, Point( 15, 20 ), Point( 70, 50), 'r', 2, 8 );
I have also learnt how to draw a line on image using mouse clicks. For example the following code draws a line connecting two points user clicks on an image:
using namespace cv;
using namespace std;
void onMouse(int evt, int x, int y, int flags, void* param) {
if(evt == CV_EVENT_LBUTTONDOWN) {
std::vector<cv::Point>* ptPtr = (std::vector<cv::Point>*)param;
ptPtr->push_back(cv::Point(x,y));
}
}
int main()
{
std::vector<Point> points;
cv::namedWindow("Output Window");
Mat frame = cv::imread("chhha.png");
cv::setMouseCallback("Output Window", onMouse, (void*)&points);
int X1=0, Y1=0, X2=0, Y2=0;
while(1)
{
cv::imshow("Output Window", frame);
if (points.size() > 1) //we have 2 points
{
for (auto it = points.begin(); it != points.end(); ++it)
{
}
break;
}
waitKey(10);
}
// Now let us draw a line on the image
line( frame, points[0], points[1], 'r', 2, 8 );
cv::imshow("Output Window", frame);
waitKey( 10 );
getch();
return 0;
}
Now basically what I want is to keep drawing the lines until I right click or may be some character is entered.
What I have tried so far is using do-while loop:
char m;
do{
while(1)
{
cv::imshow("Output Window", frame);
if (points.size() > 1) //we have 2 points
{
for (auto it = points.begin(); it != points.end(); ++it)
{
}
break;
}
waitKey(10);
}
// Draw a line
line( frame, points[0], points[1], 'r', 2, 8 );
cv::imshow("Output Window", frame);
cout<<"do you want more lines, if so , press 'y'"<<endl;
cin>>m;
// instead of this a right click check would be much better
if(m!='y')
{
break;
}
}while(m=='y');
But the problem is this way not even one line would be drawn, and after few clicks and entering 'y', the application won't respond.
Please help me solve this issue.
There are probably several issues with your code that prevent it from doing what you want. The first thing that comes to mind is that you're not clearing the vector used for capturing the coordinates. After drawing a line
line( frame, points[0], points[1], 'r', 2, 8 );
you should reset the vector like this
points.clear();
so that the next mouse click's coordinate goes to points[0]. Otherwise it would append to the vector and you would keep drawing lines between the first two mouse coordinates over and over again.

How can I perform Template Matching process in SUB-IMAGE extracted from ORIGINAL-IMAGE and Display the results in Original Image

One whole day I have tried a lot to get all the related matches (with matchtemplate function) in sub-Image , which is ROI i have already extracted from the original image with the mousecallback function. So my code is below for the Matchingfunction
////Matching Function
void CTemplate_MatchDlg::OnBnTemplatematch()
{
namedWindow("reference",CV_WINDOW_AUTOSIZE);
while(true)
{
Mat ref = imread("img.jpg"); // Original Image
mod_ref = cvCreateMat(ref.rows,ref.cols,CV_32F);// resizing the image to fit in picture box
resize(ref,mod_ref,Size(),0.5,0.5,CV_INTER_AREA);
Mat tpl =imread("Template.jpg"); // TEMPLATE IMAGE
cvSetMouseCallback("reference",find_mouseHandler,0);
Mat aim=roiImg1.clone(); // SUB_IMAGE FROM ORIGINALIMAGE
// aim variable contains the ROI matrix
// next, want to perform template matching in that ROI // and display results on original image
if(select_flag1 == 1)
{
// imshow("ref",aim);
Mat res(aim.rows-tpl.rows+1, aim.cols-tpl.cols+1,CV_32FC1);
matchTemplate(aim, tpl, res, CV_TM_CCOEFF_NORMED);
threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
while (1)
{
double minval, maxval, threshold = 0.8;
Point minloc, maxloc;
minMaxLoc(res, &minval, &maxval, &minloc, &maxloc);
//// Draw Bound boxes for detected templates in sub matrix
if (maxval >= threshold)
{
rectangle(
aim,
maxloc,
Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows),
CV_RGB(0,255,0), 1,8,0
);
floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
}else
break;
}
}
////Bounding box for ROI selection with mouse
rectangle(mod_ref, rect2, CV_RGB(255, 0, 0), 1, 8, 0); // rect2 is ROI
// my idea is to get all the matches in ROI with bounding boxes
// no need to mark any matches outside the ROI
//Clearly i want to process only ROI
imshow("reference", mod_ref); // show the image with the results
waitKey(10);
}
//cvReleaseMat(&mod_ref);
destroyWindow("reference");
}
/// ImplementMouse Call Back
void find_mouseHandler(int event, int x, int y, int flags, void* param)
{
if (event == CV_EVENT_LBUTTONDOWN && !drag)
{
/* left button clicked. ROI selection begins*/
point1 = Point(x, y);
drag = 1;
}
if (event == CV_EVENT_MOUSEMOVE && drag)
{
/* mouse dragged. ROI being selected*/
Mat img3 = mod_ref.clone();
point2 = Point(x, y);
rectangle(img3, point1, point2, CV_RGB(255, 0, 0), 1, 8, 0);
imshow("reference", img3);
//
}
if (event == CV_EVENT_LBUTTONUP && drag)
{
Mat img4=mod_ref.clone();
point2 = Point(x, y);
rect1 = Rect(point1.x,point1.y,x-point1.x,y-point1.y);
drag = 0;
roiImg1 = mod_ref(rect1); //SUB_IMAGE MATRIX
imshow("reference", img4);
}
if (event == CV_EVENT_LBUTTONUP)
{
/* ROI selected */
select_flag1 = 1;
drag = 0;
}
}
build and debugging process successfully done. But, when I click the Match button in dialog I'm getting the error:
Unhandled exception at 0x74bf812f in Match.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ae150..
So my idea is to get all the matches in the Sub-image when compare with the TEMPLATE IMAGE and show the final result (matches with bounding boxes) in the ORIGINAL IMAGE itself.
Anyone help me in this regard!! Help would be appreciated greatly!!
My code below is a modification of the original tutorial provided by OpenCV.
It loads an image from the command-line and displays it on the screen so the user can draw a rectangle somewhere to select the sub-image to be the template. After that operation is done, the sub-image will be inside a green rectangle:
Press any key to let the program perform the template matching. A new window titled "Template Match:" appears displaying the original image plus a blue rectangle that shows the matched area:
#include <cv.h>
#include <highgui.h>
#include <iostream>
const char* ref_window = "Draw rectangle to select template";
std::vector<cv::Point> rect_points;
void mouse_callback(int event, int x, int y, int flags, void* param)
{
if (!param)
return;
cv::Mat* ref_img = (cv::Mat*) param;
// Upon LMB click, store the X,Y coordinates to define a rectangle.
// Later this info is used to set a ROI in the reference image.
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
{
if (rect_points.size() == 0)
rect_points.push_back(cv::Point(x, y));
}
break;
case CV_EVENT_LBUTTONUP:
{
if (rect_points.size() == 1)
rect_points.push_back(cv::Point(x, y));
}
break;
default:
break;
}
if (rect_points.size() == 2)
{
cv::rectangle(*ref_img,
rect_points[0],
rect_points[1],
cv::Scalar(0, 255, 0),
2);
cv::imshow(ref_window, *ref_img);
}
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " <image>" << std::endl;
return -1;
}
cv::Mat source = cv::imread(argv[1]); // original image
if (source.empty())
{
std::cout << "!!! Failed to load source image." << std::endl;
return -1;
}
// For testing purposes, our template image will be a copy of the original.
// Later we will present it in a window to the user, and he will select a region
// as a template, and then we'll try to match that to the original image.
cv::Mat reference = source.clone();
cv::namedWindow(ref_window, CV_WINDOW_AUTOSIZE);
cv::setMouseCallback(ref_window, mouse_callback, (void*)&reference);
cv::imshow(ref_window, reference);
cv::waitKey(0);
if (rect_points.size() != 2)
{
std::cout << "!!! Oops! You forgot to draw a rectangle." << std::endl;
return -1;
}
// Create a cv::Rect with the dimensions of the selected area in the image
cv::Rect template_roi = cv::boundingRect(rect_points);
// Create THE TEMPLATE image using the ROI from the rectangle
cv::Mat template_img = cv::Mat(source, template_roi);
// Create the result matrix
int result_cols = source.cols - template_img.cols + 1;
int result_rows = source.rows - template_img.rows + 1;
cv::Mat result;
// Do the matching and normalize
cv::matchTemplate(source, template_img, result, CV_TM_CCORR_NORMED);
cv::normalize(result, result, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());
/// Localizing the best match with minMaxLoc
double min_val = 0, max_val = 0;
cv::Point min_loc, max_loc, match_loc;
int match_method = CV_TM_CCORR_NORMED;
cv::minMaxLoc(result, &min_val, &max_val, &min_loc, &max_loc, cv::Mat());
// When using CV_TM_CCORR_NORMED, max_loc holds the point with maximum
// correlation.
match_loc = max_loc;
// Draw a rectangle in the area that was matched
cv:rectangle(source,
match_loc,
cv::Point(match_loc.x + template_img.cols , match_loc.y + template_img.rows),
cv::Scalar(255, 0, 0), 2, 8, 0 );
imshow("Template Match:", source);
cv::waitKey(0);
return 0;
}