OpenCV: Image window keeps hanging and not responding - c++

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);
}
//just for testing that we are getting pixel values
X1=points[0].x;
X2=points[1].x;
Y1=points[0].y;
Y2=points[1].y;
cout<<"First and second X coordinates are given below"<<endl;
cout<<X1<<'\t'<<X2<<endl;
cout<<"First and second Y coordinates are given below"<<endl;
cout<<Y1<<'\t'<<Y2<<endl;
// 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;
}
The problem is the program is not exiting if I close (by clicking the cross button on the image on top right )the "Output Window", instead it hangs and says Not responding.
How do I remove this problem ?

The reason that your application does not exit is that you have an infinite loop, and clicking on the cross to close a window does nothing to break that loop.
One way to exit is to test for a key being pressed, e.g.
while(true)
{
...
char c = cv::waitKey(10);
if(c == 'q')
break;
}
BTW, assuming you are on windows, if a window is destroyed waitKey() intercepts a WM_DESTROY message. In this case waitKey() returns message.wParam, but the windows doco for WM_DESTROY says This parameter is not used. It looks like a bug to me, but it might be worth investigating whether waitKey() returns a consistent value when the window is closed - normally -1 is returned if no key is pressed.

Related

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 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.

Drawing with MouseCallback in OpenCV

Using OpenCV 2.4.3, I am trying to draw a circle on top of an image centered on the mouse (x,y) position when the user is moving the mouse, and just that circle should be there once the mouse stops moving (only the original image with just one circle drawn should be shown in that moment). I thought it was going to be easy, however, I´ve been researching and trying for a couple of hours and can´t make it work the way I described.
Im attaching my code underneath. If anyone could help out I´d be really grateful.
void my_mouse_callback( int event, int x, int y, int flags, void* param );
bool moving_mouse = false;
int main()
{
const char* name = "Circle Example";
IplImage* image_circle = cvLoadImage( "../data/lena.png" );
IplImage* image = cvLoadImage( "../data/lena.png" );
namedWindow(name, CV_WINDOW_AUTOSIZE );
// Set up the callback
cvSetMouseCallback( name, my_mouse_callback, (void*) image_circle);
//Main Loop
while(cvWaitKey(15) != 27){
//If mouse is moving draw circle on top of image
if(moving_mouse){
cvShowImage(name, image_circle);
moving_mouse = false;
}
//If mouse stops moving draw original image and reset image_cicle
else{
cvShowImage(name, image);
image_circle = cvCloneImage(image);
}
}
cvReleaseImage(&image_circle);
cvReleaseImage(&image);
cvDestroyWindow(name);
return 0;
}
// Mouse callback
void my_mouse_callback( int event, int x, int y, int flags, void* param ){
switch( event ){
case CV_EVENT_MOUSEMOVE:
//Drawing a Circle
cvCircle(param,cvPoint(x,y),25,CV_RGB(0,255,0),1);
moving_mouse = true;
break;
}
}
I show you one approach to do this. I explain it later and tell you a difficulty you will have:
static int mouse_x = -1;
static int mouse_y = -1;
void my_mouse_callback( int event, int x, int y, int flags, void* param )
{
if(event == CV_EVENT_MOUSEMOVE)
{
mouse_x = x;
mouse_y = y;
}
}
int main()
{
IplImage* image;
IplImage* image_circle = NULL;
... // load image, create window, initiate callback, etc
int x = -1;
int y = -1;
while(cvWaitKey(15) != 27)
{
if(x != mouse_x || y != mouse_y)
{
x = mouse_x;
y = mouse_y;
cvReleaseImage(&image_circle);
image_circle = cvCloneImage(image);
cvCircle(image_circle,cvPoint(x,y),25,CV_RGB(0,255,0),1);
cvShowImage(name, image_circle);
}
}
... // destroy image
}
Explanation
Here, in the mouse event, you just store the coordinates of the mouse pointer. When an event takes place, the main program will check if the mouse moved to redraw the full image again. Since you want to erase the previous circle, you must copy the original image first to draw the new circle later. You could make this smarter by just copying the part of the original image that were the previous circle was, instead of the entire image.
Problem
A problem when doing something like this in OpenCV is that you can't detect when the mouse goes out of your window, so that you will always have a circle drawn in your image. I don't think you can solve this just with OpenCV, since I don't think there is a kind of MOUSE_OUT event. You would need to look for some Qt callback or system function.

Draw on webcam using OpenCV

I want to draw/paint on a webcam screen using OpenCV. Since I'm reading from a cam, the frames are constantly changing, so I'm trying to figure out a way to keep or save the drawing on the current frame and use it for the next frame. The code below allows you to draw on the screen but when it gets the next frame, the drawing is gone and it starts over.
Could someone please help me ... Thanks.
CvCapture *input;
input = cvCaptureFromCAM( 0 );
cvSetMouseCallback("Demo",&on_mouse, 0);
for(;;)
{
frame = cvQueryFrame(input);
if(!image)
{
image = cvCreateImage( cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3);
screenBuffer = cvCreateImage( cvSize(frame->width, frame->height), IPL_DEPTH_8U, 3);
}
cvCopy(frame, image, 0);
if(drawing) //drawing is a global variable
{
cvCircle(image, cvPoint(last_x,last_y), 10,CV_RGB(red,green,blue), -1, CV_AA, 0);
cvCopy(image, screenBuffer, 0);
}
cvShowImage( "Demo", screenBuffer );
}
void on_mouse( int event, int x, int y, int flags, void* param )
{
last_x = x;
last_y = y;
if(event==CV_EVENT_LBUTTONDOWN)
{
drawing = 1;
}
}
Draw into a separate image and then cvAdd() that to the video image immediately before dispalying it
I will not go into all the details why your approach is bad, but keep in mind that creating 2 extra frames for drawing is a little bit too much.
It's important that you realize that all this kinky stuff is being done on the same thread used to capture new frames. This means what exactly? It means that the extra code you are adding inside the loop will slow the process of capturing and displaying new frames. In other words, you are sabotaging yourself by lowering the framerate of your application. If you don't care, it's ok. If you do, my tip for you is that you stack the captured frames into a buffer and have another thread read, process and display them.
Ok, so you REALLY want to draw over the window that's displaying the captured frames. Well, the obvious thing you can't do (and you discovered this yourself) is that the drawing cannot be made on the captured frame because the frame it's replaced with new data on every loop. So what do you do? You create a 2nd frame to do the drawing. Let's call it the drawing_frame.
The only thing that will be on the drawing_frame are the circles that will appear when the mouse moves over the window, when the LBUTTON of the mouse is clicked (a 2nd click switches between ON/OFF).
After the drawing of the circle occurs, the drawing_frame is overlayed on top on the frame captured by the camera. This process is a little expensive on the CPU, and since we are doing it in the main thread of the application, it will also decrease the framerate.
I strongly suggest that everyone interested in adding/merging/overlaying transparent frames with OpenCV take a look at Transparent image overlays in OpenCV.
By the way, I'm using cvCaptureFromCAM(-1) becouse I'm on Linux. You probably should change that to whatever works for you. According to your post it's cvCaptureFromCAM(0).
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int drawing = 0;
int last_x = 0;
int last_y = 0;
void on_mouse(int event, int x, int y, int flags, void* param)
{
last_x = x;
last_y = y;
if (event == CV_EVENT_LBUTTONDOWN)
{
// switches between On and Off
if (drawing)
drawing = 0;
else
drawing = 1;
}
}
int main()
{
CvCapture* capture = NULL;
if ((capture = cvCaptureFromCAM(-1)) == NULL)
{
fprintf(stderr, "ERROR: capture is NULL \n");
return -1;
}
cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
cvQueryFrame(capture); // Sometimes needed to get correct data
cvSetMouseCallback("mywindow",&on_mouse, 0);
IplImage* frame = NULL;
IplImage* drawing_frame = NULL;
while (1)
{
if ((frame = cvQueryFrame(capture)) == NULL)
{
fprintf( stderr, "ERROR: cvQueryFrame failed\n");
break;
}
if (frame == NULL)
{
fprintf( stderr, "WARNING: cvQueryFrame returned NULL, sleeping..\n");
usleep(100000);
continue;
}
if (!drawing_frame) // This frame is created only once
{
drawing_frame = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, frame->nChannels);
cvZero(drawing_frame);
}
if (drawing)
{
cvCircle(drawing_frame, cvPoint(last_x,last_y), 10,CV_RGB(0, 255, 0), -1, CV_AA, 0);
// For overlaying (copying transparent images) in OpenCV
// http://www.aishack.in/2010/07/transparent-image-overlays-in-opencv/
for (int x = 0; x < frame->width; x++)
{
for (int y = 0; y < frame->height; y++)
{
CvScalar source = cvGet2D(frame, y, x);
CvScalar over = cvGet2D(drawing_frame, y, x);
CvScalar merged;
CvScalar S = { 1,1,1,1 };
CvScalar D = { 1,1,1,1 };
for(int i = 0; i < 4; i++)
merged.val[i] = (S.val[i] * source.val[i] + D.val[i] * over.val[i]);
cvSet2D(frame, y, x, merged);
}
}
}
cvShowImage("mywindow", frame);
int key = cvWaitKey(10);
if (key == 113) // q was pressed on the keyboard
break;
}
cvReleaseImage(&frame);
cvReleaseImage(&drawing_frame);
cvReleaseCapture(&capture);
cvDestroyWindow("mywindow");
return 0;
}
You usually will have problems of adding images (they will eventually saturate), so I guess thats why you start over. I see you have color images... if you use more powerful stuff like OpenGL for your drawing you could use the overlay for your drawings. Otherwise check this out:
http://aishack.in/tutorials/transparent-image-overlays-in-opencv/