cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'imshow' > Overload resolution failed: > - mat is not a numpy array, neither a scalar > - Expected Ptr<cv::cuda::GpuMat> for argument 'mat' > - Expected Ptr<cv::UMat> for argument 'mat'
时间: 2023-06-09 19:04:10 浏览: 1390
这个错误是由于imshow函数的参数不正确而引起的。根据错误信息,有可能是你传递给函数的参数不是一个numpy数组,也可能是mat参数需要传递一个指向cv::cuda::GpuMat或cv::UMat的指针。请检查你的代码,确保传递给imshow函数的参数是numpy数组或者是正确的指针类型,或者提供更多的代码信息以便我更好地帮助你解决问题。
相关问题
cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'boundingRect' > Overload resolution failed: > - array is not a numerical tuple > - Expected Ptr<cv::UMat> for argument 'array'
这个错误通常是由于cv2.findContours()函数返回的轮廓列表为空,导致cv2.boundingRect()函数无法计算边界框而引起的。请确保在调用cv2.findContours()函数之前,使用cv2.threshold()或其他图像分割方法,将图像转换为二进制形式。另外,还需要确保使用的图像是正确的类型(例如,8位或32位浮点数),以及使用正确的数组格式(例如,numpy数组或OpenCV的UMat)。以下是一个更新后的示例代码:
```
import cv2
import numpy as np
# 读取图像并转换为HSV颜色空间
img = cv2.imread('image.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# 定义红色的HSV范围
lower_red = np.array([0, 50, 50])
upper_red = np.array([10, 255, 255])
mask1 = cv2.inRange(hsv, lower_red, upper_red)
lower_red = np.array([170, 50, 50])
upper_red = np.array([180, 255, 255])
mask2 = cv2.inRange(hsv, lower_red, upper_red)
mask = cv2.bitwise_or(mask1, mask2)
# 将图像转换为二进制形式
_, binary = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
# 查找红色矩形的轮廓并确定其边界框
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
x, y, w, h = cv2.boundingRect(contours[0])
# 将矩形分成4个子矩形
sub_w = int(w / 2)
sub_h = int(h / 2)
centers = [(x + sub_w, y + sub_h), (x + sub_w, y + 3 * sub_h), (x + 3 * sub_w, y + sub_h), (x + 3 * sub_w, y + 3 * sub_h)]
# 绘制子矩形
for center in centers:
x, y = center
cv2.rectangle(img, (x - sub_w, y - sub_h), (x + sub_w, y + sub_h), (0, 255, 0), 2)
# 显示结果图像
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print('未找到红色矩形!')
```
如果问题仍然存在,请检查输入图像的位置和格式是否正确,并尝试使用不同的阈值或分割方法来处理图像。
cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'circle' > Overload resolution failed: > - Can't parse 'center'. Sequence item with index 0 has a wrong type
抱歉,出现了错误。根据错误信息,看起来是在绘制圆形时出现了问题。这可能是由于传递给 `cv2.circle()` 函数的参数类型不正确导致的。
请确保传递给 `cv2.circle()` 函数的参数 `center` 是一个元组,其中第一个元素表示圆心的 x 坐标,第二个元素表示圆心的 y 坐标。你可以使用 `tuple` 函数将 `x` 和 `y` 转换为元组,并传递给 `cv2.circle()` 函数。
下面是修正后的代码示例:
```python
import cv2
import numpy as np
def detect_corners(image_path):
# 读取图像
image = cv2.imread(image_path)
# 将图像转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 检测角点
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
# 输出每个角点的坐标值
for corner in corners:
x, y = corner.ravel()
print(f"Corner coordinate: ({x}, {y})")
# 在图像上绘制角点
corners = np.int0(corners)
for corner in corners:
x, y = corner.ravel()
center = tuple([x, y]) # 将 x 和 y 转换为元组
cv2.circle(image, center, 3, (0, 255, 0), -1)
# 显示标记了角点的图像
cv2.imshow("Corners", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 调用函数进行角点检测和输出坐标值
image_path = 'image.jpg'
detect_corners(image_path)
```
请尝试使用这个修正后的代码,并确保传递给 `cv2.circle()` 函数的参数类型正确。如果还有其他问题,请随时提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)