makeing a laser detector have an error need some help

asked 2019-08-08 10:16:51 -0600

brian77754 gravatar image

I am making a program that detects lasers and circles and numbers then with the opencv library. This is my first time using ros indigo and I don't really know what I am doing but I am getting an error that is repeating at a rapid rate when I run my program. this is the error I am getting:

[ERROR] [WallTime: 1565273888.861720] bad callback: <function callback="" at="" 0x7fcc6a820de8=""> Traceback (most recent call last): File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback cb(msg) File "lazer3.py", line 89, in callback cv_image2 = self.convert_image(cv_image) NameError: global name 'self' is not defined

the error is in the line that says cv_image2 = self.convert_image(cv_image) and it is because I use the self if I delete self I get a different error:

[ERROR] [WallTime: 1565274017.459066] bad callback: <function callback="" at="" 0x7f5e97a45de8=""> Traceback (most recent call last): File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback cb(msg) File "lazer3.py", line 89, in callback cv_image2 = convert_image(cv_image) File "lazer3.py", line 29, in convert_image labels = measure.label(thresh, neighbors=8, background=0) AttributeError: 'module' object has no attribute 'label'

here is my code:

from __future__ import print_function
import cv2
import numpy as np
import imutils
from imutils import contours
from skimage import measure

'''
def getPoint(cameraTip,dotXY,normalPoint):
    slope= (cameraTip[2]-dotXY[2])/(cameraTip[1]-dotXY[1])
    b=cameraTip[2]-(slope*cameraTip[1])
    z=slope*normalPoint[1]+b
    return [normalPoint[0],normalPoint[1],z]
'''
# Image Processing functions
def convert_image(image): # Image of kind bgr8
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (11, 11), 0)
        #threshold the image to reveal light regions in the
        # blurred image
    thresh = cv2.threshold(blurred, 30, 255, cv2.THRESH_BINARY)[1]
        # perform a series of erosions and dilations to remove
        # any small blobs of noise from the thresholded image
    thresh = cv2.erode(thresh, None, iterations=2)
    thresh = cv2.dilate(thresh, None, iterations=4)
        # perform a connected component analysis on the thresholded
        # image, then initialize a mask to store only the "large"
        # components
    labels = measure.label(thresh, neighbors=8, background=0)
    mask = np.zeros(thresh.shape, dtype="uint8")

        # loop over the unique components
    for label in np.unique(labels):
            # if this is the background label, ignore it
        if label == 0:
            continue

            # otherwise, construct the label mask and count the
                # number of pixels
        labelMask = np.zeros(thresh.shape, dtype="uint8")
        labelMask[labels == label] = 255
        numPixels = cv2.countNonZero(labelMask)

                # if the number of pixels in the component is sufficiently
                # large, then add it to our mask of "large blobs"
        if numPixels > 300:
            mask = cv2.add(mask, labelMask)
    # find the contours in the mask, then sort them from left to
    # right
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = contours.sort_contours(cnts)[0]

        # loop over the contours
    for (i, c) in enumerate(cnts):
            # draw the bright spot on the image
        (x, y, w, h) = cv2 ...
(more)
edit retag flag offensive close merge delete