|
@ -2,7 +2,15 @@ import cv2 |
|
|
|
|
|
|
|
|
# Constant variables definition. |
|
|
# Constant variables definition. |
|
|
DESIRED_HEIGHT = 480 # The input image will be resized to this height, preserving its aspect ratio. |
|
|
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. |
|
|
BLUE_THRESHOLD = 150 # If the blue channel is bigger than this, it is considered background and removed. |
|
|
|
|
|
BINARY_THRESHOLD = 1 # If the pixel is not brighter than this, it is removed before detection. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Colors (assuming BGR order). |
|
|
|
|
|
RED = (0, 0, 255) |
|
|
|
|
|
GREEN = (0, 255, 0) |
|
|
|
|
|
BLUE = (255, 0, 0) |
|
|
|
|
|
YELLOW = (0, 255, 255) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Function definitions |
|
|
# Function definitions |
|
@ -17,8 +25,7 @@ def resizeImage(img): |
|
|
##################################################################################### |
|
|
##################################################################################### |
|
|
|
|
|
|
|
|
# Read image from source |
|
|
# Read image from source |
|
|
img = cv2.imread('/home/stelios/Desktop/IoT/drone.jpg', cv2.IMREAD_COLOR) |
|
|
img = cv2.imread('/home/stelios/Desktop/IoT/Talos_Drones_Tracking_and_Telemetry/camera module/drone.jpg', cv2.IMREAD_COLOR) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Resize image to the desired height. |
|
|
# Resize image to the desired height. |
|
|
img = resizeImage(img) |
|
|
img = resizeImage(img) |
|
@ -35,22 +42,29 @@ for i in range(p[0]): |
|
|
# Convert to grayscale. |
|
|
# Convert to grayscale. |
|
|
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) |
|
|
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) |
|
|
|
|
|
|
|
|
##################################################################### |
|
|
# Threshold the image and find its contours. |
|
|
|
|
|
thres, imgThres = cv2.threshold(img, BINARY_THRESHOLD, 255, cv2.THRESH_BINARY) |
|
|
thres1, img2 = cv2.threshold(img, 1, 255, cv2.THRESH_BINARY) |
|
|
img2, contours, hierarchy = cv2.findContours(imgThres, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
|
|
|
im2, contours, hierarchy = cv2.findContours(img2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Find the biggest contour. |
|
|
maxContour = max(contours, key = cv2.contourArea) |
|
|
maxContour = max(contours, key = cv2.contourArea) |
|
|
|
|
|
|
|
|
cv2.drawContours(imgOriginal, maxContour, -1, (0,0,255), 1) |
|
|
''' |
|
|
print(len(maxContour)) |
|
|
hull = cv2.convexHull(maxContour) |
|
|
|
|
|
cv2.drawContours(imgOriginal, maxContour, -1, (0,0,255), 3) |
|
|
|
|
|
cv2.drawContours(imgOriginal, hull, -1, (255,0,0), 3) |
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
|
|
|
# Get the bounding rectangle of the contour. |
|
|
x,y,w,h = cv2.boundingRect(maxContour) |
|
|
x,y,w,h = cv2.boundingRect(maxContour) |
|
|
|
|
|
|
|
|
# draw the biggest contour (c) in green |
|
|
# Get the centroid of the rectangle. |
|
|
cv2.rectangle(imgOriginal,(x,y),(x+w,y+h),(0,20,255),2) |
|
|
centerX = int( (x + x + w) / 2) |
|
|
|
|
|
centerY = int( (y + y + h) / 2) |
|
|
|
|
|
|
|
|
cv2.imshow('Test', imgOriginal) |
|
|
# Draw the bounding rectangle and its centroid to the image. |
|
|
cv2.waitKey(0) |
|
|
cv2.circle(imgOriginal, (centerX, centerY), 5, YELLOW, 2) |
|
|
|
|
|
cv2.rectangle(imgOriginal, (x,y), (x+w,y+h), RED, 2) |
|
|
|
|
|
|
|
|
|
|
|
cv2.imshow('Output', imgOriginal) |
|
|
|
|
|
cv2.waitKey(0) |