Reputation: 315
I am pretty new to OpenCV and Python. I am trying to use OpenCV to do some image processing of the ARDrone. However, I can only rosrun my python file in the source folder as
root@ubuntu:~/ros_workspace/sandbox/ardrone/src# rosrun ardrone ardrone_colortrack.py
In this way, there was no error and the program could be executed.
However, if I do a rosrun directly in the package folder as
root@ubuntu:~/ros_workspace/sandbox/ardrone_dii# rosrun ardrone_dii ardrone_colortrack.py
There were errors:
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
I didn't understand why this happened. The OpenCV libraries and the CV_bridge have been included in the CMakeLists.txt and the Manifest.xml.
Could anyone help me on this? Thanks.
Here is the source code:
#!/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()
Upvotes: 2
Views: 1363
Reputation: 93468
The error message seems pretty clear. The instruction:
hsv_img = cv2.cvtColor(self.img, cv2.COLOR_BGR2HSV)
failed because one of these things happened:
scn
(the number of channels of the source), i.e. self.img.channels()
, is neither 3 nor 4.depth
(of the source), i.e. self.img.depth()
, is neither CV_8U nor CV_32F.So before calling cvtColor()
print these information to the console and try to understand why their values doesn't match with what is expected by cvtColor()
.
Upvotes: 0