Python 是否有方法仅使用opencv检测自动着陆的着陆区域?

Python 是否有方法仅使用opencv检测自动着陆的着陆区域?,python,opencv,image-processing,Python,Opencv,Image Processing,我的目标: 1-仅使用已知区域的图像处理来检测无人机的适当着陆区域 2-无人机着陆时探测目标并避开它们 我正试图对无人驾驶飞机在已知区域自动着陆进行区域探测。 我已经画了一个小标记作为自动着陆的矩形。但是我希望系统在适合着陆的随机区域绘制这个矩形,并且这个标记不应该覆盖任何对象。如果有物体,则会选择另一个检测区域 如果无法打开图像,则可以使用此链接: 创建于2019年10月28日星期一16:23:44 @作者:维瑟 """ 从skimage.feature导入峰值\本地\最大值 从skimag

我的目标: 1-仅使用已知区域的图像处理来检测无人机的适当着陆区域

2-无人机着陆时探测目标并避开它们

我正试图对无人驾驶飞机在已知区域自动着陆进行区域探测。 我已经画了一个小标记作为自动着陆的矩形。但是我希望系统在适合着陆的随机区域绘制这个矩形,并且这个标记不应该覆盖任何对象。如果有物体,则会选择另一个检测区域


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

创建于2019年10月28日星期一16:23:44
@作者:维瑟
"""
从skimage.feature导入峰值\本地\最大值
从skimage.com导入分水岭
从scipy导入ndimage
将numpy作为np导入
导入imutils
进口cv2
img=cv2.imread('grass.jpg')
kernel=np.array([
[0,0,1,0,0],#椭圆核5x5
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0,0,1,0,0]],dtype=“uint8”)
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
模糊=cv2.高斯模糊(灰色,(5,5),0)
hsv_img=cv2.cvt颜色(img,cv2.COLOR_BGR2HSV)
边低=np.数组([30,25,25])
edge_high=np.数组([74255105])
遮罩=cv2.inRange(hsv\U img,边缘低,边缘高)
imask=掩码>0
绿色=np.零类(img,np.uint8)
绿色[imask]=img[imask]
ret,th=cv2.阈值(灰色,177255,cv2.阈值)
opening=cv2.morphologyEx(模糊,cv2.MORPH_OPEN,内核)
关闭=cv2.morphologyEx(打开,cv2.MORPH\u关闭,内核)
边缘=cv2.Canny(闭合,100200)
扩张=cv2。扩张(边、核、迭代次数=2)
轮廓,层次=cv2.findContours(dictional.copy(),cv2.RETR_树,cv2.CHAIN_近似_简单)
轮廓=已排序(轮廓,键=cv2.contourArea,反转=真)
cnt=等高线[0]
面积=cv2。轮廓面积(cnt)
#画轮廓
img_copy=img.copy()
最终=cv2。绘制轮廓(img_副本,轮廓,轮廓ID=-1,颜色=(255,0,0),厚度=2)
图纸=cv2。图纸轮廓(img,轮廓-1,(0255,0),2)
#等高线的一阶
c_0=等高线[0]
#检测凸轮廓
外壳=cv2.凸形外壳(c_0)
img_copy=img.copy()
img_外壳=cv2.图纸轮廓(img_副本,轮廓=[外壳],
轮廓线=-0,
颜色=(255,0,0),厚度=2)
hierarchy=hierarchy[0]#获取层次结构描述的实际内部列表
对于zip中的组件(轮廓、层次):
currentContour=组件[0]
currentHierarchy=组件[1]
x、 y,w,h=cv2.boundingRect(当前轮廓)
如果currentHierarchy[2]<0:
#这些是最里面的子组件
cv2.矩形(img,(x,y),(x+w,y+h),(0,0255),2)
elif currentHierarchy[3]<0:
#这些是最外层的父组件
cv2.矩形(img,(x,y),(x+w,y+h),(255,0,0),2)
掩码=np.0(shape=img.shape,dtype=“uint8”)
对于等高线中的c:
(x,y,w,h)=cv2.boundingRect(c)
cv2.矩形(img=遮罩,
pt1=(x,y),
pt2=(x+w,y+h),
颜色=(255、255、255),
厚度=-1)
image=cv2.按位_和(src1=img,src2=mask)
颜色=(255,0,0)
x1=np.random.random_整数(300)#我希望这个矩形应该被画出来
y1=np.random.random_整数(300)#没有对象的任何区域
h1=50
w1=50
cv2.矩形(绿色,(x1,y1),(x1+w1,y1+h1),颜色,-1)
cv2.imshow(“过滤器图像”,图像1)
cv2.imshow(“扩张术”,扩张术)
cv2.imshow(“HSV”,HSV\U img)
cv2.imshow(“HSV2BGR”,绿色)
cv2.等待键(0)
cv2.destroyAllWindows()


您可以添加原始输入图像和预期输出图像吗?图像中预期的着陆区域是哪个?我已经添加了输入和输出(预期)图像。你可以在帖子上看到它们。我可以检测对象并绘制一个矩形作为标记,但这个矩形是随机绘制的。所以它可以在对象上绘制,我不想要它。我只想检测对象并随机选择一个安全区域用于着陆。我想使用矩形将这个安全区域作为标记。如果你不能打开图像,那么你可以可以使用此链接:
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()