简体   繁体   English

当我尝试将图像拆分为多个部分以分别处理并计算每个图像中的白色像素数时出现错误?

[英]When I try to split an image into sections to process separately and count the number of white pixels in each I get an error?

I'm not sure how I would go about doing this, here is what I have so far.我不确定我将如何去做,这是我到目前为止所拥有的。

def camera_callback(self, rgb_msg):
   

    # Check frame counter
    if( self.frame_count % self.frame_skip != 0 ):
        self.frame_count += 1
        return
    self.frame_count = 1
    
    
    # Get the camera image and make a copy
    img = CvBridge().imgmsg_to_cv2(rgb_msg, "bgr8" )
    img_hsv=cv.cvtColor(img, cv.COLOR_BGR2HSV)
    img_gray=cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    

    hsv_low=np.array([self.H_low,self.S_low,self.V_low], np.uint8)
    hsv_high=np.array([self.H_high,self.S_high,self.V_high], np.uint8)

    mask=cv.inRange(img_hsv, hsv_low, hsv_high)
    res=cv.bitwise_and(img, img, mask=mask)

    self.n1=res[0:480,   0:213]
    self.n2=res[0:480, 214:427]
    self.n3=res[0:480, 428:640]

    
    nnz1=cv.countNonZero(self.n1)
    nnz2=cv.countNonzero(self.n2)
    nnz3=cv.countNonzero(self.n3)

    if(nnz1>nnz2 and nnz1>nnz3):
            self.yaw_rate=.5
    elif(nnz2>nnz1 and nnz2>nnz3):
            self.yaw_rate=0
    elif(nnz3>nnz2 and nnz3>nnz1):
            self.yaw_rate=-.5 
    
    self.msg.angular.x=self.yaw_rate
    self.pub_twist.publish(self.msg)

    #print(mask.shape[0]," ",mask.shape[1])

    #cv.imshow("mask",mask)
    #cv.imshow("res", res)
    self.display_image("mask", mask,True)
    self.display_image("result",res, True)

The error I get when running it is运行它时我得到的错误是

[ERROR] [1656360138.949456]: bad callback: <bound method LineFollowNode.camera_callback of < main .LineFollowNode object at 0x7f10c61a7b20>> 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/student/catkin_ws/src/hw06_freeman/scripts/line_follow.py", line 130, in camera_callback nnz1=cv.countNonZero(self.n1) cv2.error: OpenCV(4.2.0) ../modules/core/src/count_non_zero.dispatch.cpp:124: error: (-215:Assertion failed) cn == 1 in function 'countNonZero' [错误] [1656360138.949456]:错误回调:< main .LineFollowNode 对象在 0x7f10c61a7b20>> 的绑定方法 LineFollowNode.camera_callback>> Traceback(最近一次调用):文件“/opt/ros/noetic/lib/python3/dist-packages /rospy/topics.py”,第 750 行,在 _invoke_callback cb(msg) 文件中“/home/student/catkin_ws/src/hw06_freeman/scripts/line_follow.py”,第 130 行,在 camera_callback nnz1=cv.countNonZero(self. n1) cv2.error: OpenCV(4.2.0) ../modules/core/src/count_non_zero.dispatch.cpp:124: 错误: (-215:Assertion failed) cn == 1 in function 'countNonZero'

You're trying to count non-zero pixels on a colored image.您正在尝试计算彩色图像上的非零像素。 You cannot do this as it's only meant to function with greyscale images.您不能这样做,因为它仅适用于灰度图像。 You should be using img_gray instead of img with res您应该使用img_gray而不是img with res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算图像中白色像素的总数 - Count total number of white pixels in an image 当我尝试刮取图像的详细信息页面时,出现错误 - When i try to scrape details page for image I get an error 获取图像中每个对象的像素数 - Get number of pixels of each object in image 如何使用 python 获取图像中每种唯一颜色下的像素总数? - How to get the count of total number of pixels under each unique color in an image using python? 当我尝试从表单添加时出现白色错误屏幕 - White Error Screen Appears when I try to add from form 当我尝试从表单更新时出现白色错误屏幕 - White Error Screen Appears when I try to update from form 如何计算轮廓中的像素数? - How can I count number of pixels in a contour? Python-获取图像的白色像素 - Python - get white pixels of image 计算OpenCV中沿线的白色像素数 - Count number of white pixels along a line in OpenCV 每次我尝试按字符串值过滤数据框时,我的数据框都是空的。 但是当我尝试从一列中获取计数值时,我得到了数字 - Every time I try to filter dataframe by string values, my dataframe is empty. But when I try to get count value from one column, I get the number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM