opencv set mouse callback - opencv

I'm a beginner in OpenCV and would like to print in my console the specific value of a pixel (RGB format) that I define by clicking on it.
After some searches I managed to prind the coordinates of the click I make on the image.
If anyone knows how to do that please modify this code that I am using:
void mouseEvent (int evt, int x, int y, int flags, void* param)
{
if (evt == CV_EVENT_LBUTTONDOWN)
{
printf("%d %d\n",x ,y );
}
}
and this is what I use to call the function:
cvSetMouseCallback("blah blah", mouseEvent, 0);

Place your image in a Mat called frame, then:
namedWindow("test");
cvSetMouseCallback("test", mouseEvent, &frame);
char key = 0;
while ((int)key != 27) {
imshow("test", frame);
key = waitKey(1);
}
where mouseEvent is defined as:
void mouseEvent(int evt, int x, int y, int flags, void* param) {
Mat* rgb = (Mat*) param;
if (evt == CV_EVENT_LBUTTONDOWN) {
printf("%d %d: %d, %d, %d\n",
x, y,
(int)(*rgb).at<Vec3b>(y, x)[0],
(int)(*rgb).at<Vec3b>(y, x)[1],
(int)(*rgb).at<Vec3b>(y, x)[2]);
}
}

Related

Is it possible to set OpenCV Mouse Call back on two windows simultaneously?

Is it possible to set OpenCV Mouse call-back on two different windows simultaneously such that it can decide which window the mouse is on ?
Yes, it is possible:
void onMouse(int event, int x, int y, int, void* userData)
{
int * windowID = (int*)userData;
std::cout << "mouse in window: " << (*windowID) << std::endl;
if((*windowID) == 1)
{
// do whatever you want to do for window 1
}
else if((*windowID) == 2)
{
// do whatever you want to do for window 2
}
}
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png");
cv::imshow("input1", input);
cv::imshow("input2", input);
int id1 = 1;
int id2 = 2;
cv::setMouseCallback("input1", &onMouse, &id1);
cv::setMouseCallback("input2", &onMouse, &id2);
cv::waitKey(0);
return 0;
}
Those are my outputs (unfortunately mouse cursor not visible...):

WINDOW_AUTOSIZE in namedWindow() method

I want to enable the user to be able to draw rectangles and define regions in the image window. The trouble is that the image has to strictly be of A4 size dimensions which is why I have used the WINDOW_AUTOSIZE mode while calling namedWindow. But this seems to mess up the points P1 and P2 that are used to make the rectangle. This somehow doesn't happen with WINDOW_NORMAL mode.
Here is the code:
using namespace std;
using namespace cv;
cv::Point P1;
cv::Point P2;
Mat temp;
void CallBack(int event, int x, int y, int flags, void* userdata) {
if(event == CV_EVENT_LBUTTONDOWN) {
P1.x = x;
P1.y = y;
}
else if(event == CV_EVENT_LBUTTONUP) {
P2.x = x;
P2.y = y;
rectangle(temp, P1, P2, Scalar(0, 0, 0), 1);
}
}
int main() {
temp = imread("template.png");
namedWindow("Templating", CV_WINDOW_AUTOSIZE);
imshow("Templating", temp);
cv::setMouseCallback("Templating", CallBack, NULL);
waitKey(0);
imshow("Templating", temp);
waitKey(0);
return 0;
}
Thanks in advance!!

Libgdx Pixmap Memory Leak

I am codeing a little project where i need a Line from a given Object to my Mouse. I made things work and came up with this quick and dirty code:
addListener(new ClickListener() {
Image lineImage;
Pixmap pixmap;
#Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
// Get Actor Origin
// Get local Origin
int x2 = (int) event.getListenerActor().getX(Align.center);
int y2 = (int) event.getListenerActor().getY(Align.center);
// Make it global
x2 = (int) event.getListenerActor().getParent().getX() + x2;
y2 = (int) event.getListenerActor().getParent().getY() + y2;
// Get Stage Coordinates
Vector2 v = localToStageCoordinates(new Vector2(x, y));
Vector2 v2 = new Vector2(x2, y2);
Stage stage = event.getStage();
int width = (int) stage.getWidth();
int height = (int) stage.getHeight();
if (pixmap == null) {
pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
} else {
pixmap.setColor(1, 1, 1, 0);
pixmap.fill();
}
pixmap.setColor(Color.BLUE);
// line
for (int m = -2; m <= 2; m++) {// x
for (int n = -2; n <= 2; n++) {// y
pixmap.drawLine((int) (v2.x+m), (int) (height-v2.y+n) , (int) (v.x+m), (int) (height-v.y+n));
}
}
if (lineImage != null) {
/*lineImage.clear();
lineImage.remove();
*/
lineImage.setDrawable(new SpriteDrawable(new Sprite(new Texture(pixmap))));
} else {
lineImage = new Image(new Texture(pixmap));
}
lineImage.setPosition(0,0);
stage.addActor(lineImage);
// super.touchDragged(event, x, y, pointer);
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
if (lineImage != null) {
lineImage.clear();
lineImage.remove();
}
lineImage = null;
super.touchUp(event, x, y, pointer, button);
}
});
The Problem with this is, when i use this Listener on a Image and i activate touchdragged for about 20 seconds, there will be a memory leak.
I have no idea why this happens, i tried a lot of things but nothing seams to help me fix this. Do you have any ideas?
#noone is right. Add the line where is commented to dispose your pixmap after you assigned the drawable to the lineImage.
if (lineImage != null) {
/*lineImage.clear();
lineImage.remove();
*/
lineImage.setDrawable(new SpriteDrawable(new Sprite(new Texture(pixmap))));
} else {
lineImage = new Image(new Texture(pixmap));
}
pixmap.dispose(); // <-----------Add this line here!!!
lineImage.setPosition(0,0);
stage.addActor(lineImage);

opencv r6010 abort() has been called error in visual studio 2013

I have some code to draw a line between two points on an image which are selected by mouse, and then to display a histogram.
However, when I press q as required by code I get an error saying R6010 abort() has been called and saying VC++ run time error.
Please advise me how I can find this error.
#include <vector>
#include "opencv2/highgui/highgui.hpp"
#include <opencv\cv.h>
#include <iostream>
#include<conio.h>
using namespace cv;
using namespace std;
struct Data_point
{
int x;
unsigned short int y;
};
int PlotMeNow(unsigned short int *values, unsigned int nSamples)
{
std::vector<Data_point> graph(nSamples);
for (unsigned int i = 0; i < nSamples; i++)
{
graph[i].x = i;
graph[i].y = values[i];
}
cv::Size imageSize(5000, 500); // your window size
cv::Mat image(imageSize, CV_8UC1);
if (image.empty()) //check whether the image is valid or not
{
std::cout << "Error : Image cannot be created..!!" << std::endl;
system("pause"); //wait for a key press
return 0;
}
else
{
std::cout << "Good job : Image created successfully..!!" << std::endl;
}
// tru to do some ofesseting so the graph do not hide on x or y axis
Data_point dataOffset;
dataOffset.x = 20;
// we have to mirror the y axis!
dataOffset.y = 5000;
for (unsigned int i = 0; i<nSamples; ++i)
{
graph[i].x = (graph[i].x + dataOffset.x) * 3;
graph[i].y = (graph[i].y + dataOffset.y) / 200;
}
// draw the samples
for (unsigned int i = 0; i<nSamples - 1; ++i)
{
cv::Point2f p1;
p1.x = graph[i].x;
p1.y = graph[i].y;
cv::Point2f p2;
p2.x = graph[i + 1].x;
p2.y = graph[i + 1].y;
cv::line(image, p1, p2, 'r', 1, 4, 0);
}
cv::namedWindow("MyWindow1", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
cv::imshow("MyWindow1", image); //display the image which is stored in the 'img' in the "MyWindow" window
while (true)
{
char c = cv::waitKey(10);
if (c == 'q')
break;
}
destroyWindow("MyWindow1");
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
void IterateLine(const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int* count1)
{
LineIterator it(image, p2, p1, 8);
for (int i = 0; i < it.count; i++, it++)
{
linePixels.push_back(image.at<ushort>(it.pos())); //doubt
}
*count1 = it.count;
}
//working line with mouse
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));
}
}
void drawline(Mat image, std::vector<Point>& points)
{
cv::namedWindow("Output Window");
cv::setMouseCallback("Output Window", onMouse, (void*)&points);
int X1 = 0, Y1 = 0, X2 = 0, Y2 = 0;
while (1)
{
cv::imshow("Output Window", image);
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;
// Draw a line
line(image, Point(X1, Y1), Point(X2, Y2), 'r', 2, 8);
cv::imshow("Output Window", image);
//exit image window
while (true)
{
char c = cv::waitKey(10);
if (c == 'q')
break;
}
destroyWindow("Output Window");
}
void show_histogram_image(Mat img1)
{
int sbins = 65536;
int histSize[] = { sbins };
float sranges[] = { 0, 65536 };
const float* ranges[] = { sranges };
cv::MatND hist;
int channels[] = { 0 };
cv::calcHist(&img1, 1, channels, cv::Mat(), // do not use mask
hist, 1, histSize, ranges,
true, // the histogram is uniform
false);
double maxVal = 0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
int xscale = 10;
int yscale = 10;
cv::Mat hist_image;
hist_image = cv::Mat::zeros(65536, sbins*xscale, CV_16UC1);
for int s = 0; s < sbins; s++)
{
float binVal = hist.at<float>(s, 0);
int intensity = cvRound(binVal * 65535 / maxVal);
rectangle(hist_image, cv::Point(s*xscale, hist_image.rows),
cv::Point((s + 1)*xscale - 1, hist_image.rows - intensity),
cv::Scalar::all(65535), 1);
}
imshow("Histogram", hist_image);
waitKey(0);
}
int main()
{
vector<Point> points1;
vector<ushort>linePixels;
Mat img = cvLoadImage("desert.jpg");
if (img.empty()) //check whether the image is valid or not
{
cout << "Error : Image cannot be read..!!" << endl;
system("pause"); //wait for a key press
return -1;
}
//Draw the line
drawline(img, points1);
//now check the collected points
Mat img1 = cvLoadImage("desert.jpg");
if (img1.empty()) //check whether the image is valid or not
{
cout << "Error : Image cannot be read..!!" << endl;
system("pause"); //wait for a key press
return -1;
}
int *t = new int;
IterateLine( img1, linePixels, points1[1], points1[0], t );
PlotMeNow(&linePixels[0], t[0]);
show_histogram_image(img);
delete t;
_getch();
return 0;
}
This is one of the bad smells in your code:
void IterateLine(const Mat& image, vector<ushort>& linePixels, Point p2, Point p1, int* count1)
{
...
linePixels.push_back(image.at<ushort>(it.pos())); //doubt
Now image is a CV_8UC3 image (from Mat img1 = cvLoadImage("desert.jpg");, but you are accessing here like it is CV_16UC1, so what gets put in linePixels is garbage. This will almost certainly cause PlotMeNow() to draw outside its image and corrupt something, which is probably why your code is crashing.
Sine it is very unclear what your code is trying to do, I can't suggest what you should have here instead.
I have just managed to do this, you only have to put "-1" to your loop limit:
for (unsigned int i = 0; i < nSamples-1; i++)
{
graph[i].x = i;
graph[i].y = values[i];
}

Using mouse to paint an area of video with OpenCV

I want to paint an area of my video using mouse when the video is paused, but I'm having a little problem. When I pause the video and try to paint a region, the painted area only appears after I press any key on my keyboard (which is not implemented it). I wonder what I could do to the painted area appears when I press the mouse button?
Thank you, : )
My code:
//mouse callback
void rotina_mouse(int event, int x, int y, int flags, void* param);
bool continua = false;
//function to paint
void pinta(IplImage* image, int x, int y){
cvFloodFill (image, cvPoint (x,y), cvScalar(103), cvScalarAll(2), cvScalarAll(2), 0, CV_FLOODFILL_FIXED_RANGE , 0);
}
//main program
int _tmain(int argc, _TCHAR* argv[])
{
cvNamedWindow ("saida", CV_WINDOW_AUTOSIZE);
CvCapture* g_capture = cvCreateFileCapture ("vid.avi");
IplImage* frame = cvQueryFrame(g_capture);
IplImage* temp = cvCloneImage( frame );
cvSetMouseCallback("saida",rotina_mouse,(void*) frame);
while(1){
frame = cvQueryFrame(g_capture);
cvNot(frame, frame);
cvCopyImage( frame, temp );
cvShowImage("saida", temp);
if(!frame) break;
//pause with 'p'
char e = cvWaitKey(33);
if(e==112){
while(1){
cvCopyImage( frame, temp );
cvShowImage("saida", temp);
char d = cvWaitKey(0);
if(d==112) break;
}
}
//close video with'esc'
if(e==27) break;
}
cvReleaseCapture (&g_capture);
cvDestroyWindow("saida");
return 0;
}
//mouse callback
void rotina_mouse(int event, int x, int y, int flags, void* param) {
IplImage* image = (IplImage*) param;
switch( event ) {
case CV_EVENT_MOUSEMOVE: {
if(continua==true)
pinta(image, x, y);
}
break;
case CV_EVENT_LBUTTONDOWN: {
pinta(image, x, y);
continua=true;
}
break;
case CV_EVENT_LBUTTONUP: {
continua=false;
}
break;
default:
break;
}
}
Call cv::imshow (from inside the callback).

Resources