diff --git a/catkin_ws/src/ball_tracking/src/trackernew.py b/catkin_ws/src/ball_tracking/src/trackernew.py deleted file mode 100755 index b8fb5b4..0000000 --- a/catkin_ws/src/ball_tracking/src/trackernew.py +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env python3 - -import rospy -from geometry_msgs.msg import Point -import cv2 -import numpy as np - -def nothing(x): - pass - -# Initialize the ROS node -rospy.init_node('ball_tracking_node', anonymous=True) - -# Create a ROS publisher for the ball's coordinates -coord_pub = rospy.Publisher('/ball_coordinates', Point, queue_size=10) - -# Create a window for the trackbars -cv2.namedWindow('settings') - -# Create trackbars for adjusting the HSV range -cv2.createTrackbar('Lower-H', 'settings', 0, 179, nothing) -cv2.createTrackbar('Lower-S', 'settings', 100, 255, nothing) -cv2.createTrackbar('Lower-V', 'settings', 100, 255, nothing) -cv2.createTrackbar('Upper-H', 'settings', 22, 179, nothing) -cv2.createTrackbar('Upper-S', 'settings', 255, 255, nothing) -cv2.createTrackbar('Upper-V', 'settings', 255, 255, nothing) - -# Attempt to open the video capture -cap = cv2.VideoCapture(0) - -# Check if the camera opened successfully -if not cap.isOpened(): - print("Error: Could not open camera.") - exit() - -while not rospy.is_shutdown(): - # Capture frame-by-frame - ret, frame = cap.read() - if not ret: - print("Can't receive frame (stream end?). Exiting ...") - break - - # Convert the captured frame to HSV - hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) - - # Get the current positions of the trackbars - lh = cv2.getTrackbarPos('Lower-H', 'settings') - ls = cv2.getTrackbarPos('Lower-S', 'settings') - lv = cv2.getTrackbarPos('Lower-V', 'settings') - uh = cv2.getTrackbarPos('Upper-H', 'settings') - us = cv2.getTrackbarPos('Upper-S', 'settings') - uv = cv2.getTrackbarPos('Upper-V', 'settings') - - # Define the HSV range for the orange color - lower_orange = np.array([lh, ls, lv]) - upper_orange = np.array([uh, us, uv]) - - # Threshold the HSV image to only get the orange colors - mask = cv2.inRange(hsv, lower_orange, upper_orange) - res = cv2.bitwise_and(frame, frame, mask=mask) - - # Find contours in the mask - contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - center = None - - if contours: - # Find the largest contour in the mask - c = max(contours, key=cv2.contourArea) - ((x, y), radius) = cv2.minEnclosingCircle(c) - - if radius > 10: # Minimum radius threshold - # Draw the circle and centroid on the frame - cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) - center = (int(x), int(y)) - cv2.circle(frame, center, 5, (0, 0, 255), -1) - - # Publish the ball's coordinates - point_msg = Point() - point_msg.x = x - point_msg.y = y - point_msg.z = 0 # Z-coordinate is not applicable here - coord_pub.publish(point_msg) - - # Display the original and the result - cv2.imshow('frame', frame) - cv2.imshow('mask', mask) - cv2.imshow('res', res) - - if cv2.waitKey(1) & 0xFF == 27: # ESC key to break - break - -# When everything done, release the capture and destroy all windows -cap.release() -cv2.destroyAllWindows() -#Minabebis :3