Still buggy cause point.x != width value? Will find out until Wednesday.
This commit is contained in:
2023-12-04 13:54:39 +01:00
parent a409658f8b
commit b7cd47b2c0
+40 -11
View File
@@ -6,6 +6,7 @@ import sys
import rospy
import cv2
import argparse
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
@@ -65,9 +66,33 @@ class image_converter:
self.t = Twist()
### Markers haven been detected, now do something
def markDetected(self, ids, cv_image):
(h, w) = cv_image.shape[:2]
cv2.circle(cv_image, (w//2, h//2), 7, (255, 255, 255), -1)
def markDetected(self, ids, img, corners):
(h, w) = img.shape[:2]
cv2.circle(img, (w//2, h//2), 100, (255, 255, 255), -1)
thresh_left = w//2 - 100
thresh_right = w//2 + 100
cv2.line(img, (thresh_left, h//2), (thresh_right, h//2), (0,0,0), 2)
for markerCorner in corners:
corners = markerCorner.reshape((4,2))
(top_left, top_right, bottom_right, bottom_left) = corners
top_left = (int(top_left[0]), int(top_left[1]))
top_right = (int(top_right[0]), int(top_right[1]))
bottom_right = (int(bottom_right[0]), int(bottom_right[1]))
bottom_left = (int(bottom_left[0]), int(bottom_left[1]))
cv2.circle(img, top_left, 10, (0,255,0), -1)
if (top_left[1] or top_right[1] or bottom_left[1] or bottom_right[1]) >= thresh_left and (top_left[1] or top_right[1] or bottom_left[1] or bottom_right[1]) <= thresh_right:
print("###############################")
print(top_left[1])
print(top_right[1])
print(bottom_left[1])
print(bottom_right[1])
print(thresh_left)
print(thresh_right)
print("##############################################")
self.t.linear.x = 0.1
self.pub.publish(self.t)
self.rate.sleep()
#print(corners[0][0])
ids = ids.flatten()
#if [1] in ids: # ID:1
# self.t.angular.z = 0.0
@@ -80,8 +105,9 @@ class image_converter:
# self.t.linear.x = 0.0
#else:
# return
self.pub.publish(self.t)
self.rate.sleep()
#self.pub.publish(self.t)
#self.rate.sleep()
return img
def callback(self, data):
try:
@@ -102,12 +128,15 @@ class image_converter:
#####
# verify *at least* one ArUCo marker was detected
if len(corners) > 0:
self.markDetected(ids, cv_image)
else:
self.t.angular.z = 0.8
self.t.linear.x = 0.2
self.pub.publish(self.t)
self.rate.sleep()
cv_image = self.markDetected(ids, cv_image, corners)
#else:
#self.t.angular.z = 0.8
#self.t.linear.x = 0.2
#self.pub.publish(self.t)
#self.rate.sleep()
#self.t.angular.z = -0.8
#self.pub.publish(self.t)
#self.rate.sleep()
#####