Cv2.WaitKey(1) != (char)27
时间: 2023-12-19 09:06:17 浏览: 53
在OpenCV中,`cv2.waitKey(1)`的作用是等待1毫秒,以便用户可以按下键盘上的按键。如果用户按下了某个按键,则会返回该按键的ASCII码值;否则返回-1。因此,如果想要检测用户是否按下了ESC键,可以使用以下代码:
```python
import cv2
# ...
while True:
# ...
key = cv2.waitKey(1)
if key == 27: # ESC键的ASCII码值为27
break
```
在C#中,`cv2.waitKey(1)`的返回值是一个整数,表示用户按下的键的ASCII码值。因此,如果要检测用户是否按下了ESC键,可以将其转换为char类型,并与字符'27'进行比较。以下是一个示例代码:
```C#
using OpenCvSharp;
// ...
while (true)
{
// ...
int key = Cv2.WaitKey(1);
if ((char)key == '27')
{
break;
}
}
```
需要注意的是,`cv2.waitKey()`方法在不同的操作系统和不同的OpenCV版本中可能会有所不同,因此具体的使用方法可能会有所差异。
相关问题
把下面的代码用c++实现 import numpy as np import torch import cv2 result = np.fromfile('permute_1_0.raw', dtype=np.float32) img = result.reshape((2,480,480)) torch_img = torch.from_numpy(img) index = torch.max(torch_img, dim=0)[1].numpy().astype(np.uint8) index[index > 0] = 255 # 白色 cv2.imshow('out', index) cv2.waitKey(0)
以下是用C++实现的代码:
```
#include <opencv2/opencv.hpp>
#include <torch/torch.h>
int main() {
// 读取数据
std::ifstream input("permute_1_0.raw", std::ios::binary);
std::vector<float> data(2 * 480 * 480);
input.read(reinterpret_cast<char*>(data.data()), data.size() * sizeof(float));
input.close();
// 转换数据类型
torch::Tensor tensor = torch::from_blob(data.data(), {2, 480, 480}, torch::kFloat32).clone();
// 获取最大值索引
torch::Tensor max = std::get<0>(torch::max(tensor, 0));
torch::Tensor index = max.argmax(0).to(torch::kUInt8);
// 去除背景
index.masked_fill_(index == 0, 255);
// 转换为OpenCV格式
cv::Mat out(index.sizes()[0], index.sizes()[1], CV_8UC1, index.data_ptr());
// 显示图像
cv::imshow("out", out);
cv::waitKey(0);
return 0;
}
```
注意需要安装OpenCV和PyTorch C++ API,并且需要将`permute_1_0.raw`文件放在当前目录下。
把下面的代码用c++实现,不使用torch import numpy as np import torch import cv2 result = np.fromfile('permute_1_0.raw', dtype=np.float32) img = result.reshape((2,480,480)) torch_img = torch.from_numpy(img) index = torch.max(torch_img, dim=0)[1].numpy().astype(np.uint8) index[index > 0] = 255 # 白色 cv2.imshow('out', index) cv2.waitKey(0)
以下是使用 C++ 实现的代码:
```cpp
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <torch/torch.h>
using namespace std;
using namespace cv;
int main() {
// 读取二进制文件
ifstream fin("permute_1_0.raw", ios::in | ios::binary);
if (!fin) {
cerr << "Failed to open file." << endl;
return -1;
}
// 读取数据到数组
const int channels = 2;
const int height = 480;
const int width = 480;
float data[channels * height * width];
fin.read(reinterpret_cast<char*>(data), channels * height * width * sizeof(float));
fin.close();
// 将数组转为 OpenCV Mat 对象
Mat img(channels, cv::Size(width, height), CV_32F, data);
// 将 Mat 转为 Tensor 对象
auto tensor_img = torch::from_blob(img.data, {channels, height, width}, torch::kFloat).clone();
// 求取最大值对应的索引
auto indices = torch::argmax(tensor_img, 0, false).to(torch::kByte);
// 将 Tensor 转为 OpenCV Mat 对象
Mat index(height, width, CV_8UC1, indices.data_ptr());
// 处理结果并显示
threshold(index, index, 0, 255, THRESH_BINARY);
imshow("out", index);
waitKey(0);
return 0;
}
```
需要注意的是,本例中使用了 OpenCV 和 PyTorch C++ API。在编译时需要添加相应的头文件和库文件,并确保环境配置正确。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)