diff --git a/scripts/contour_centroid.py b/scripts/contour_centroid.py old mode 100644 new mode 100755 index 4dd7f6b5652ecb127a7621ce889d5f2191cfeaed..d4bdd75127432b33107f8603f717d026c80a8540 --- a/scripts/contour_centroid.py +++ b/scripts/contour_centroid.py @@ -4,55 +4,66 @@ from obj_recogkinect import * import cv2 import numpy as np +from collections import Counter from matplotlib import pyplot as plt -class CentroidContour(): - - def __init__(self): - - self.image = cv2.imread('/home/maitreyee/IMG_20171011_162319075.jpg') - - def contourXO(self,contours): - #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) - #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) - hull = cv2.convexHull(c) - hullArea = cv2.contourArea(hull) - solidity = float(area) / hullArea - char = '?' - if solidity > 0.9: - char = "O" - elif solidity > 0.5: - char = "X" - if char != "?": - 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() - ctr = np.array(cnts).reshape((-1, 1, 2)).astype(np.int32) - for c in cnts: - m = cv2.moments(c) - cx = int(m["m10"] / m["m00"]) - cy = int(m["m01"] / m["m00"]) - 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) - cv2.imshow("Image", self.image) - cv2.waitKey(0) + +img1 = cv2.imread('/home/maitreyee/Desktop/board.jpg') +img = cv2.resize(img1, (640, 480), interpolation=cv2.INTER_CUBIC) +gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) +cimg = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR) +hsv1 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) +blur = cv2.GaussianBlur(gray, (17, 17), 0) +(ret, thresh) = cv2.threshold(blur, 100, 250, cv2.THRESH_BINARY) +(im2, contours, hierarchy) = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) +ratio = img.shape[0] / float(img.shape[0]) +circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, + param1=620, param2=10, minRadius=39, maxRadius=42) +circles = np.uint16(np.around(circles)) + +for c in contours: + shape = "unidentified" + peri = cv2.arcLength(c, True) + approx = cv2.approxPolyDP(c, 0.04 * peri, True) + # if the shape has 4 vertices, it is either a square or + # a rectangle + if len(approx) == 4: + # compute the bounding box of the contour and use the + # bounding box to compute the aspect ratio + + (x, y, w, h) = cv2.boundingRect(approx) + ar = w / float(h) + + M = cv2.moments(c) + cX = int((M["m10"] / M["m00"]) * ratio) + cY = int((M["m01"] / M["m00"]) * ratio) + mean = cv2.mean((cX, cY), mask=None) + c = c.astype("float") + c *= ratio + c = c.astype("int") + cv2.drawContours(img, [c], -1, (0, 255, 0), 2) + cv2.circle(img, (cX, cY), 7, (255, 255, 255), -1) + + # a square will have an aspect ratio that is approximately + # equal to one, otherwise, the shape is a rectangle + shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle" + for i in circles[0, :]: + # draw the center of the circle§ + cv2.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2) + cv2.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3) + + lower = np.array([1, 25, 25]) + upper = np.array([10, 255, 255]) + mask = cv2.inRange(hsv1, lower, upper) + output = cv2.bitwise_and(img, img, mask=mask) + + # Threshold the HSV image to get only red colors + + print mean + cv2.imshow(np.hstack([hsv1,output])) + cv2.imshow('contours',img) + cv2.waitKey(0) + cv2.destroyAllWindows() +plt.imshow(cimg) +plt.show() + diff --git a/scripts/obj_recogkinect.py b/scripts/obj_recogkinect.py index 587aafd7028773f6089285b07c47f4cb7f9bd907..7e1615b1e725a2292dd3f9581cfe0cdd8e306662 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 -import cv_bridge import CvBridge, CvBridgeError +from cv_bridge import CvBridge, CvBridgeError import sys