Segmentation Fault (core dump) in Python and ROS

asked 2014-06-12 08:33:56 -0500

I am working on ROS and Opencv. I am able to run the code but after some times the code stops and show the error Segmentation Fault (Core Dumped). The following is the code in which a quadrotor is finding the color box which I will modify by giving the values of RGB through trackbars. I have added some of the debugging statements to find out the line of error. and I think the line of error is hsv=cv2.cvtColor(cv_image,cv2.COLOR_BGR2HSV) The following is the whole code

#!/usr/bin/env python
import roslib
import sys
import time
import math
import rospy
import cv2
import cv2.cv as cv
import numpy as np
from geometry_msgs.msg import PoseStamped
from geometry_msgs.msg import Vector3
from geometry_msgs.msg import Twist
from geometry_msgs.msg import Wrench
from geometry_msgs.msg import Pose
from geometry_msgs.msg import Point
from sensor_msgs.msg import NavSatFix
from sensor_msgs.msg import CameraInfo
from sensor_msgs.msg import Image
from sensor_msgs.msg import RegionOfInterest
from cv_bridge import CvBridge, CvBridgeError
from rospy.numpy_msg import numpy_msg

H_MIN = 0
H_MAX = 255
V_MIN = 0
V_MAX = 255
S_MIN = 0
S_MAX = 255

class test_vision_node_fly:
    def __init__(self):
        print('a')
        self.image_pub = rospy.Publisher("quadrotor/videocamera1/camera_info",Image)
        print('b')
        self.ROI = rospy.Publisher("roi", RegionOfInterest)
        print('c')  
        rospy.sleep(1)
        print('d')
        self.cv_window_name = "Image window"
        print('e')
            cv.NamedWindow(self.cv_window_name, 0)
        print('f')
            cv.NamedWindow("trackbars" , 0) 
        print('g')      
            self.bridge = CvBridge()
        print('h')
            self.image_sub = rospy.Subscriber("quadrotor/videocamera1/image",Image,self.callback)
        print('i')
        rospy.loginfo("Waiting for image topics...")
        print('j')
    def on_trackbar(self,x):
            global H_MAX
            global H_MIN
            global S_MAX
            global S_MIN
            global V_MAX
            global V_MIN
            print('k')
        H_MIN = cv2.getTrackbarPos('H_MIN', 'trackbars')
            H_MAX = cv2.getTrackbarPos('H_MAX', 'trackbars')  
            S_MAX = cv2.getTrackbarPos('S_MAX', 'trackbars')
            S_MIN = cv2.getTrackbarPos('S_MIN', 'trackbars')
            V_MIN = cv2.getTrackbarPos('V_MIN', 'trackbars')
            V_MAX = cv2.getTrackbarPos('V_MAX', 'trackbars')
        print('l')     
            pass   
    def callback(self,data):

        """ Convert the raw image to OpenCV format using the convert_image() helper function """
            cv_image = self.convert_image(data)
        print('m')
        # Convert the image to a Numpy array since most cv2 functions
            # require Numpy arrays.
            cv_image = np.array(cv_image, dtype=np.uint8)
        print('n')
            """ Apply the CamShift algorithm using the do_camshift() helper function """
            cv_image = self.do_camshift(cv_image)
        print('o')
            """ Refresh the displayed image """
            cv.ShowImage(self.cv_window_name, cv_image)
        print('p')
    def convert_image(self, ros_image):  
        try:
                cv_image = self.bridge.imgmsg_to_cv(ros_image, "bgr8")
            print('q')
            return cv_image
            except CvBridgeError, e:
                print e
    def do_camshift(self, cv_image):            
            ''' converting bgr to hsv ''' 
        print('r')         
            hsv=cv2.cvtColor(cv_image,cv2.COLOR_BGR2HSV) 
        print('s')    
        '''creating trackbars'''
        cv2.createTrackbar('H_MIN', 'trackbars', H_MIN, H_MAX, self.on_trackbar) 
        cv2.createTrackbar('H_MAX', 'trackbars', H_MAX, H_MAX, self.on_trackbar)
            cv2.createTrackbar('S_MIN', 'trackbars', S_MIN, S_MAX, self.on_trackbar) 
        cv2.createTrackbar('S_MAX', 'trackbars', S_MAX, S_MAX, self.on_trackbar)    
        cv2.createTrackbar('V_MIN', 'trackbars', V_MIN, V_MAX, self.on_trackbar)    
            cv2.createTrackbar('V_MAX', 'trackbars', V_MAX, V_MAX, self.on_trackbar)
        print('t')                                      
            ''' threshdolding '''
        COLOR_MIN = np.array([H_MIN, S_MIN, V_MIN],dtype=np.uint8)
        print('u')
            COLOR_MAX = np.array([H_MAX, S_MAX, V_MAX],dtype=np.uint8)
        print('v')
            mask=cv2.inRange(hsv ...
(more)
edit retag flag offensive close merge delete