opencv - 图像编码接受度在运行时发生变化
问题描述
我有一个将数据输入深度学习模型的相机。该模型评估图像并返回它找到的对象。该程序运行良好一段时间,突然关闭。
mask_rcnn: Traceback (most recent call last):
mask_rcnn: File "/home/riwo-rack-pc/GIT/automatic-planthandling/Ros/catkin_ws/src/image_processing/mask_rcnn_ros/nodes/mask_rcnn_node", line 227, in <module>
mask_rcnn: main()
mask_rcnn: File "/home/riwo-rack-pc/GIT/automatic-planthandling/Ros/catkin_ws/src/image_processing/mask_rcnn_ros/nodes/mask_rcnn_node", line 224, in main
mask_rcnn: node.run()
mask_rcnn: File "/home/riwo-rack-pc/GIT/automatic-planthandling/Ros/catkin_ws/src/image_processing/mask_rcnn_ros/nodes/mask_rcnn_node", line 114, in run
mask_rcnn: self._mask_pub.publish(self.center_mask(result))
mask_rcnn: File "/home/riwo-rack-pc/GIT/automatic-planthandling/Ros/catkin_ws/src/image_processing/mask_rcnn_ros/nodes/mask_rcnn_node", line 133, in center_mask
mask_rcnn: im, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
mask_rcnn: cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/imgproc/src/contours.cpp:199: error: (-210) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function cvStartFindContours_Impl
我已经研究了将 mono8 转换为所需的 CV_8UC1 的编码和可能性的差异。结论似乎是它们或多或少相同。
rate = rospy.Rate(self._publish_rate)
while not rospy.is_shutdown():
if self._msg_lock.acquire(False):
msg = self._last_msg
self._last_msg = None
self._msg_lock.release()
else:
rate.sleep()
continue
if msg is not None:
np_image = self._cv_bridge.imgmsg_to_cv2(msg, 'mono8')
np_image = cv2.merge((np_image, np_image, np_image))
# Run detection
results = self._model.detect([np_image], verbose=0)
result = results[0]
result_msg = self._build_result_msg(msg, result) #TODO
self._result_pub.publish(result_msg)
self._mask_pub.publish(self.center_mask(result))
# Visualize results
if self._visualization:
vis_image = self._visualize(result, np_image)
cv_result = np.zeros(shape=vis_image.shape, dtype=np.uint8)
cv2.convertScaleAbs(vis_image, cv_result)
image_msg = self._cv_bridge.cv2_to_imgmsg(cv_result, 'bgr8')
vis_pub.publish(image_msg)
rate.sleep()
def center_mask(self, result):
centers = xyz()
pt = Point()
ret, thresh = cv2.threshold(result['masks'], 0, 255, 0)
#
im, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if(contours != []):
for c in contours:
print("in the forloop met contouren")
M = cv2.moments(c)
if(M["m10"] and M["m01"] and M["m00"]) != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
print("cx: ", cX)
print("cy:", cY)
pt.x = cX
pt.y = cY
pt.z = 0
centers.points.append(copy.deepcopy(pt))
else:
print('no pot found else')
pt.x = 0
pt.y = 0
pt.z = 0
centers.points.append(copy.deepcopy(pt))
return centers
我希望代码继续运行,直到我停止它,并且我希望我正在评估的消息的编码不会在没有明显原因的情况下改变。
是否有可以转换它的函数,或者是否可以在将编码传递给 findContours 函数之前检查编码?
解决方案
这不是由图像编码引起的问题。当模型在图像中找到多个掩码时,阈值会更改为 findContours 函数无法处理的内容。
推荐阅读
- javascript - 如何获取从 ec2 上传的文件的 url
- django - Django/DRF 过滤
- android - 颤振:添加 firebase_admob 后构建失败
- javascript - 是否可以通过单击任何按钮来更改 DOM 加载的 javascript 代码而无需加载页面?
- c# - Entity Framework 代码先将 FK 加倍为 PK
- mql4 - 无法使用功能正常的 Pine-Script 示例在 MQL4 中计算正确的交易量 RSI
- php - WordPress 活动日程无法正常工作
- python - 我收到这种类型的错误:TypeError:'int'和'str'的实例之间不支持'<'
- python - tf.sets.intersection 的错误:ValueError:形状必须至少为 2 级,但对于“DenseToDenseSetOperation_12”为 1 级
- android - 使用 Kotlin Coroutines 替换 LocalBroadcastManager 进行 Firebase 消息传递