3 Issues with OpenCV course

Hi all,

I am having 3 issues with the OpenCV course (Unit 2).

  1. Every time I use a method from the cv2 library, I get an error:
    “Module ‘cv2’ has no ‘method_name’ member.” How can I get rid of this error?

  2. When I implement the code provided by the course in exercise 2.6 (and other exercises as well), I get an error:
    “(Original:2622): Gdk-ERROR *: 04:10:38.079: The program ‘Original’ received an X Window System error.
    This probably reflects a bug in the program.
    The error was ‘BadAccess (attempt to access private resource denied)’.
    (Details: serial 387 error_code 10 request_code 130 (MIT-SHM) minor_code 1)
    (Note to programmers: normally, X errors are reported asynchronously;
    that is, you will receive the error a while after causing it.
    To debug your program, run it with the GDK_SYNCHRONIZE environment
    variable to change this behavior. You can then get a meaningful
    backtrace from your debugger if you break on the gdk_x_error() function.)
    [sobela_node-1] process has died [pid 2622, exit code -5, cmd /home/user/catkin_ws/src/unit2/src/sobela.py __name:=sobela_node __log:=/home/user/.ros/log/3c1164be-afdd-11ec-a2d8-0242ac170007/sobela_node-1.log].
    log file: /home/user/.ros/log/3c1164be-afdd-11ec-a2d8-0242ac170007/sobela_node-1
    .log”

Therefore, I cannot execute any codes in this course.

  1. I got this error when I executed this code for exercise 2.6:

Error:
[ERROR] [1648613319.398475, 756.050000]: bad callback: <bound method LoadImage.callback of <main.LoadImageobject at 0x7f15755d6d60>>
Traceback (most recent call last):
File “/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py”, line 750, in _invoke_callback
cb(msg)
File “/home/user/catkin_ws/src/unit2/src/sobela.py”, line 14, in callback
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.2.0) …/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() infunction ‘cvtColor’

Code:

#!/usr/bin/env python

import cv2
import rospy
from sensor_msgs.msg import Image

class LoadImage(object):
    def __init__(self):
        self.image_sub = rospy.Subscriber("/camera/rgb/image_raw", Image, self.callback)
    def callback(self, data):
        example_path = 'home/user/opencv_for_robotics_images/Unit_2/Course_images/test_img_b.jpg'
        img = cv2.imread(example_path)
        #Convert the image to gray scale so the gradient is better visible
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = cv2.resize(img,(450,350))
        #Apply the horizontal sobel operator with a kernel size of 3
        sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3)
        #Apply the vertical sobel operator with a kernel size of 3
        sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
        cv2.imshow('Original',img)
        cv2.imshow('X',sobelx)
        cv2.imshow('Y',sobely)
        cv2.waitKey(1)

def main():
    load_image_object = LoadImage()
    rospy.init_node('sobela_node', anonymous=True)
    try:
        rospy.spin()
    except KeyboardInterrupt:
        print("Shutting down")
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

What is this error telling me to fix?

I have already tried catkin_make and source devel/setup.bash. It would be appreciated if anybody can provide solutions to the issues.
Thank you in advance

For the first error,

Try

from cv2 import cv2

Hi, thank you for the response.

I tried your suggestion, but I got an error:
“No name ‘cv2’ in module ‘cv2’”

Hello @yuseialexs ,

  1. Can you share an example code that gives you this error? I’ve been testing in the course and I’m not been able to reproduce this issue. Import of cv2 is working of for me.

  2. This is a known issue. If you get this 'W Window' error, just close the program and launch it again, it’s going to work.

  3. Here you have an error in the test image path. It should be:

example_path = '/home/user/catkin_ws/src/opencv_for_robotics_images/Unit_2/Course_images/test_img_b.jpg'

If you update this line the could is going to work fine.