有一个std::string "111111.png" 取出".png"
时间: 2024-06-05 22:05:03 浏览: 12
可以通过std::string的substr方法来截取字符串中的子串,代码如下:
```
std::string str = "111111.png";
std::string suffix = str.substr(str.find_last_of('.'));
```
其中,find_last_of方法用于查找最后一个"."的位置,substr方法则用于截取"."后面的子串,这样就能够得到字符串中的后缀名".png"了。
相关问题
QImage转std::string数据发送
QImage是Qt框架中用于处理图像的类,而std::string是C++标准库中用于处理字符串的类。要将QImage转换为std::string并发送数据,可以按照以下步骤进行操作:
1. 将QImage转换为QByteArray:
使用QImage的save()函数将图像保存到QByteArray中。可以指定保存的格式,如PNG、JPEG等。例如:
```
QImage image;
// 加载或创建图像
QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG"); // 保存为PNG格式
```
2. 将QByteArray转换为std::string:
使用QByteArray的data()函数获取指向数据的指针,并使用std::string的构造函数将其转换为std::string。例如:
```
std::string imageData(byteArray.data(), byteArray.size());
```
3. 发送std::string数据:
使用相应的网络通信库或方法将std::string数据发送出去。具体的发送方式取决于你使用的网络通信库或方法。
此代码中有以下报错信息,请修改: 1.while函数中报错“No matching function for call to 'drawResponse'” 2.No member named 'rect' in 'cv::linemod::Template' 3.No viable overloaded operator[] for type 'const std::vector<cv::Ptr<cv::linemod::Template>>' 4.主函数中detector->addTemplate部分、drawResponse部分、detector->getTemplates部分都少给了一个参数
非常抱歉,我之前的回答有一些错误。请使用以下修改后的代码:
```cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/linemod.hpp>
using namespace std;
using namespace cv;
void drawResponse(const std::vector<cv::linemod::Match>& matches, cv::Mat& dst, const std::vector<cv::Ptr<cv::linemod::Template>>& templates, const cv::Ptr<cv::linemod::Detector>& detector);
int main()
{
// 读取模板图片
cv::Ptr<cv::linemod::Detector> detector = cv::linemod::getDefaultLINE();
std::vector<cv::Mat> templateImages;
cv::Mat templateImage = cv::imread("template.png", 0);
if (templateImage.empty())
{
std::cout << "Failed to read template image!" << std::endl;
return -1;
}
templateImages.push_back(templateImage);
// 添加模板
std::string class_id = "template";
detector->addTemplate(templateImages, class_id);
// 读取视频
cv::VideoCapture cap("video.mp4");
if (!cap.isOpened())
{
std::cout << "Failed to open video!" << std::endl;
return -1;
}
// 创建窗口
cv::namedWindow("Template Matching", cv::WINDOW_NORMAL);
// 进行模板匹配
cv::Mat frame;
while (cap.read(frame))
{
// 转换为灰度图像
cv::Mat gray;
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
// 运行模板匹配
std::vector<cv::linemod::Match> matches;
detector->match(gray, 80, matches);
// 绘制匹配结果
drawResponse(matches, frame, detector->getTemplates(class_id), detector);
// 显示匹配结果
cv::imshow("Template Matching", frame);
// 按下ESC键退出
if (cv::waitKey(1) == 27)
break;
}
// 释放资源
cv::destroyAllWindows();
cap.release();
return 0;
}
void drawResponse(const std::vector<cv::linemod::Match>& matches, cv::Mat& dst, const std::vector<cv::Ptr<cv::linemod::Template>>& templates, const cv::Ptr<cv::linemod::Detector>& detector)
{
for (size_t i = 0; i < matches.size(); ++i)
{
const cv::linemod::Match& match = matches[i];
const std::vector<cv::linemod::Template>& classTemplates = templates[match.class_id]->getTemplates();
for (size_t j = 0; j < classTemplates.size(); ++j)
{
cv::Mat display = dst.clone();
cv::Rect rect = classTemplates[j].boundingRect;
cv::rectangle(display, rect, cv::Scalar(0, 255, 255));
cv::Point center(match.x, match.y);
cv::circle(display, center, 5, cv::Scalar(0, 0, 255), 2);
cv::imshow("Template Matching", display);
}
}
}
```
请将示例代码中的`template.png`和`video.mp4`替换为你自己的模板图片和视频文件路径。这些修改应该能够解决之前提到的问题。如果还有其他问题,请随时提问!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)