有没有一种方法可以仅使用opencv来检测降落区域以进行自主降落?

时间:2019-11-27 22:33:51

标签: python opencv image-processing

我的目标: 1-仅使用已知区域中的图像处理来为无人机确定合适的着陆区域

2-在无人机着陆时检测物体并避开物体。

我正在尝试对无人机在已知区域的自动降落进行区域检测。 我已经绘制了一个小标记,作为一个矩形用于自主着陆。但是我希望系统在适合着陆的随机区域中绘制此矩形,并且此标记不应覆盖任何对象。如果有物体,则将做出另一种选择来检测区域。

如果无法打开图像,则可以使用此链接:

https://drive.google.com/open?id=1fHfE3IP0JKPNk8DpQaFXX4jk6mnEq3Xz

Created on Mon Oct 28 16:23:44 2019

@author: Veysel
"""

from skimage.feature import peak_local_max
from skimage.morphology import watershed
from scipy import ndimage
import numpy as np
import imutils
import cv2


img = cv2.imread('grass.jpg')
kernel = np.array([
       [0, 0, 1, 0, 0], # elliptical Kernel 5x5
       [0, 1, 1, 1, 0],
       [1, 1, 1, 1, 1],
       [0, 1, 1, 1, 0],
       [0, 0, 1, 0, 0]], dtype="uint8")

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)


hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

edge_low = np.array([ 30 , 25, 25])
edge_high = np.array([ 74 ,  255, 105])
mask = cv2.inRange(hsv_img,edge_low,edge_high)


imask = mask>0
green = np.zeros_like(img,np.uint8)
green[imask] = img[imask]

ret,th = cv2.threshold(gray, 177, 255,cv2.THRESH_BINARY)



opening = cv2.morphologyEx(blur, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)


edges = cv2.Canny(closing,100,200)
dilation = cv2.dilate(edges, kernel, iterations=2)

contours, hierarchy = cv2.findContours(dilation.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

contours = sorted(contours, key = cv2.contourArea, reverse = True)

cnt = contours[0]


area = cv2.contourArea(cnt)

# Draw the contour 
img_copy = img.copy()
final = cv2.drawContours(img_copy, contours, contourIdx = -1, color = (255, 0, 0), thickness = 2)


draw = cv2.drawContours(img, contours, -1, (0,255,0), 2)

# The first order of the contours
c_0 = contours[0]

# Detect the convex contour
hull = cv2.convexHull(c_0)
img_copy = img.copy()
img_hull = cv2.drawContours(img_copy, contours = [hull], 
                            contourIdx = -0, 
                            color = (255, 0, 0), thickness = 2)

hierarchy = hierarchy[0] # get the actual inner list of hierarchy descriptions
for component in zip(contours, hierarchy):
    currentContour = component[0]
    currentHierarchy = component[1]
    x,y,w,h = cv2.boundingRect(currentContour)
    if currentHierarchy[2] < 0:
        # these are the innermost child components
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
    elif currentHierarchy[3] < 0:
        # these are the outermost parent components
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)


mask = np.zeros(shape = img.shape, dtype = "uint8")

for c in contours:
    (x, y, w, h) = cv2.boundingRect(c)

    cv2.rectangle(img = mask, 
        pt1 = (x, y), 
        pt2 = (x + w, y + h), 
        color = (255, 255, 255), 
        thickness = -1)
image = cv2.bitwise_and(src1 = img, src2 = mask)



color =(255,0,0)
x1 = np.random.random_integers(300) #I want this rectangle should be drawn
y1 = np.random.random_integers(300) # any area where is no objects
h1 = 50
w1 = 50

cv2.rectangle(green, (x1,y1), (x1+w1,y1+h1), color, -1)

cv2.imshow("FilterImage",image1)
cv2.imshow("DilationCanny",dilation)
cv2.imshow("HSV",hsv_img)
cv2.imshow("HSV2BGR",green)



cv2.waitKey(0)
cv2.destroyAllWindows()

Input_Image_Simplest_area_for_landing

Output_Image

0 个答案:

没有答案