Browse Source

Added initial drone detection algorithm

master
Stelios Giakoumidis 5 years ago
parent
commit
756ddd8989
  1. 58
      camera module/main.py

58
camera module/main.py

@ -1,4 +1,56 @@
import servo
import cv2
# Constant variables definition.
DESIRED_HEIGHT = 480 # The input image will be resized to this height, preserving its aspect ratio.
BLUE_THRESHOLD = 128 # If the blue channel is bigger than this, it is considered background and removed.
# Function definitions
def resizeImage(img):
"Resize the input image based on the DESIRED_HEIGHT variable."
p = img.shape;
aspectRatio = p[0]/p[1]
width = DESIRED_HEIGHT*aspectRatio
img = cv2.resize(img, ( DESIRED_HEIGHT, int(width) ))
return img
#####################################################################################
# Read image from source
img = cv2.imread('/home/stelios/Desktop/IoT/drone.jpg', cv2.IMREAD_COLOR)
# Resize image to the desired height.
img = resizeImage(img)
imgOriginal = img.copy()
# Remove BLUE
p = img.shape
for i in range(p[0]):
for j in range(p[1]):
if (img[i,j,0] > BLUE_THRESHOLD):
img[i,j,:] = 0
# Convert to grayscale.
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
#####################################################################
thres1, img2 = cv2.threshold(img, 1, 255, cv2.THRESH_BINARY)
im2, contours, hierarchy = cv2.findContours(img2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
maxContour = max(contours, key = cv2.contourArea)
cv2.drawContours(imgOriginal, maxContour, -1, (0,0,255), 1)
print(len(maxContour))
x,y,w,h = cv2.boundingRect(maxContour)
# draw the biggest contour (c) in green
cv2.rectangle(imgOriginal,(x,y),(x+w,y+h),(0,20,255),2)
cv2.imshow('Test', imgOriginal)
cv2.waitKey(0)
servo.SetAngleUp(180)
servo.SetAngleDown(180)
Loading…
Cancel
Save