Created
August 9, 2022 13:18
-
-
Save raiots/fdc63ac80eeaef75777b9c4a391dbf8e to your computer and use it in GitHub Desktop.
This is a simple ice detect script using opencv, mainly used for UAV auto landing at seasonal ice area
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
# img = cv2.imread('./data/ice1.jpeg') | |
# img = cv2.imread('./data/ice3.jpg') | |
cap = cv2.VideoCapture(1) | |
# cap.set(3,1920) | |
# cap.set(4,1080) | |
img_width = cap.get(3) | |
img_height = cap.get(4) | |
def target_estimate(img, img_width, img_height): | |
target = img[round(img_height/2-50):round(img_height/2+50), round(img_width/2-50):round(img_width/2+50)] | |
average_color_row = np.average(target, axis=0) | |
average_color = np.average(average_color_row, axis=0) | |
# print(average_color) | |
return average_color == 0 | |
def hud(img, img_width, img_height, is_target): | |
line_color = (244, 150, 5) | |
cv2.rectangle(img, (round(img_width/2-50), round(img_height/2-50)), (round(img_width/2+50), round(img_height/2+50)), line_color, 2) | |
cv2.line(img, (0, 0), (round(img_width/2-50), round(img_height/2-50)), line_color, 1) | |
cv2.line(img, (round(img_width/2+50), round(img_height/2+50)), (round(img_width), round(img_height)), line_color, 1) | |
cv2.line(img, (0, round(img_height)), (round(img_width/2-50), round(img_height/2+50)), line_color, 1) | |
cv2.line(img, (round(img_width/2+50), round(img_height/2-50)), (round(img_width), 0), line_color, 1) | |
if is_target: | |
cv2.putText(img, 'REACHABLE', (round(img_width/2-50), round(img_height/2-50)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) | |
else: | |
cv2.putText(img, 'X STOP', (round(img_width/2-50), round(img_height/2-50)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) | |
while True: | |
img = cap.read()[1] | |
# cv2.imshow('image', img) | |
# 设定颜色HSV范围,假定为红色 | |
Lower = np.array([0, 0, 100]) | |
Upper = np.array([180, 100, 255]) | |
# 将图像转化为HSV格式 | |
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
# 去除颜色范围外的其余颜色 | |
mask = cv2.inRange(hsv, Lower, Upper) | |
# cv2.imshow('image1', mask) | |
# 二值化操作 | |
ret, binary = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY) | |
# 获取图像轮廓坐标,其中contours为坐标值,此处只检测外形轮廓 | |
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
aim_box = (0, 0, 0, 0) | |
if len(contours) > 0: | |
# cv2.boundingRect()返回轮廓矩阵的坐标值,四个值为x, y, w, h, 其中x, y为左上角坐标,w,h为矩阵的宽和高 | |
boxes = [cv2.boundingRect(c) for c in contours] | |
for box in boxes: | |
x, y, w, h = box | |
fin_x, fin_y, fin_w, fin_h = aim_box | |
if fin_x * fin_y < x * y: | |
fin_x = x | |
fin_y = y | |
# cv2.rectangle(img, (x, y), (x + w, y + h), (9, 212, 241), -1) | |
# cv2.line(img, (x, y), (x + w, y + h), (32, 53, 210), 2) | |
# cv2.line(img, (x, y + h), (x + w, y), (32, 53, 210), 2) | |
# cv2.putText(img, 'CANNOT LAND', (fin_x, fin_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) | |
# print(boxes) | |
is_target = target_estimate(binary, img_width, img_height) | |
hud(img, img_width, img_height, is_target) | |
print(target_estimate(binary, img_width, img_height)) | |
cv2.imshow('image2', img) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment