只能在ROS包源文件夹中运行OpenCV,但不能直接在ROS包中运行

时间:2013-12-23 17:34:41

标签: python opencv ros

我是OpenCV和Python的新手。我正在尝试使用OpenCV对ARDrone进行一些图像处理。但是,我只能将源文件夹中的python文件作为

运行
root@ubuntu:~/ros_workspace/sandbox/ardrone/src# rosrun ardrone ardrone_colortrack.py

通过这种方式,没有错误,程序可以执行。

但是,如果我直接在包文件夹中执行rosrun

root@ubuntu:~/ros_workspace/sandbox/ardrone_dii# rosrun ardrone_dii ardrone_colortrack.py

有错误:

OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cvtColor, file /tmp/buildd/ros-hydro-opencv2-2.4.6-3precise-20131020-2223/modules/imgproc/src/color.cpp, line 3541
Traceback (most recent call last):
  File "/root/ros_workspace/sandbox/ardrone/src/ardrone_colortrack.py", line 86, in <module>
    main()
  File "/root/ros_workspace/sandbox/ardrone/src/ardrone_colortrack.py", line 75, in main
    ct.run()
  File "/root/ros_workspace/sandbox/ardrone/src/ardrone_colortrack.py", line 31, in run
    hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.error: /tmp/buildd/ros-hydro-opencv2-2.4.6-3precise-20131020-2223/modules/imgproc/src/color.cpp:3541: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cvtColor

我不明白为什么会这样。 OpenCV库和CV_bridge已包含在CMakeLists.txt和Manifest.xml中。

有人可以帮我这个吗?谢谢。

以下是源代码:

#!/usr/bin/env python
import roslib
roslib.load_manifest('ardrone_dii')

import sys
import rospy
import cv2
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

class ArdroneColorTrack(object):

    def __init__(self):
        self.video_src = None
        self.img = None
        self.bridge = CvBridge()
        self.lowerb = np.array([120, 80, 80], np.uint8)
        self.upperb = np.array([140, 255, 255], np.uint8)
        self.subVideo = rospy.Subscriber('/ardrone/image_raw', Image, self.image_converter)

    def image_converter(self, video_src):
        self.video_src = video_src
        try:
            self.img = self.bridge.imgmsg_to_cv(self.video_src, "bgr8")
        except CvBridgeError e:
            print e

    def display(self):  
        print "Getting image from the camera ..."

        if not self.img:
            print "Having trouble reading the raw image!!!"
            return -1
        print '------------------------------------------------------------'

        hsv_img = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV)
        grayimg = cv2.GaussianBlur(hsv_img, (11, 11), 0, 0) 
        gray_img = cv2.inRange(grayimg, self.lowerb, self.upperb)

        moments = cv2.moments(gray_img, 0)
        area = moments['m00']

        # there can be noise in the video so ignore objects with small areas 
        if (area > 1000):
            # determine the x and y coordinates of the center of the object 
            # we are tracking by dividing the 1, 0 and 0, 1 moments by the area
            x = moments['m10']
            y = moments['m01']
            cv2.circle(gray_img, (int(x), int(y)), 2, (255, 255, 255), 20)

        #display the image  
        cv2.imshow('Raw Image Window', self.img)
        cv2.imshow('Color-tracked Window', gray_img)            
        cv2.waitKey(1)


def main(): 
    rospy.init_node( 'ardrone_colortrack' )
    ct = ArdroneColorTrack()
    loop = rospy.Rate(1)

    while not rospy.is_shutdown():
        try:
            ct.display()
            loop.sleep()
        except KeyboardInterrupt:
            print "Keyboard interrupted!"

    rospy.signal_shutdown('Closing ROS node!')
    sys.exit()

if __name__=="__main__":
    main()

1 个答案:

答案 0 :(得分:0)

错误消息似乎很清楚。说明:

hsv_img = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV)

失败,因为其中一件事发生了:

  • scn(来源的渠道数量),即self.img.channels(),既不是 3 也不是 4
  • depth(来源),即self.img.depth(),既不是 CV_8U 也不是 CV_32F

因此,在致电cvtColor()之前,请将这些信息打印到控制台,并尝试了解其值与cvtColor()所期望的值不符的原因。