【问题标题】:making a laser detector have an error in my code使激光探测器在我的代码中有错误
【发布时间】:2019-12-16 07:09:44
【问题描述】:

我正在制作一个程序,然后使用 opencv 库检测激光、圆圈和数字。这是我第一次使用 ros indigo,我真的不知道自己在做什么,但是当我运行我的程序时,我得到了一个错误,该错误正在快速重复。这是我得到的错误:

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

错误出现在cv_image2 = self.convert_image(cv_image) 的行中,这是因为如果我删除自我,我会使用自我,我会得到一个不同的错误:

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

这是我的代码:

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()

我还想明确指出,仅仅因为代码相似,这不是我上一个问题的重复,因为我遇到了一个不同的错误,我问的是一个完全不同的问题

【问题讨论】:

    标签: python opencv camera ros


    【解决方案1】:

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

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

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

    【讨论】:

    • 我认为您可能是对的,它是一个版本错误 scikit-image==0.9.3 但是我正在尝试更新它并且遇到了一些麻烦当我尝试命令 python 时遇到此错误setup.py egg_info 在 /tmp/pip_build_root/scikit-image 中失败,错误代码 1 在 /home/robot/.pip/pip.log 中存储失败的调试日志
    • 我尝试更新它的方式是 sudo pip install --upgrade scikit-image
    • 如果您使用正确的模块应该没问题,请参阅编辑。如果你调整你的 requirements.txt,Pip 也可以处理它。
    • 对不起,我不知道edits是什么,也不知道requirements.txt是什么
    • 你知道如何升级 scikit 镜像
    【解决方案2】:

    如果函数convert_image是一个类的方法,那么你通常会这样做

    # instantiate the class
    my_obj = my_class()
    
    # call the class' method
    my_obj.convert_image(paramaters)
    

    所以cv_image2 = self.convert_image(cv_image) 应该是cv_image2 = my_obj.convert_image(cv_image)

    如果你没有实例化一个类,而只是调用一个函数,那么你可以摆脱self - cv_image2 = convert_image(cv_image)

    【讨论】:

    • 正如我之前所说,我确实摆脱了自我,它给了我一个不同的错误@ron g
    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多