如何在opencv中使用鼠标点击在图像上绘制线条
How do I draw lines on a image using mouse clicks in opencv?
因此,在遵循stackexchange用户关于鼠标事件的建议后,我能够理解并使用鼠标点击实现一些简单的任务。所以,下一个目标是用鼠标左键和鼠标右键画一条简单的线。不幸的是,在我实现我的程序后,我看不到任何行。
int x,y;
Point p(0,0);
Point q(0,0);
Mat xal;
void drawimage()
{
a = q.x - p.x; //i am just checking out the values of a and b to see if the drawimagefunction is being called in the rightmouse click event
b = q.y - p.y;
cout<<" a is :"<<a<<endl;
cout<<"b is:"<<b<<endl;
line(xal,Point(p.x,p.y),Point(q.x,q.y),Scalar(0,0,255),2,8);
}
void onMouse( int event, int x, int y, int f, void* )
{
switch (event)
{
case EVENT_LBUTTONDOWN:
cout<<"Left button was pressed"<<x<<" "<<y<<" "<<endl;
{
p.x = x;
p.y = y;
cout<<"p is:"<<p.x<<p.y<<endl;
}
break;
case EVENT_RBUTTONDOWN:
cout<<"Right button was pressed at :"<<x <<" "<<y<<endl;
{
q.x = x;
q.y = y;
drawimage();//no line is being drawn though i can see that i get the values of a and b in the drawimage function.
}
break;
default:
break;
}
}
int main()
{
xal = imread("pic.JPG);
namedWindow("Picture",1);
setMouseCallback("Picture",onMouse,NULL);
imshow("Picture",xal);
cvwaitkey(0);
}
在drawLine()函数中的"line(..)"调用后添加以下内容:
imshow("Picture", xal);
问题是,您正在将行写入xal矩阵,但尚未更新屏幕上的图像,这就是imshow(..)调用所要做的。
试试这段代码。它对你很有用。
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void drawimage()
{
line(xal,Point(p->x,p->y),Point(q->x,q->y),Scalar(0,0,255),2,8);
}
void CallBackFunc(int event, int x, int y, int flags, void *ptr )
{
if ( event == EVENT_LBUTTONDOWN )
{
Point*p = (Point*)ptr;
p->x = x;
p->y = y;
drawimage();
}
else if ( event == EVENT_RBUTTONDOWN )
{
Point*q = (Point*)ptr;
q->x = x;
q->y = y;
drawimage();
}
}
int main(int argc, char** argv)
{
// Read image from file
Point p;
Point q;
Mat xal = imread("MyPic.JPG");
//if fail to read the image
if ( xal.empty() )
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc,&p);
setMouseCallback("My Window", CallBackFunc,&q);
//show the image
imshow("My Window", xal);
// Wait until user press some key
waitKey(0);
return 0;
}
相关文章:
- 如何在 direct2d 中使用随其窗口重新缩放的效果绘制图像
- 获取位于安卓工作室中可绘制的图像的绝对路径,用于NDK项目
- Opencv,在图像中的对象周围绘制一个框
- 为什么在开始绘制获取的交换链图像时不需要内存障碍?
- OPENCV C .在for循环中在不同的图像像素上绘制一个圆圈(应在每个循环运行时将图像打开)
- 如何在圆圈内绘制图像
- 在绘制信号上保留绘图区域"图像"
- 如何使用GLADE和GTKMM C 重新绘制GTK ::图像组件
- 过剩无法绘制 2D 图像
- 尝试递归地重新绘制所有图像像素
- 从C 中的文件中读取图像,然后传递到Libharu的绘制方法
- SDL 不绘制图像
- 如何使用QGraphicsItem绘制图像?
- 绘制太大的图像无法适应Qimage
- 图像绘制使用NDK显示轮廓
- 打开不同的图像文件,并使用GDI 库在主应用程序窗口的背景上绘制它们
- 在灰度 (B/W) 图像上绘制彩色点
- 使用图像指针绘制图像
- QPixmap将绘制的正方形的内容复制到另一个图像中
- OpenCV将透明图像绘制到另一个图像上