【发布时间】:2014-07-06 20:14:05
【问题描述】:
以下程序将给出鼠标左键单击的位置。
void onMouse(int evt, int x, int y, int flags, void* param) {
if(evt == CV_EVENT_LBUTTONDOWN) {
cv::Point* ptPtr = (cv::Point*)param;
ptPtr->x = x;
ptPtr->y = y;
}
}
int main() {
cv::Point2i pt(-1,-1);//assume initial point
cv::namedWindow("Output Window");
Mat frame = cv::imread("chhhha.png");
cv::setMouseCallback("Output Window", onMouse, (void*)&pt);
int X, Y;
while(1)
{
cv::imshow("Output Window", frame);
X=pt.x;
Y=pt.y;
cout<<"X and Y coordinates are given below"<<endl;
cout<<X<<'\t'<<Y<<endl;
waitKey(10);
}
getch();
}
我想画一条连接用户点击的两点的线。我知道该函数可以画线:
C++: void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
但问题是我必须为这个函数提供两个点,但是我之前的点丢失了,如下代码所示:
void onMouse(int evt, int x, int y, int flags, void* param) {
if(evt == CV_EVENT_LBUTTONDOWN) {
cv::Point* ptPtr = (cv::Point*)param;
ptPtr->x = x;
ptPtr->y = y;
}
}
int main() {
cv::Point2i pt(-1,-1);
cv::namedWindow("Output Window");
Mat frame = cv::imread("chhhha.png");
cv::setMouseCallback("Output Window", onMouse, (void*)&pt);
int X, Y;
while(1)
{
cv::imshow("Output Window", frame);
X=pt.x;
Y=pt.y;
cout<<"X and Y coordinates are given below"<<endl;
cout<<X<<'\t'<<Y<<endl;
line(frame, pt1, pt2, 'r', 1, 8, 0); //here I am having only one point. This is the issue
waitKey(10);
}
getch();
}
编辑
那么无论如何都要存储用户点击的点的坐标。因此,假设用户单击图像上的两个点,我们会将两次单击的 x 坐标存储在 X[0] 和 X[1] 中,类似地,Y[0] 和 Y[1] 存储 y 坐标。
然后我可以轻松地使用画线功能。请帮助我朝着这个方向前进。
提前感谢您的建议。
我的最新代码
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 X=0, Y=0;
while(1)
{
cv::imshow("Output Window", frame);
X=points[0].x;
Y=points[0].y;
cout<<"First X and Y coordinates are given below"<<endl;
cout<<X<<'\t'<<Y<<endl;
waitKey(10);
}
getch();
}
这有两个主要问题:
1- 这可以很好地编译,但在运行时它会给出Debug Assertion Failed! 错误,
当我通过放置断点进行调试时,在以下几行:
X=points[0].x;
Y=points[0].y;
它进一步说:
表达式:向量下标超出范围
2- 如何退出 while 循环? 在其他类似的程序中,我注意到它永远保持在 while 循环中。
【问题讨论】: