是否有任何功能可以通过按不同的按钮来更改操作

Is there any function for changing the operation by pressing different buttons

本文关键字:按钮 操作 任何 功能 可以通过 是否      更新时间:2023-10-16

我想在屏幕中间显示一个十字,并按键盘上的一些键更改它的大小。

例如如果我按 b ,十字架应该变大。如果我按 s,十字架应该变小。如果我按 m,十字架应该变成中等。

#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;

int main()
{
    VideoCapture cap(0);
    while(true)
    {
        Mat frame;
        // Capture frame-by-frame
        cap >> frame;
        // If the frame is empty, break immediately
        if (frame.empty())
            break;
        // for converting the frame to grayscale  
        Mat gray;
        cvtColor( frame, gray , COLOR_BGR2GRAY);
        line( frame, Point( 300, 240 ), Point( 340,240), Scalar( 0, 0, 255 ),  2, 4 );
        line( frame, Point( 320, 260), Point( 320, 220), Scalar( 0, 0, 255 ), 2, 4);
        imshow(" figure ",frame);
        char c=(char)waitKey(25);
        if(c==27)
            break;
    }
    cap.release();
    destroyAllWindows();
    return 0;
}

请帮我解决这个问题

我的建议是引入"scale"变量,该变量将通过按键修改以计算两条线的起点和终点。假设这些点被定义为 [start point] = [middle point] - [scale] * [scale factor][end point] = [middle point] + [scale] * [scale factor] .所以它看起来像:

VideoCapture cap(0);
int size = 2;
bool drawCross = 1;
while(true)
{
    Mat frame;
    // Capture frame-by-frame
    cap >> frame;
    // If the frame is empty, break immediately
    if (frame.empty())
        break;
    // for converting the frame to grayscale  
    Mat gray;
    cvtColor( frame, gray , COLOR_BGR2GRAY);
    if (drawCross) {
        line( frame, Point( 320 - 10*size, 240 ), Point( 320 + 10*size,240), Scalar( 0, 0, 255 ),  2, 4 );
        line( frame, Point( 320, 240 - 10*size), Point( 320, 240 + 10*size), Scalar( 0, 0, 255 ), 2, 4);
    }
    imshow(" figure ",frame);
    char c=(char)waitKey(25);
    if(c==27)
        break;
    else if (c==[whatever this "small" key is])
        size = 1;
    else if (c==[whatever this "medium" key is])
        size = 2;
    else if (c==[whatever this "large" key is])
        size = 4;
    else if (c==[whatever this "do not draw cross" key is])
        drawCross = !drawCross;
}
==

编辑==解决方案现已修复,可与下面评论中给出的新"要求"一起使用。这是我对这个问题的最后一次输入,因为SO不是"你能为我写吗?"类型的社区。您在评论中描述的问题需要我长达两分钟的谷歌搜索。在继续之前,您需要阅读编程基础知识,例如条件分支之类的。而且我不知道这段代码是否 100% 有效,因为我目前不想安装C++编译器。