[英]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.