diff --git a/scripts/contour_centroid.py b/scripts/contour_centroid.py index a897cd557d89b7e5107df0d28b247d09cdb84ec5..4dd7f6b5652ecb127a7621ce889d5f2191cfeaed 100644 --- a/scripts/contour_centroid.py +++ b/scripts/contour_centroid.py @@ -13,13 +13,20 @@ class CentroidContour(): self.image = cv2.imread('/home/maitreyee/IMG_20171011_162319075.jpg') def contourXO(self,contours): -#import the image from obj_recogkinect.py + #import the image from obj_recogkinect.py gray = cv2.cvtColor(self.image , cv2.COLOR_BGR2GRAY) + #Find out threshold to use for finding contours ret, thresh = cv2.threshold(gray , 127,255,cv2.THRESH_BINARY) + #find contours and hierarchy of contours, here we use cv2 parameter to find out all the possible contours im2, contours, hierarchy = cv2.findContours(thresh , cv2.RETR_TREE , cv2.CHAIN_APPROX_TC89_L1) + #draw the contours, it will be in array form cv2.drawContours(self.image , contours , -1 , (0, 255, 0) , 5) - plt.imshow(self.image, cmap='gray') - plt.show() + #show the contour with the image + cv2.imshow('Contours',self.image) + k = cv2.waitKey(0) & 0xFF + if k==27:#wait for esc key to destroy all windows + cv2.destroyAllWindows() + #here we find out solidity of all the contours and loop through a list of contours to classify X and O into different categories. for (i,c) in enumerate(contours): area = cv2.contourArea(c) (x, y, w, h) = cv2.boundingRect(c) @@ -35,7 +42,7 @@ class CentroidContour(): cv2.drawContours(self.image ,[c] ,-1 , (0,255,0) , 5) cv2.putText(self.image , char , (x, y - 10) , cv2.FONT_HERSHEY_COMPLEX, 1.25, (0, 255, 0), 5) return contours[0] - + #For the available contours we find their entre points and print those centre points which will be in an array format def detectCentroids(self): centres = [] cnts = self.contourXO() @@ -47,4 +54,5 @@ class CentroidContour(): cv2.drawContours(self.image , [ctr] , -1 , (0, 255, 0) , 2) cv2.circle(self.image , (cx, cy) , 7 , (255, 255, 255) , -1) cv2.putText(self.image, "center", (cx - 20, cy - 20) , cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) - print centres + cv2.imshow("Image", self.image) + cv2.waitKey(0) diff --git a/scripts/obj_recogkinect.py b/scripts/obj_recogkinect.py index 89be401756a3449d39a4774d6f0da4423234eea8..587aafd7028773f6089285b07c47f4cb7f9bd907 100755 --- a/scripts/obj_recogkinect.py +++ b/scripts/obj_recogkinect.py @@ -8,7 +8,7 @@ import cv2 #from std_msgs.msg import String from sensor_msgs.msg import Image #ROS Image message to OpenCV2 image converter -from cv_bridge import CvBridge, CvBridgeError +import cv_bridge import CvBridge, CvBridgeError import sys @@ -23,8 +23,7 @@ class image_converter: self.depth_sub = rospy.Subscriber(img_tpc1, Image, self.img_callback) ros_img = cv2.imread('/home/maitreyee/Pictures/IMG_20170927_090557092.jpg') self.bridge = CvBridge() - - # Convert ROS image to OpenCV2 + # Convert ROS image to OpenCV2 def img_callback(self,ros_img) : try: cv2_img = self.bridge.imgmsg_to_cv2(ros_img, "bgr8") @@ -34,11 +33,12 @@ class image_converter: + def main(args): ic = image_converter() rospy.init_node('image_converter',anonymous=True) try: - rospy.spin() + rospy.sleep(30) except KeyboardInterrupt: print("Shutting down") cv2.destroyAllWindows()