Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 2.zip
Binary file not shown.
Binary file added 2_labels.zip
Binary file not shown.
24 changes: 24 additions & 0 deletions Description
Original file line number Diff line number Diff line change
@@ -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




56 changes: 56 additions & 0 deletions IR_SENSOR_AUV.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* rosserial Publisher Example
* Prints "hello world!"
*/

#include <ros.h>
#include <std_msgs/Float64.h>

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<std_msgs::Float64> 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);
}





Binary file added Publisher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Subscriber.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 104 additions & 0 deletions auv.py
Original file line number Diff line number Diff line change
@@ -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












1 change: 1 addition & 0 deletions coordinate.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int32[] x
20 changes: 20 additions & 0 deletions coordinate_subscriber.py
Original file line number Diff line number Diff line change
@@ -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()





52 changes: 52 additions & 0 deletions lane.py
Original file line number Diff line number Diff line change
@@ -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()

Binary file added output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions publisher.py
Original file line number Diff line number Diff line change
@@ -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



Binary file added rosserialpublisher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.