本文介绍了使激光探测器在我的代码中有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv库制作一个检测激光,圆和数字的程序.这是我第一次使用ros indigo,我真的不知道我在做什么,但是在运行程序时,我遇到了迅速重复出现的错误.这是我得到的错误:

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:

[错误] [WallTime:1565273888.861720]错误的回调:追溯(最近一次通话): _invoke_callback中的第720行的文件"/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py" cb(味精) 在回调中的文件"lazer3.py",第89行 cv_image2 = self.convert_image(cv_image)NameError:未定义全局名称"self"

[ERROR] [WallTime: 1565273888.861720] bad callback: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

该错误出现在显示cv_image2 = self.convert_image(cv_image)的行中,这是因为如果我删除了self,则使用self会得到另一个错误:

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:

[错误] [WallTime:1565274017.459066]错误的回调:追溯(最近一次通话): _invoke_callback中的第720行的文件"/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py" cb(味精) 在回调中的文件"lazer3.py",第89行 cv_image2 = convert_image(cv_image) 在convert_image中的文件"lazer3.py",第29行 标签= measure.label(阈值,邻居= 8,背景= 0)AttributeError:模块"对象没有属性标签"

[ERROR] [WallTime: 1565274017.459066] bad callback: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'

这是我的代码:

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.boundingRect(c)
        ((cX, cY), radius) = cv2.minEnclosingCircle(c)
        #x and y center are cX and cY
        cv2.circle(image, (int(cX), int(cY)), int(radius),
            (0, 0, 255), 3)
        cv2.putText(image, "#{}".format(i + 1), (x, y - 15),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

        # show the output image
    cv2.imshow("Image", image)
    cv2.waitKey(1)
        #camera.release()
    return image

# ROS Interface
if __name__ == "__main__":
    import rospy
    from cv_bridge import CvBridge, CvBridgeError
    from sensor_msgs.msg import Image
    bridge = CvBridge()
    def show_img(cv_image):
        (rows,cols,channels) = cv_image.shape
        if cols > 60 and rows > 60 :
            cv2.circle(cv_image, (50,50), 10, 255)
        cv2.imshow("Image window", cv_image)
        cv2.waitKey(3)
    image_pub = rospy.Publisher("image_topic_2", Image)
    def callback(data):
        try:
            cv_image = bridge.imgmsg_to_cv2(data, "bgr8")
            show_img(cv_image)
            cv_image2 = self.convert_image(cv_image)
            image_pub.publish(bridge.cv2_to_imgmsg(cv_image2, "bgr8"))
        except CvBridgeError as e:
            print(e)
    image_sub = rospy.Subscriber("CM_040GE/image_raw", Image, callback)
    rospy.init_node('image_converter', anonymous=True)
    rospy.spin()
    print("image_converter: Shutting down")
    cv2.destroyAllWindows()

我还想澄清一点,就是因为代码相似,所以这不是我最后一个问题的重复,因为我遇到了另一个错误,而且我在问一个完全独立的问题

also I want to make it clear that just because the code is the similar this is not a repeat of my last question because I was getting a different error and I was asking about a completely separate issue

推荐答案

包含self是正确的,因为convert_image()是常规函数.真正的错误是measure没有label属性/功能.除了(取决于版本), skimage.measure.label 存在,正如您所说.

It is correct to not have the self in there, as convert_image() is a regular function.The real error is that measure has no label attribute/function. Except, (depending on the version), skimage.measure.label exists as you say.

基于针对此问题(模块中没有属性)的其他SO解决方案,请尝试检查您所使用的skimage版本是否具有measure.label,尝试删除任何.pyc文件,并确保没有命名冲突或与您要导入的模块名称相同的模块.

Based on other SO solutions to this (no attribute in module) problem, try checking that the version of skimage you're using has measure.label, try deleting any .pyc files, and make sure you have no naming conflicts or modules with the same name as what you're trying to import.

skimage v0.9.x在下具有label功能morphology 模块: skimage.morphology.label .以后将其移至measure.

skimage v0.9.x has the label function under the morphology module: skimage.morphology.label. It was moved to measure at a later date.

这篇关于使激光探测器在我的代码中有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 23:08