赞
踩
导师给了个练手项目,记录下第一个项目过程:
项目要求:检测无人机降落时是否偏移,识别降落点。
想法是用opencv,使用传统的轮廓检测
首先把视频流转换成图片:只提取最后两分钟降落的片段
- import cv2
- import os
- dir=r"..\wrong\video"
- file_name="2022-06-07.mp4"
- if os.path.exists(dir+"/img")==False:
- os.makedirs(dir+"/img")
- file=os.path.join(dir,file_name)
- cap=cv2.VideoCapture(file)
- fps=cap.get(cv2.CAP_PROP_FPS)
- frame_num=cap.get(cv2.CAP_PROP_FRAME_COUNT)
- (width,height)=(cap.get(cv2.CAP_PROP_FRAME_WIDTH),cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
- print(width,height,fps,frame_num)
- num=0
- while True:
- ret,frame=cap.read()
- if ret==False:
- break
- if num>frame_num-fps*120:
- export_path=os.path.join(dir,"img",file_name.split('.')[0]+str(num)+'.jpg')
- #print(export_path)
- a=cv2.imwrite(export_path,frame)
- #print(a)
- num+=1
- print(num)
对图像进行分割,根据面积和颜色特征提取降落点信息:
- import cv2
- import os
- import numpy as np
- from tqdm import tqdm
- dir=r"..\video\CAPTURE_2022-06-07-14-35-13_img\descend"
- files=os.listdir(dir)
- qrcoder=cv2.QRCodeDetector()
- writer = cv2.VideoWriter(dir + '.avi', cv2.VideoWriter_fourcc(*'mp4v'), 30, (1920, 1080), isColor=True)
- for file in tqdm(files):
- if file.endswith(".jpg"):
- file=os.path.join(dir,file)
- img=cv2.imread(file)
- #points=qrcoder.detect(img)
- #if points !=(False,None):
- #img_qrcode = cv2.drawContours(img, [np.int32(points)], 0, (0, 0, 255), 2)
- #else:
- #img_qrcode=img
- img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- img_binary=cv2.adaptiveThreshold(img_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,5,0)
- ret,img_binary=cv2.threshold(img_gray,250,255,cv2.THRESH_BINARY_INV)
- kernel=np.ones((7,7))
- im_erode = cv2.erode(img_binary, kernel)
- im_erode = cv2.erode(im_erode, kernel)
- im_erode = cv2.erode(im_erode, kernel)
- im_dilate=cv2.dilate(im_erode,kernel)
- im_dilate = cv2.dilate(im_dilate, kernel)
- im_dilate = cv2.dilate(im_dilate, kernel)
- im_dilate = cv2.dilate(im_dilate, kernel)
- im_dilate = cv2.dilate(im_dilate, kernel)
- #img_open=cv2.morphologyEx(img_binary,cv2.MORPH_OPEN,kernel)
- img_blur=cv2.medianBlur(im_dilate,3)
- img_canny=cv2.Canny(img_blur,0,255)
- contours, hierarchy = cv2.findContours(img_blur, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
- max_area=0
- for contour in contours:
- lenth = cv2.arcLength(contour, True)
- area = cv2.contourArea(contour)
- if lenth>0 and area/lenth>max_area and 400<area< 1000000:
- contour_max=contour
- max_area=area/lenth
- img_contours = cv2.drawContours(img, contour_max, -1, (0, 0, 255),3)
- box=cv2.minAreaRect(contour_max)
- box=cv2.boxPoints(box)
- box=np.int0(box)
- #img_contours =cv2.drawContours(img,[box],0,(0,0,255),2)
- #cv2.imshow("raw",img)
- #cv2.imshow("qrcode",img_qrcode)
- #cv2.imshow("ray",img_gray)
- #cv2.imshow("binary",img_binary)
- #cv2.imshow("close",img_open)
- #cv2.imshow("blur",img_blur)
- #cv2.imshow("canny", img_canny)
- #cv2.imshow("rota", img_rota)
- #cv2.imshow("img_connect", img_connect)
- #cv2.imshow("img_contours", img_contours)
- #cv2.waitKey()
- writer.write(img_contours)
- writer.release()
泛化性不太好,新到一批数据检测效果不佳,下一步考虑使用yolov5来检测。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。