OpenCV stop rendering while mouse moving - c++

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;
}

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;
}

How to detect mouse clicked location on an image with custom shaped cursor in c++

In my problem, there is an image and I need to give user to select some specific location within that image. For that I need to provide a square shape(customized by myself-widths and heights) with the cursor. Then user just wanted to place that on the location of the given image and click. Then I want take that locations. Can anyone with such experiences please guide me with sample code in c++ windows forms.
This is an ideal way of solving this problem. refer this source
#include "stdafx.h"
#include "test.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <cv.h>
#include <highgui.h>
IplImage* frame, *img1;
CvPoint point;
int drag = 0;
CvCapture *capture = 0;
int key = 0;
CvRect rect;
void mouseHandler(int event, int x, int y, int flags, void* param)
{
/* user press left button */
if (event == CV_EVENT_LBUTTONDOWN && !drag)
{
point = cvPoint(x, y);
drag = 1;
}
/* user drag the mouse */
if (event == CV_EVENT_MOUSEMOVE && drag)
{
img1 = cvCloneImage(frame);
cvRectangle(img1, point, cvPoint(x, y), CV_RGB(255, 0, 0), 1, 8, 0);
cvShowImage("result", img1);
}
/* user release left button */
if (event == CV_EVENT_LBUTTONUP && drag)
{
rect = cvRect(point.x, point.y, x - point.x, y - point.y);
cvSetImageROI(frame, rect);
cvShowImage("result", frame);
drag = 0;
}
/* user click right button: reset all */
if (event == CV_EVENT_RBUTTONUP)
{
drag = 0;
}
}
int main(int argc, char *argv[])
{
capture = cvCaptureFromCAM(0);
if (!capture)
{
printf("Cannot open initialize webcam!\n");
exit(0);
}
/* create a window for the video */
cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
while (key != 'q')
{
frame = cvQueryFrame(capture);
if (rect.width>0)
cvSetImageROI(frame, rect);
cvSetMouseCallback("result", mouseHandler, NULL);
key = cvWaitKey(10);
if ((char)key == 'r') { rect = cvRect(0, 0, 0, 0); cvResetImageROI(frame); }
cvShowImage("result", frame);
}
cvDestroyWindow("result");
cvReleaseImage(&img1);
return 0;
}
I would suggest that use VTK toolkit as it has cursor position location but make sure that your image top left corner is positioned with VTK's (world co-ordinate system's) (0,0) or if you do not want to position image that way then you need to maintain offset and use this offset to add/subtract when you get mouse position. In beginning you can refer below link as how VTK cursor position code works:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/ClickWorldCoordinates

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.

Picking contours using the mouse?

My project takes a bitmap snapshot of the client area of a given window, and converts this data into an IplImage instance. Then after a grayscale conversion, threshing etc, bounding boxes are drawn around any contours which exceed a given minimum size (area volume).
The result is then shown within a cvNamedWindow
All I need to do now is allow the user to click within these rects to effectively "choose" this contour, so that the application can then extract the subrect as a new image and save it to disk.
How can this be achieved using OpenCV in C++?
If you store your bounding boxes you can check in a for loop in a mouse event handler if a box is clicked and which box is clicked. The code for creating a mouse event:
cvNamedWindow("MyWindow", CV_WINDOW_NORMAL);
cvSetMouseCallback("MyWindow", mouseEvent, 0);
imshow("MyWindow", image);
void mouseEvent(int evt, int x, int y, int flags, void *param) {
if (evt == CV_EVENT_LBUTTONDOWN) {
printf("%d %d\n", x, y);
}
}
You can probably find out yourself how to check if these coordinates are within a bounding box. I would also recommend to use the C++ API, as stated in the comments, as it is much easier once you get the hang of it.
In case anyone else was interested on how to actually extract the chosen image,
here's how I was able to do it:
inline
IplImage* getSubImage(IplImage *image, CvRect region)
{
cvSetImageROI(image, region);
IplImage *imgRet = cvCreateImage( cvSize(region.width, region.height), image->depth, image->nChannels );
cvCopy(image, imgRet);
cvResetImageROI(image);
return imgRet;
}
inline
bool pointInRect(const int x, const int y, const CvRect& r)
{
return (
(x > r.x) && (x < (r.x + r.width)) &&
(y > r.y) && (y < (r.y + r.height))
);
}
void onMouseEvent(int evt, int x, int y, int flags, void *param)
{
if (evt == CV_EVENT_LBUTTONDOWN) {
// boundingBoxes is declared as a vector of CvRects and
// filled in the main loop using cvBoundingRect(contour)
for(UINT i = 0; i < boundingBoxes.size(); i++)
{
CvRect rect = boundingBoxes[i].rect;
if( pointInRect(x, y, rect) )
{
IplImage* img = getSubImage(imgSource, rect);
// Do whatever you want with the sub-image here
cvNamedWindow("Selection");
cvShowImage("Selection", img);
cvReleaseImage(&img);
break;
}
}
}
}

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;
}