con = np.array(contours) ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (84,) + inhomogeneous part.
时间: 2023-07-24 13:00:35 浏览: 70
这个错误说明在将`contours`转换为NumPy数组时出现了问题。出现这个错误的原因是`contours`中的元素具有不同的形状,导致无法创建一个均匀形状的NumPy数组。
要解决这个问题,你可以尝试使用`np.vstack()`函数来垂直堆叠`contours`中的元素,以创建一个具有一致形状的NumPy数组。示例如下:
```python
import numpy as np
con = np.vstack(contours)
con[:, 0] *= factor
```
通过`np.vstack(contours)`,我们将`contours`中的元素垂直堆叠成一个NumPy数组`con`,然后可以对`con[:, 0]`进行操作。请确保`factor`是一个数值类型的变量,并且与`con[:, 0]`的形状相匹配。
相关问题
triangle = cv2.minEnclosingTriangle(max_contour) triangle = np.int0(triangle)报错:valueerror
这个错误可能是由于`cv2.minEnclosingTriangle()`函数返回值的数据类型不是整数类型,而`cv2.drawContours()`函数需要的轮廓数据类型是整数型的。
为了解决这个错误,我们需要将`cv2.minEnclosingTriangle()`函数返回的三角形数据类型转换为整数型。可以使用`np.int0()`函数将其转换为整数型。
以下是修改后的代码示例:
```python
import cv2
import numpy as np
# 读取图片
img = cv2.imread('example.jpg')
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化处理
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 找到最大轮廓
max_contour = max(contours, key=cv2.contourArea)
# 计算最小外接三角形
triangle = cv2.minEnclosingTriangle(max_contour)
triangle = np.int0(triangle)
# 绘制最小外接三角形
cv2.drawContours(img, [triangle], 0, (0, 0, 255), 2)
# 显示结果
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在此示例中,我们使用`np.int0()`函数将三角形数据类型转换为整数型。
但是运行后报错:terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(4.5.0) /home/sniper/Downloads/opencv-4.5.0/modules/imgproc/src/contours.cpp:1799: error: (-215:Assertion failed) (_contours.kind() == _InputArray::STD_VECTOR_VECTOR || _contours.kind() == _InputArray::STD_VECTOR_MAT || _contours.kind() == _InputArray::STD_VECTOR_UMAT) in function 'findContours'
这个错误通常是由于传递给 `findContours()` 函数的轮廓参数类型不正确导致的。
在你的代码中,`findContours()` 函数的第一个参数是一个 `cv::Mat` 类型的矩阵,而第二个参数 `intersectionPolygon` 是一个 `std::vector<cv::Point>` 类型的向量。这里的问题是,`findContours()` 函数期望的第二个参数是一个用于存储轮廓的输出向量,而你传递了一个点的向量。因此,你需要将 `intersectionPolygon` 声明为一个 `std::vector<std::vector<cv::Point>>` 类型的向量,以存储轮廓。
更改后的代码如下所示:
```
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
bool allInside = true;
for (const auto& vertex : polygon1) {
double distance = cv::pointPolygonTest(polygon2, vertex, true);
if (distance < 0) {
allInside = false;
break;
}
}
if (allInside) {
return true;
}
// Polygon1 is partially or completely outside polygon2
std::vector<std::vector<cv::Point>> intersectionPolygon; // 修改此处
if (cv::isContourConvex(polygon1) && cv::isContourConvex(polygon2)) {
cv::Mat intersectionMat;
cv::intersectConvexConvex(cv::Mat(polygon1), cv::Mat(polygon2), intersectionMat);
if (cv::countNonZero(intersectionMat) > 0) {
cv::findContours(intersectionMat, intersectionPolygon, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
}
} else {
std::vector<cv::Point> hull1, hull2;
cv::convexHull(polygon1, hull1);
cv::convexHull(polygon2, hull2);
std::vector<cv::Point> hullIntersection;
cv::convexHull(hull1, hullIntersection, false, false);
cv::fillConvexPoly(cv::Mat(hullIntersection), hull2, cv::Scalar(0), false);
cv::findContours(cv::Mat(hullIntersection), intersectionPolygon, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
}
if (intersectionPolygon.empty()) {
outsideArea = 0;
return false;
}
double intersectionArea = std::abs(cv::contourArea(intersectionPolygon[0])); // 修改此处
double polygon1Area = std::abs(cv::contourArea(polygon1));
outsideArea = polygon1Area - intersectionArea;
return true; // 修改此处
}
```
你需要将 `intersectionPolygon` 的类型从 `std::vector<cv::Point>` 改为 `std::vector<std::vector<cv::Point>>`,以及在计算交集面积时使用 `intersectionPolygon[0]` 来获取轮廓。
另外,最后一行需要返回 true,因为在计算出多边形1在多边形2外部的面积后,函数应该返回 true。
相关推荐
![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)