c++ - 有没有按不同按钮改变操作的功能
问题描述
我想在屏幕中间显示一个十字,并按下键盘上的一些键来改变它的大小。
例如,如果我按 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;
}
请在这件事上给予我帮助
解决方案
我的建议是引入“比例”变量,该变量将通过按键进行修改,以计算两条线的起点和终点。假设这些点被定义为[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++ 编译器。
推荐阅读
- python-3.x - 为什么我打印的条形码在 Odoo 12 中是错误的?
- shell - 从 Shell 脚本中的字符串获取文件名
- swift - 如何更改导航栏的背景颜色
- python - 在 pandas DataFrame 中使用索引进行分组和聚合
- javascript - 打开html
- google-tag-manager - 如何在谷歌标签管理器中定义 google_tag_params?
- reactjs - 当我的数据为布尔值时,在 mui-datatables 中的 switch 中显示表数据
- ionic-framework - 离子 g 管道搜索过滤器在 ionic 5 中不起作用
- javascript - 如何使用 JavaScript 按钮来提醒其他哪些按钮当前处于活动状态?
- javascript - onChange 在每次击键时触发