In the process of UAV flight, the higher the flight, the farther the field of vision, and only part of the required area, the additional area will become interference, so it is necessary to extract the target area.
This is our high-altitude overhead view, from which we can see that the obvious feature of our target area is: a large area of white background. The method is obvious. First, the white area is deleted and the contour points are extracted. Finally, the target area is extracted from the original image according to the contour points.
Code:
import cv2 import numpy as np import imutils from imutils.perspective import four_point_transform frame = cv2.imread("cut3.JPG") frame = imutils.resize(frame, width=320) #size pictures edges = cv2.Canny(frame,100,300) #Edge detection, just for display #Extraction of white areas hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, np.array((0., 0., 160)), np.array((180., 15.,255.))) mask = cv2.medianBlur(mask,3) #Salt and pepper noise #Extracting Contour Points of White Region cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] aimcnt = None if len(cnts) > 0: #Sort from large to small according to area size cnts = sorted(cnts, key=cv2.contourArea, reverse=True) #Find four corners of the target contour for i in cnts: arc = cv2.arcLength(i, True) #In calculating the circumference, the following functions generally take the circumference as a parameter approx = cv2.approxPolyDP(i, 0.1 * arc, True) #Extracting Approximate Contour Points if len(approx) == 4: aimcnt = approx break #rect = cv2.minAreaRect(cnts[0]) #box = np.int0(cv2.boxPoints(rect)) #Four contour points can also be obtained here, but the effect in this picture is not as good as the above method. #Drawing Four Object Contour Points newFrame=frame.copy() for i in aimcnt: cv2.circle(newFrame, (i[0][0],i[0][1]),4, (0, 0, 255), -1) #Extraction of target region aimFrame = four_point_transform(frame, aimcnt.reshape(4, 2)) aimMask = four_point_transform(mask, aimcnt.reshape(4, 2)) aimEdges = four_point_transform(edges, aimcnt.reshape(4, 2)) aimGray = cv2.cvtColor(aimFrame,cv2.COLOR_BGR2GRAY) #Transform to grayscale and perform Hough circle detection. #Detection circle circles = cv2.HoughCircles(aimGray,cv2.HOUGH_GRADIENT,1,100,param1=100,param2=20,minRadius=1,maxRadius=20) #Draw circles if circles is not None: for i in circles[0,:]: cv2.circle(aimFrame,(i[0],i[1]),i[2],(255,0,0),2) Circles_x = int(i[0]) Circles_y = int(i[1]) cv2.line(aimFrame, (circles[0][0][0], circles[0][0][1]), (circles[0][1][0], circles[0][1][1]), (255, 0, 0), 2) else: Circles_x = 160 #Image Points When Opening Camera Circles_y = 120 cv2.imshow('newFrame',newFrame) cv2.imshow('mask',mask) cv2.imshow('edges',edges) cv2.imshow('aimFrame',aimFrame) cv2.imshow('aimMask',aimMask) cv2.imshow('aimEdges',aimEdges) cv2.waitKey(0)
Results:
Source download: