|
@ -4,16 +4,19 @@ import cv2 |
|
|
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 = 150 # 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. |
|
|
BINARY_THRESHOLD = 1 # If the pixel is not brighter than this, it is removed before detection. |
|
|
|
|
|
LINE_THICKNESS = 2 # Thickness of the drawn lines. |
|
|
|
|
|
BLUR_KERNEL_SIZE = 3 # The size of the Gaussian blur kernel. |
|
|
|
|
|
DILATION_KERNEL_SIZE = 5 # The size of the dilation kernel. |
|
|
|
|
|
DILATION_ITERATIONS = 5 # The number of dilation iterations. |
|
|
|
|
|
|
|
|
|
|
|
# Colors (assuming the default BGR order). |
|
|
# Colors (assuming BGR order). |
|
|
|
|
|
RED = (0, 0, 255) |
|
|
RED = (0, 0, 255) |
|
|
GREEN = (0, 255, 0) |
|
|
GREEN = (0, 255, 0) |
|
|
BLUE = (255, 0, 0) |
|
|
BLUE = (255, 0, 0) |
|
|
YELLOW = (0, 255, 255) |
|
|
YELLOW = (0, 255, 255) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Function definitions |
|
|
# -------------- Function definitions ----------------------------- |
|
|
def resizeImage(img): |
|
|
def resizeImage(img): |
|
|
"Resize the input image based on the DESIRED_HEIGHT variable." |
|
|
"Resize the input image based on the DESIRED_HEIGHT variable." |
|
|
p = img.shape; |
|
|
p = img.shape; |
|
@ -22,49 +25,71 @@ def resizeImage(img): |
|
|
img = cv2.resize(img, ( DESIRED_HEIGHT, int(width) )) |
|
|
img = cv2.resize(img, ( DESIRED_HEIGHT, int(width) )) |
|
|
return img |
|
|
return img |
|
|
|
|
|
|
|
|
##################################################################################### |
|
|
def processImage(img): |
|
|
|
|
|
|
|
|
# Read image from source |
|
|
# Resize image to the desired height. |
|
|
img = cv2.imread('/home/stelios/Desktop/IoT/Talos_Drones_Tracking_and_Telemetry/camera module/drone.jpg', cv2.IMREAD_COLOR) |
|
|
resized = resizeImage(img) |
|
|
|
|
|
|
|
|
|
|
|
dim = resized.shape |
|
|
|
|
|
|
|
|
|
|
|
# Remove BLUE |
|
|
|
|
|
noBlue = resized.copy() |
|
|
|
|
|
for i in range(dim[0]): |
|
|
|
|
|
for j in range(dim[1]): |
|
|
|
|
|
if (resized[i,j,0] > BLUE_THRESHOLD): |
|
|
|
|
|
noBlue[i,j,:] = 0 |
|
|
|
|
|
|
|
|
|
|
|
# Convert to grayscale. |
|
|
|
|
|
gray = cv2.cvtColor(noBlue, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
|
|
|
# Resize image to the desired height. |
|
|
# Blur the image. |
|
|
img = resizeImage(img) |
|
|
blur = cv2.GaussianBlur(gray, (BLUR_KERNEL_SIZE, BLUR_KERNEL_SIZE), 0) |
|
|
|
|
|
|
|
|
imgOriginal = img.copy() |
|
|
# Threshold the image and find its contours. |
|
|
|
|
|
_, imgThres = cv2.threshold(blur, BINARY_THRESHOLD, 255, cv2.THRESH_BINARY) |
|
|
|
|
|
|
|
|
# Remove BLUE |
|
|
# Dilate the image. |
|
|
p = img.shape |
|
|
dilated = cv2.dilate(imgThres, (DILATION_KERNEL_SIZE,DILATION_KERNEL_SIZE), iterations=DILATION_ITERATIONS) |
|
|
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. |
|
|
# Find the largest image contour. |
|
|
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) |
|
|
_, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
maxContour = max(contours, key = cv2.contourArea) |
|
|
|
|
|
|
|
|
# Threshold the image and find its contours. |
|
|
''' |
|
|
thres, imgThres = cv2.threshold(img, BINARY_THRESHOLD, 255, cv2.THRESH_BINARY) |
|
|
hull = cv2.convexHull(maxContour) |
|
|
img2, contours, hierarchy = cv2.findContours(imgThres, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
|
cv2.drawContours(imgOriginal, maxContour, -1, (0,0,255), 3) |
|
|
|
|
|
cv2.drawContours(imgOriginal, hull, -1, (255,0,0), 3) |
|
|
|
|
|
''' |
|
|
|
|
|
|
|
|
# Find the biggest contour. |
|
|
# Get the bounding rectangle of the contour. |
|
|
maxContour = max(contours, key = cv2.contourArea) |
|
|
x,y,w,h = cv2.boundingRect(maxContour) |
|
|
|
|
|
|
|
|
''' |
|
|
# Get the centroid of the rectangle. |
|
|
hull = cv2.convexHull(maxContour) |
|
|
objCenterX = int( (x + x + w) / 2) |
|
|
cv2.drawContours(imgOriginal, maxContour, -1, (0,0,255), 3) |
|
|
objCenterY = int( (y + y + h) / 2) |
|
|
cv2.drawContours(imgOriginal, hull, -1, (255,0,0), 3) |
|
|
imgCenterX = int(dim[1]/2) |
|
|
''' |
|
|
imgCenterY = int(dim[0]/2) |
|
|
|
|
|
|
|
|
# Get the bounding rectangle of the contour. |
|
|
# Draw the bounding rectangle and its centroid to the image. |
|
|
x,y,w,h = cv2.boundingRect(maxContour) |
|
|
cv2.circle(resized, (objCenterX, objCenterY), 5, YELLOW, LINE_THICKNESS) |
|
|
|
|
|
cv2.rectangle(resized, (x,y), (x+w,y+h), RED, LINE_THICKNESS) |
|
|
|
|
|
|
|
|
|
|
|
return resized |
|
|
|
|
|
|
|
|
|
|
|
##################################################################################### |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Read image from source |
|
|
|
|
|
img = cv2.imread('/home/stelios/Desktop/IoT/Talos_Drones_Tracking_and_Telemetry/camera module/drone.jpg', cv2.IMREAD_COLOR) |
|
|
|
|
|
|
|
|
# Get the centroid of the rectangle. |
|
|
# Process the image and get the output. |
|
|
centerX = int( (x + x + w) / 2) |
|
|
output = processImage(img) |
|
|
centerY = int( (y + y + h) / 2) |
|
|
|
|
|
|
|
|
|
|
|
# Draw the bounding rectangle and its centroid to the image. |
|
|
cv2.imshow('Output', output) |
|
|
cv2.circle(imgOriginal, (centerX, centerY), 5, YELLOW, 2) |
|
|
|
|
|
cv2.rectangle(imgOriginal, (x,y), (x+w,y+h), RED, 2) |
|
|
|
|
|
|
|
|
|
|
|
cv2.imshow('Output', imgOriginal) |
|
|
# Terminate if the escape key is pressed or the window is closed. |
|
|
cv2.waitKey(0) |
|
|
while True: |
|
|
|
|
|
k = cv2.waitKey() |
|
|
|
|
|
if k == 27 or k == 255: |
|
|
|
|
|
break |
|
|
|
|
|
cv2.destroyAllWindows() |