在我的问题中,有一张图片,我需要让用户在该图片中选择某个特定位置。为此,我需要提供一个带有光标的方形(由我自己定制的宽度和高度)。然后用户只想将其放在给定图像的位置并单击。然后我想占据那个位置。任何有这种经验的人都可以用 C++ Windows 窗体中的示例代码指导我吗?
最佳答案
这是解决这个问题的理想方式。引用这个来源
#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;
}
关于c++ - 如何在 C++ 中使用自定义形状的光标检测图像上的鼠标单击位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34194605/