diff --git a/2.zip b/2.zip new file mode 100644 index 0000000..0e0fc48 Binary files /dev/null and b/2.zip differ diff --git a/2_labels.zip b/2_labels.zip new file mode 100644 index 0000000..24c73ef Binary files /dev/null and b/2_labels.zip differ diff --git a/Description b/Description new file mode 100644 index 0000000..c836cda --- /dev/null +++ b/Description @@ -0,0 +1,24 @@ + +TASK 1 : IMAGE PROCESSING +Using image processing, design an implementation to detect each colored Buoy in the image:You are required to compute the X,Y coordinates of the centre of each buoy detected.Colored Buoys must be isolated from similar colored objects. Opencv Image Processing library is allowed for the same, if you have no prior experience with OpenCV , we will accept Matlab implementations as well. Nevertheless, priority will be given to design using OpenCV .Plus Points For -1.Machine Learning Approach for buoy detection. 2.A line from mid point of the frame to mid point of each buoy. 3.Classifying each buoy with its color name. 3. Implementing ROS with the task. + +SOLUTION: +auv.py(Image Processing Task) + +In the auv.py I completed my task first I took the image using cv2.imread() function converted the image from BGR to HSV. The very simple reason to convert BGR to HSV is that HSV which stands for Hue, Saturation and Value defines a range of color in them rather than BGR which defines a particular set of pixels of color which is quite ambiguous. Then i defined the HSV for blue color. which masks all the color defined in that range i.e blue(here in this reference) The next what I applied is cv2.bitwise_not which masks all the color which are not blue. I then applied smoothing, blurring and morphology for perfect detection of buoys and removal of noise. The contours are then found by the function cv2.findContours() and then stored in the cnts list. I used the moments function to define each and every center of the color. Befor displaying the image I again converted into BGR and displayed it. + +publisher.py(ROS publisher) +I defined a publisher which gets a list of coordinates of the buoys from the auv.py and publishes those messages + +coordinate_subscriber.py(ROS subscriber) +It is the ROS subscriber which are defined in the same channel as of ROS publisher i.e center + +coordinate.msg +It is the custom message + +2.zip +The data labelling task that was given. Each and every image is converted into .xml file + + + + diff --git a/IR_SENSOR_AUV.ino b/IR_SENSOR_AUV.ino new file mode 100644 index 0000000..26a162d --- /dev/null +++ b/IR_SENSOR_AUV.ino @@ -0,0 +1,56 @@ +/* + * rosserial Publisher Example + * Prints "hello world!" + */ + +#include +#include + +float x; +ros::NodeHandle nh; +void messageCb( const std_msgs::Float64& msg) +{ + x = msg.data; + if(x>5) + { + digitalWrite(13, HIGH-digitalRead(13)); + } +} + + +std_msgs::Float64 str_msg; +ros::Publisher chatter("chatter", &str_msg); +ros::Subscriber sub("chatter", messageCb); +float val = 0; + + + +void setup() +{ + Serial.begin(9600); + pinMode(A0, OUTPUT); + pinMode(13, OUTPUT); + nh.initNode(); + nh.advertise(chatter); + nh.subscribe(sub); +} + +void loop() +{ + val = analogRead(A0); + str_msg.data = val ; + + if(val>13) + Serial.println("Out of Rnge"); + delay(100); + Serial.println(val); + delay(100); + chatter.publish( &str_msg ); + nh.spinOnce(); + delay(1000); +} + + + + + diff --git a/Publisher.png b/Publisher.png new file mode 100644 index 0000000..a390ad2 Binary files /dev/null and b/Publisher.png differ diff --git a/Subscriber.png b/Subscriber.png new file mode 100644 index 0000000..16213a7 Binary files /dev/null and b/Subscriber.png differ diff --git a/auv.py b/auv.py new file mode 100644 index 0000000..7175663 --- /dev/null +++ b/auv.py @@ -0,0 +1,104 @@ +import cv2 +import numpy as np +import matplotlib as plt +import numpy as np +image = cv2.imread("/home/rahil/catkin_ws/src/assignment/scripts/auv.png") +centerx = [] + + + +def colordetection(): + while True: + hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) + + mask = cv2.inRange(hsv, (88,100,100), (130, 255, 255)) + inv = cv2.bitwise_not(mask) + inv_2 = cv2.medianBlur(inv, 15) + inv_2 = cv2.GaussianBlur(inv_2,(15,15), 0) + + res = cv2.bitwise_and(hsv, hsv, mask = inv_2) + + kernel = np.ones((5,5), np.uint8) + erosion = cv2.erode(inv_2, kernel, iterations=1) + dilation = cv2.dilate(erosion, kernel, iterations=1) + opening = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, kernel) + closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) + im2, cnts, h = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + M = cv2.moments(cnts[0]) + + if M['m00']!=0: + cX = int(M['m10']/M['m00']) + cY = int(M["m01"]/M["m00"]) + centerx.append(cX) + + + cv2.drawContours(hsv, cnts[0], -1, (0,255,0), 2) + cv2.circle(hsv, (cX, cY), 4, (0,0,0), -1) + cv2.putText(hsv, "center", (cX-20, cY-20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2) + M = cv2.moments(cnts[1]) + if M['m00']!=0: + cX = int(M['m10']/M['m00']) + cY = int(M["m01"]/M["m00"]) + centerx.append(cX) + + + + + cv2.drawContours(hsv, cnts[1], -1, (0,255,0), 2) + cv2.circle(hsv, (cX, cY), 4, (0,0,0), -1) + cv2.putText(hsv, "center", (cX-20, cY-20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2) + M = cv2.moments(cnts[2]) + if M['m00']!=0: + cX = int(M['m10']/M['m00']) + cY = int(M["m01"]/M["m00"]) + + + + centerx.append(cX) + + cv2.drawContours(hsv, cnts[2], -1, (0,255,0), 2) + cv2.circle(hsv, (cX, cY), 4, (0,0,0), -1) + cv2.putText(hsv, "center", (cX-20, cY-20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2) + res_2 = cv2.medianBlur(image, 15) + res_2 = cv2.GaussianBlur(hsv,(15,15), 0) + + res_2 = cv2.medianBlur(res, 15) + res_2 = cv2.GaussianBlur(res_2,(15,15), 0) + kernel = np.ones((5,5), np.uint8) + smoothed = cv2.filter2D(res, -1, kernel) + erosion = cv2.erode(smoothed, kernel, iterations=1) + dilation = cv2.dilate(erosion, kernel, iterations=1) + opening = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, kernel) + closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) + kernel = np.ones((3,3), np.uint8) + smoothed = cv2.filter2D(closing, -1, kernel) + erosion = cv2.erode(smoothed, kernel, iterations=1) + dilation = cv2.dilate(erosion, kernel, iterations=1) + opening = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, kernel) + closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) + hsv = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) + + + + + k = cv2.waitKey(20) & 0xff + cv2.imshow("image", hsv) + cv2.imwrite("output.png", hsv) + return centerx + +def coordinatex(): + center1 = [] + center1 = colordetection() + return center1 + + + + + + + + + + + + diff --git a/coordinate.msg b/coordinate.msg new file mode 100644 index 0000000..9a1cad6 --- /dev/null +++ b/coordinate.msg @@ -0,0 +1 @@ +int32[] x diff --git a/coordinate_subscriber.py b/coordinate_subscriber.py new file mode 100644 index 0000000..d87254c --- /dev/null +++ b/coordinate_subscriber.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +import rospy +from assignment.msg import coordinate + + +def callback(msg): + print(int(msg.x[0])) + + print(int(msg.x[1])) + + print(int(msg.x[2])) + +rospy.init_node("coordinate_subscriber", anonymous=True) +rospy.Subscriber("centers", coordinate, callback) +rospy.spin() + + + + + diff --git a/lane.py b/lane.py new file mode 100644 index 0000000..5db09a6 --- /dev/null +++ b/lane.py @@ -0,0 +1,52 @@ +import cv2 +import numpy as np +import math +cap=cv2.VideoCapture("raw_front.avi") + +while True: + ret,frame = cap.read() + hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + + lower = np.array([10,80,0]) + upper = np.array([190,255,20]) + mask = cv2.inRange(hsv,lower,upper) + vertices= np.array([[200,550],[200,550],[370,400],[400,400],[600,550],[600,550]]) + b = cv2.fillPoly(mask, [vertices],190) + radius=(math.sqrt((600*600-200*200))*0.3)/70 + center = (600/2-200/2) + cv2.putText(mask, "Mean Radius(in m):"+str(radius),(0,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (100,110,150), 2) + + imshape = mask.shape + + lower_left = [imshape[1]/9,imshape[0]] + lower_right = [imshape[1]-imshape[1]/9,imshape[0]] + top_left = [imshape[1]/2-imshape[1]/8,imshape[0]/2+imshape[0]/10] + top_right = [imshape[1]/2+imshape[1]/8,imshape[0]/2+imshape[0]/10] + + diff_1=top_right[0]-top_left[0] + + + edges=cv2.Canny(mask,50,150) + + lines = cv2.HoughLinesP(edges, 1, np.pi/180, 180,20,15) + for line in lines: + coord = lines[0] + diff=center-(coord[0][2]-coord[0][1]) + if (diff-152)==0: + cv2.putText(mask, "Center",(650,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (100,110,150), 2) + cv2.imshow("mask",mask) + if diff>152: + cv2.putText(mask, "Right",(650,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (100,110,150), 2) + cv2.imshow("mask",mask) + if diff<152: + cv2.putText(mask, "Left",(650,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (100,110,150), 2) + cv2.imshow("mask",mask) + + + + k = cv2.waitKey(20)&0xff + if k==27: + break + +cv2.destroyAllWindows() + diff --git a/output.png b/output.png new file mode 100644 index 0000000..8f2004f Binary files /dev/null and b/output.png differ diff --git a/publisher.py b/publisher.py new file mode 100644 index 0000000..766e6f5 --- /dev/null +++ b/publisher.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +import rospy +import numpy as np +import cv2 +from assignment.msg import coordinate +from auv import coordinatex + +e = [] +e = coordinatex() +def talker(): + pub = rospy.Publisher("centers", coordinate, queue_size=10) + rospy.init_node("image", anonymous=True) + rate = rospy.Rate(10) + msg = coordinate() + msg.x = coordinatex() + + + + + while not rospy.is_shutdown(): + rospy.loginfo(msg) + pub.publish(msg) + + + rate.sleep() +if __name__=="__main__": + try: + talker() + except rospy.ROSInterruptException: + pass + + + diff --git a/rosserialpublisher.png b/rosserialpublisher.png new file mode 100644 index 0000000..5da1504 Binary files /dev/null and b/rosserialpublisher.png differ