以下代码发生TypeError: Expected Ptrcv::UMat for argument 'mat',代码如下: def on_pushButton_5_clicked(self): # 读取左相机图像 left_image_path = '1_left.JPG' # 替换为实际图像的路径 left_image = cv2.imread(left_image_path) # 转换为HSV颜色空间 hsv_image = cv2.cvtColor(left_image, cv2.COLOR_BGR2HSV) # 定义红色的HSV颜色范围 lower_red = np.array([0, 100, 100]) upper_red = np.array([10, 255, 255]) # 对图像进行红色阈值处理 red_mask = cv2.inRange(hsv_image, lower_red, upper_red) # 执行形态学操作,去除噪声 kernel = np.ones((5, 5), np.uint8) red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_OPEN, kernel) # 查找红色轮廓 contours, _ = cv2.findContours(red_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 保留最大的两个轮廓 contours = sorted(contours, key=cv2.contourArea, reverse=True)[:2] # 遍历轮廓并绘制圆心和坐标 for contour in contours: # 计算轮廓的最小外接圆 (x, y), radius = cv2.minEnclosingCircle(contour) center = (int(x), int(y)) radius = int(radius) # 绘制圆心 cv2.circle(left_image, center, 3, (0, 255, 0), -1) # 绘制圆形轮廓 cv2.circle(left_image, center, radius, (0, 0, 255), 2) # 绘制坐标 text = f'({int(x)}, {int(y)})' cv2.putText(left_image, text, (int(x) + 10, int(y) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.imshow('Result', cv2.circle) cv2.waitKey() showImg = cv2.cvtColor('image', cv2.COLOR_BGR2RGB) qImgae = QImage(showImg, showImg.shape[1], showImg.shape[0], showImg.shape[1]*3, QImage.Format_RGB888) self.label.setPixmap(QPixmap(qImage).scaled(self.label.width(), self.label.height(), Qt.KeepAspectRatio))
时间: 2023-11-12 20:03:38 浏览: 177
河南平顶山接口文档2
这个错误通常是由于cv2.imshow()函数的参数不正确导致的。在你的代码中,cv2.imshow()函数的参数应该是left_image,而不是cv2.circle。另外,你还需要将cv2.imshow()函数放在cv2.waitKey()函数之前,否则窗口将无法正常显示。修改后的代码如下:
```
def on_pushButton_5_clicked(self):
# 读取左相机图像
left_image_path = '1_left.JPG' # 替换为实际图像的路径
left_image = cv2.imread(left_image_path)
# 转换为HSV颜色空间
hsv_image = cv2.cvtColor(left_image, cv2.COLOR_BGR2HSV)
# 定义红色的HSV颜色范围
lower_red = np.array([0, 100, 100])
upper_red = np.array([10, 255, 255])
# 对图像进行红色阈值处理
red_mask = cv2.inRange(hsv_image, lower_red, upper_red)
# 执行形态学操作,去除噪声
kernel = np.ones((5, 5), np.uint8)
red_mask = cv2.morphologyEx(red_mask, cv2.MORPH_OPEN, kernel)
# 查找红色轮廓
contours, _ = cv2.findContours(red_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 保留最大的两个轮廓
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:2]
# 遍历轮廓并绘制圆心和坐标
for contour in contours:
# 计算轮廓的最小外接圆
(x, y), radius = cv2.minEnclosingCircle(contour)
center = (int(x), int(y))
radius = int(radius)
# 绘制圆心
cv2.circle(left_image, center, 3, (0, 255, 0), -1)
# 绘制圆形轮廓
cv2.circle(left_image, center, radius, (0, 0, 255), 2)
# 绘制坐标
text = f'({int(x)}, {int(y)})'
cv2.putText(left_image, text, (int(x) + 10, int(y) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
# 显示结果图像
cv2.imshow('Result', left_image)
cv2.waitKey()
# 将图像显示在Qt中
showImg = cv2.cvtColor(left_image, cv2.COLOR_BGR2RGB)
qImage = QImage(showImg, showImg.shape[1], showImg.shape[0], showImg.shape[1]*3, QImage.Format_RGB888)
self.label.setPixmap(QPixmap(qImage).scaled(self.label.width(), self.label.height(), Qt.KeepAspectRatio))
```
阅读全文