c++ - 如何在 C++ 中使用自定义形状的光标检测图像上的鼠标单击位置

标签 c++ windows windows-forms-designer custom-component

在我的问题中,有一张图片,我需要让用户在该图片中选择某个特定位置。为此,我需要提供一个带有光标的方形(由我自己定制的宽度和高度)。然后用户只想将其放在给定图像的位置并单击。然后我想占据那个位置。任何有这种经验的人都可以用 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/

相关文章:

c++ - CEIL 对于精确的整数除法来说太高了

c++ - 混合 C++ ABI 以针对遗留库进行构建

c++ - stringstream 和 nullptr 的子类

c++ - 这感觉有点反模式,有没有更好的方法(C++ - 分配给引用)

windows - 如何避免在远程调试时加载windows dll 的符号?

c# - VS 设计时错误 - 类型 'ICustomType[]' 的对象无法转换为类型 'ICustomType[]'

c# - 如何创建一个可以在设计时订阅另一个 WinForm 控件的 C# 属性?

c# - 处理我刚刚用 ActiveMQ 和 C# 发送的消息

c++ - 使用 Windows API 将波形音频转储到标准输出

windows - 杂技 Actor (阅读器): to open at a specific page number via command line on Windows