cv::mat 放到 vector 里取出 是empty
时间: 2023-07-29 14:05:48 浏览: 50
将 `cv::Mat` 对象存储到 `std::vector` 中,可以使用 `push_back()` 函数来添加元素。例如:
```cpp
cv::Mat image = cv::imread("image.jpg");
std::vector<cv::Mat> images;
images.push_back(image);
```
如果你想从 `std::vector` 中提取一个 `cv::Mat` 对象,可以使用 `at()` 函数。例如:
```cpp
cv::Mat image2 = images.at(0);
```
但是,如果你在从 `std::vector` 中提取 `cv::Mat` 对象之前没有检查它是否为空,可能会导致问题。你可以使用 `empty()` 函数检查 `cv::Mat` 对象是否为空。例如:
```cpp
if (!image2.empty()) {
// Do something with the image
} else {
// Image is empty
}
```
相关问题
C++ cv::imdecode怎么把字节流转成cv::Mat
要使用cv::imdecode将字节流转换为cv::Mat,可以按照以下步骤进行操作:
1. 首先,创建一个cv::Mat对象,用于存储解码后的图像数据。
2. 然后,将字节流存储在一个std::vector<uchar>容器中。
3. 调用cv::imdecode函数,将字节流解码为cv::Mat对象。
4. 检查解码是否成功,并处理可能的错误。
下面是一种可能的实现方式:
```cpp
// 假设字节流存储在一个名为data的std::vector<uchar>容器中
std::vector<uchar> data;
// 将字节流传递给cv::imdecode函数进行解码
cv::Mat image = cv::imdecode(data, cv::IMREAD_COLOR);
// 检查解码是否成功
if (image.empty()) {
// 处理解码错误的情况
// ...
} else {
// 解码成功,可以使用cv::Mat对象进行后续处理
// ...
}
```
请注意,这只是一种简单的实现方式,实际使用时可能需要根据具体情况进行调整。引用<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [CvArr、Mat、CvMat、IplImage、BYTE转换(总结而来)](https://blog.csdn.net/qq_22194315/article/details/77413563)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
bool isPolygonInside(const std::vector<cv::Point>& polygon1, const std::vector<cv::Point>& polygon2, double& outsideArea) { // Check if all vertices of polygon1 are inside polygon2 for (const auto& vertex : polygon1) { double distance = cv::pointPolygonTest(polygon2, vertex, true); if (distance < 0) { // Vertex is outside polygon2 // Calculate area of polygon1 outside polygon2 cv::Mat polygon1Mat = cv::Mat(polygon1).reshape(1); cv::Mat polygon2Mat = cv::Mat(polygon2).reshape(1); std::vector<cv::Point2f> intersectionPolygon; if (cv::isContourConvex(polygon1) && cv::isContourConvex(polygon2)) { cv::Mat intersectionMat; cv::intersectConvexConvex(polygon1Mat, polygon2Mat, intersectionMat); if (cv::countNonZero(intersectionMat) > 0) { intersectionMat.reshape(2).copyTo(intersectionPolygon); } } else { cv::Rect rect1 = cv::boundingRect(polygon1Mat); cv::Rect rect2 = cv::boundingRect(polygon2Mat); cv::Rect intersectionRect = rect1 & rect2; if (!intersectionRect.empty()) { cv::Mat intersectionMat = cv::Mat::zeros(intersectionRect.size(), CV_8UC1); cv::fillConvexPoly(intersectionMat, polygon1 - rect1.tl(), cv::Scalar(255)); cv::fillConvexPoly(intersectionMat, polygon2 - rect2.tl(), cv::Scalar(0), cv::LINE_AA); std::vector<std::vector<cv::Point>> contours; cv::findContours(intersectionMat, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); if (!contours.empty()) { intersectionPolygon = contours[0]; } } } double intersectionArea = std::abs(cv::contourArea(intersectionPolygon)); double polygon1Area = std::abs(cv::contourArea(polygon1)); outsideArea = polygon1Area - intersectionArea; return false; } } // All vertices of polygon1 are inside polygon2 return true; }
这段代码是用于判断一个多边形是否被另一个多边形包含,并计算出多边形1与多边形2之间的“外部面积”。其中,函数的输入参数是两个多边形的顶点坐标向量,输出参数是外部面积。函数的实现过程是,对于多边形1的每个顶点,计算其到多边形2的距离。如果该距离小于0,则判定该顶点在多边形2外部,需要计算多边形1在多边形2外部的面积。计算面积的方法是,先判断两个多边形是否都是凸多边形,如果是则使用cv::intersectConvexConvex函数计算两个多边形的交集;如果不是,则使用cv::boundingRect函数计算两个多边形的外接矩形,并在该矩形内部构造一个新的二值图像,将多边形1和多边形2分别填充到该二值图像中,并使用cv::findContours函数找到交集多边形的轮廓。最后,使用cv::contourArea函数计算多边形1和交集多边形的面积,相减即为多边形1在多边形2外部的面积。如果多边形1的所有顶点都在多边形2内部,则返回true,否则返回false。