首页 > 解决方案 > 有没有按不同按钮改变操作的功能

问题描述

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

例如,如果我按 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;
}

请在这件事上给予我帮助

标签: c++opencv

解决方案


我的建议是引入“比例”变量,该变量将通过按键进行修改,以计算两条线的起点和终点。假设这些点被定义为[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;
}

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


推荐阅读