def intersection(self, v1,v2): inter = cv2.rotatedRectangleIntersection(self.rotatedRect(v1),self.rotatedRect(v2)) if inter is None: return None if inter[1] is None: return None inter = np.array( [i[0,:] for i in inter[1]]) inter = cv2.convexHull(cv2.rotatedRectangleIntersection(self.rotatedRect(v1),self.rotatedRect(v2))[1]) inter = np.array( [i[0] for i in inter]) return inter
def set_not_wait_for_person(self, tracker_db, person_id_list, crossing_rect): if cv2.rotatedRectangleIntersection(self.rect, crossing_rect)[0] == cv2.INTERSECT_NONE: self.not_wait_for_person = False else: for person_id in person_id_list: person = tracker_db[person_id] if cv2.rotatedRectangleIntersection(person.rect, crossing_rect)[0] != cv2.INTERSECT_NONE: self.not_wait_for_person = True return self.not_wait_for_person = False
def presave(IMG_Str): pic = cv.imread(IMG_Str[1], cv.IMREAD_UNCHANGED) cand_rect = [] image = pic.copy() gray = image[:, :, 0] gray_top = gray[0, :] if np.nonzero(gray[0, :])[0].size > 0 else gray[1, :] gray_bottom = gray[2048, :] if np.nonzero( gray[2048, :])[0].size > 0 else gray[2047, :] cord_top = np.nonzero(gray_top)[0][0] cord_bottom = np.nonzero(gray_bottom)[0][0] cord_left = cord_bottom cord_right = cord_top angle = 30 if cord_top > 1000 else 60 big_rect = ((2049 // 2, 2049 // 2), (1500, 1500), -angle) # pts=np.array([[cord_top,0],[0,cord_left],[cord_bottom,2048],[2048,cord_right]],dtype=np.int64) # pts=pts.reshape((-1,1,2)) # cv.polylines(image,[pts],True,(0,0,255)) # small_rect = ((222 + crop_width // 2, 914 + crop_height // 2), (crop_width, crop_height), 0) # ppt=cv.boxPoints(big_rect).astype(np.int64) # # ppt = ppt.reshape((-1, 1, 2)) # cv.polylines(image,[ppt],True,(0,0,255),thickness=5) # inter_type = cv.rotatedRectangleIntersection(big_rect, small_rect) # print(inter_type[0]) # # cv.rectangle(image,(222,914),(734,1426),(0,0,255),thickness=5) # cv.imwrite("test3.bmp",image) for x in range(1, pic.shape[1] - crop_width): for y in range(1, pic.shape[0] - crop_height): small_rect = ((x + crop_width // 2, y + crop_height // 2), (crop_width, crop_height), 0) inter_type = cv.rotatedRectangleIntersection(big_rect, small_rect) if inter_type[0] == 2: cand_rect.append([x, y]) cand_np = np.asarray(cand_rect) np.save("cand_angle_60.npy", cand_np)
def iou_rotate_calculate2(boxes1, boxes2): ious = [] if boxes1.shape[0] != 0: area1 = boxes1[:, 2] * boxes1[:, 3] area2 = boxes2[:, 2] * boxes2[:, 3] for i in range(boxes1.shape[0]): temp_ious = [] r1 = ((boxes1[i][0], boxes1[i][1]), (boxes1[i][2], boxes1[i][3]), boxes1[i][4]) r2 = ((boxes2[i][0], boxes2[i][1]), (boxes2[i][2], boxes2[i][3]), boxes2[i][4]) int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area1[i] + area2[i] - int_area) temp_ious.append(inter) else: temp_ious.append(0.0) ious.append(temp_ious) return np.array(ious, dtype=np.float32)
def bbox_overlaps(boxes, query_boxes): """ Calculate IoU(intersection-over-union) and angle difference for each input boxes and query_boxes. """ N = boxes.shape[0] K = query_boxes.shape[0] boxes = np.round(boxes, decimals=2) query_boxes = np.round(query_boxes, decimals=2) overlaps = np.reshape(np.zeros((N, K)), (N, K)) delta_theta = np.reshape(np.zeros((N, K)), (N, K)) for k in range(K): rect1 = ((query_boxes[k][0], query_boxes[k][1]), (query_boxes[k][2], query_boxes[k][3]), query_boxes[k][4]) for n in range(N): rect2 = ((boxes[n][0], boxes[n][1]), (boxes[n][2], boxes[n][3]), boxes[n][4]) # can check official document of opencv for details num_int, points = cv2.rotatedRectangleIntersection(rect1, rect2) S1 = query_boxes[k][2] * query_boxes[k][3] S2 = boxes[n][2] * boxes[n][3] if num_int == 1 and len(points) > 2: s = cv2.contourArea(cv2.convexHull(points, returnPoints=True)) overlaps[n][k] = s / (S1 + S2 - s) elif num_int == 2: overlaps[n][k] = min(S1, S2) / max(S1, S2) delta_theta[n][k] = np.abs(query_boxes[k][4] - boxes[n][4]) return overlaps, delta_theta
def GetIntersection(box1,box2): ## centerx, centery w, h, thetta # rect1 = ((1,1),(2,2),0) # rect2 = ((1,2),(2,2),0) rect1 = cv2.minAreaRect(box1) rect2 = cv2.minAreaRect(box2) r1 = cv2.rotatedRectangleIntersection(rect1, rect2) # has no intersection if r1[0] == 0: return 0 x = 0 y = 0 p = [] len_p = r1[1].shape[0] for i in range(len_p): p.append(Point(r1[1][i][0][0], r1[1][i][0][1])) x += r1[1][i][0][0] y += r1[1][i][0][1] c = Point(x / len_p, y/len_p) key = cmp_to_key(lambda x,y: cmp(x, y,c)) pp = sorted(p, key=key) r = np.full((len_p, 2), 0.0, dtype='float32') for i in range(len(pp)): print(pp[i].x, pp[i].y) r[i][0] = pp[i].x r[i][1] = pp[i].y intersectArea = cv2.contourArea(r) return intersectArea
def rbox_overlaps(boxes, query_boxes, indicator=None, thresh=1e-1): # rewrited by cython N = boxes.shape[0] K = query_boxes.shape[0] a_tt = boxes[:, 4] a_ws = boxes[:, 2] - boxes[:, 0] a_hs = boxes[:, 3] - boxes[:, 1] a_xx = boxes[:, 0] + a_ws * 0.5 a_yy = boxes[:, 1] + a_hs * 0.5 b_tt = query_boxes[:, 4] b_ws = query_boxes[:, 2] - query_boxes[:, 0] b_hs = query_boxes[:, 3] - query_boxes[:, 1] b_xx = query_boxes[:, 0] + b_ws * 0.5 b_yy = query_boxes[:, 1] + b_hs * 0.5 overlaps = np.zeros((N, K), dtype=np.float32) for k in range(K): box_area = b_ws[k] * b_hs[k] for n in range(N): if indicator is not None and indicator[n, k] < thresh: continue ua = a_ws[n] * a_hs[n] + box_area rtn, contours = cv2.rotatedRectangleIntersection( ((a_xx[n], a_yy[n]), (a_ws[n], a_hs[n]), a_tt[n]), ((b_xx[k], b_yy[k]), (b_ws[k], b_hs[k]), b_tt[k])) if rtn == 1: ia = cv2.contourArea(contours) overlaps[n, k] = ia / (ua - ia) elif rtn == 2: ia = np.minimum(ua - box_area, box_area) overlaps[n, k] = ia / (ua - ia) return overlaps
def nms_rotate_cpu(boxes, scores, iou_threshold, max_output_size): keep = []#保留框的结果集合 order = scores.argsort()[::-1]#对检测结果得分进行降序排序 num = boxes.shape[0]#获取检测框的个数 suppressed = np.zeros((num), dtype=np.int) angle_det = np.zeros((num)) for _i in range(num): if len(keep) >= max_output_size:#若当前保留框集合中的个数大于max_output_size时,直接返回 break i = order[_i] if suppressed[i] == 1:#对于抑制的检测框直接跳过 continue keep.append(i)#保留当前框的索引 # (midx,midy),(width,height), angle) r1 = ((boxes[i, 0], boxes[i, 1]), (boxes[i, 2], boxes[i, 3]), boxes[i, 4]) # r1 = ((boxes[i, 1], boxes[i, 0]), (boxes[i, 3], boxes[i, 2]), boxes[i, 4]) #根据box信息组合成opencv中的旋转bbox # print("r1:{}".format(r1)) area_r1 = boxes[i, 2] * boxes[i, 3]#计算当前检测框的面积 for _j in range(_i + 1, num):#对剩余的而进行遍历 j = order[_j] if suppressed[i] == 1: continue r2 = ((boxes[j, 0], boxes[j, 1]), (boxes[j, 2], boxes[j, 3]), boxes[j, 4]) area_r2 = boxes[j, 2] * boxes[j, 3] inter = 0.0 int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]#求两个旋转矩形的交集,并返回相交的点集合 if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True)#求点集的凸边形 int_area = cv2.contourArea(order_pts)#计算当前点集合组成的凸边形的面积 inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + 0.0000001) if inter >= iou_threshold:#对大于设定阈值的检测框进行滤除 suppressed[j] = 1 angle_det[j]=np.abs( r1 [2]-r2[2]) return np.array(keep, np.int64),angle_det
def nms_rotate_cpu_tf(boxes, scores, iou_threshold, max_output_size): #keep = [] keep = tf.placeholder(shape=[max_output_size], dtype=tf.float32) #order = scores.argsort()[::-1] order = tf.argsort(scores, direction='DESCENDING') num = boxes.shape[0] #suppressed = np.zeros((num), dtype=np.int) suppressed = tf.zeros([num], tf.int32) for _i in range(num): if len(keep) >= max_output_size: break i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((boxes[i, 0], boxes[i, 1]), (boxes[i, 2], boxes[i, 3]), boxes[i, 4]) area_r1 = boxes[i, 2] * boxes[i, 3] for _j in range(_i + 1, num): j = order[_j] if suppressed[i] == 1: continue if np.sqrt((boxes[i, 0] - boxes[j, 0])**2 + (boxes[i, 1] - boxes[j, 1])**2) > (boxes[i, 2] + boxes[j, 2] + boxes[i, 3] + boxes[j, 3]): inter = 0.0 else: r2 = ((boxes[j, 0], boxes[j, 1]), (boxes[j, 2], boxes[j, 3]), boxes[j, 4]) area_r2 = boxes[j, 2] * boxes[j, 3] inter = 0.0 try: int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + cfgs.EPSILON) except: """ cv2.error: /io/opencv/modules/imgproc/src/intersection.cpp:247: error: (-215) intersection.size() <= 8 in function rotatedRectangleIntersection """ # print(r1) # print(r2) inter = 0.9999 if inter >= iou_threshold: suppressed[j] = 1 return np.array(keep, np.int64)
def merge_intersected(contours): ret_val = [] merged_index = [] #lista indeksa kontura koje su već spojene sa nekim for i,contour1 in enumerate(contours): #slova if i in merged_index: continue rect1 = cv2.minAreaRect(contour1) for j,contour2 in enumerate(contours): #kukice if j in merged_index or i == j: continue rect2 = cv2.minAreaRect(contour2) #TODO 2 - izvršiti spajanje kukica iznad slova #spajanje dva niza je moguće obaviti funkcijom np.concatenate((contour1,contour2)) indicator, vertices = cv2.rotatedRectangleIntersection(rect1, rect2) if indicator>0: #spajanje kontura ret_val.append(np.concatenate((contour1,contour2))) merged_index.append(i) merged_index.append(j) #svi regioni koji se nisu ni sa kim spojili idu u listu kontura, bez spajanja for idx,contour in enumerate(contours): if idx not in merged_index: ret_val.append(contour) return ret_val
def two_d_rotate_iou(box, boxes): """Compute 2D rotate IOU between a 2D bounding box 'box' and a list :param box: a numpy array in the form of [xc, yc, w, h, angle] where (xc,yc) are image coordinates of the center of the bounding box, and (w, h) are the size of the bounding box and angle is the rotate angle of the bbox(in rad format). :param boxes: a numpy array formed as a list of boxes in the form [[xc, yc, w, h, angle], [xc, yc, w, h, angle]]. :return iou: a numpy array containing 2D rotate IOUs between box and every element in numpy array boxes. """ iou = np.zeros(len(boxes), np.float64) #non_empty = np.zeros(len(boxes), np.bool) rect1 = ((box[0], box[1]), (box[2], box[3]), box[4] * 180 / np.pi) area1 = box[2] * box[3] for i, box2 in enumerate(boxes): rect2 = ((box2[0], box2[1]), (box2[2], box2[3]), box2[4] * 180 / np.pi) int_pts = cv2.rotatedRectangleIntersection(rect1, rect2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) area2 = box2[2] * box2[3] union_area = area1 + area2 - int_area iu = int_area * 1.0 / union_area iou[i] = iu return iou.round(3)
def rotate_cpu_nms(dets, threshold): ''' Parameters ---------------- dets: (N, 6) --- x_ctr, y_ctr, height, width, angle, score threshold: 0.7 or 0.5 IoU ---------------- Returns ---------------- keep: keep the remaining index of dets ''' keep = [] scores = dets[:, -1] tic = time.time() order = scores.argsort()[::-1] ndets = dets.shape[0] # print "nms start" # print ndets suppressed = np.zeros((ndets), dtype=np.int) for _i in range(ndets): i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((dets[i, 0], dets[i, 1]), (dets[i, 3], dets[i, 2]), dets[i, 4]) area_r1 = dets[i, 2] * dets[i, 3] for _j in range(_i + 1, ndets): # tic = time.time() j = order[_j] if suppressed[j] == 1: continue r2 = ((dets[j, 0], dets[j, 1]), (dets[j, 3], dets[j, 2]), dets[j, 4]) area_r2 = dets[j, 2] * dets[j, 3] ovr = 0.0 # +++ # d = math.sqrt((dets[i,0] - dets[j,0])**2 + (dets[i,1] - dets[j,1])**2) # d1 = math.sqrt(dets[i,2]**2 + dets[i,3]**2) # d2 = math.sqrt(dets[j,2]**2 + dets[j,3]**2) # if d<d1+d2: # +++ int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) # t2 = time.time() int_area = cv2.contourArea(order_pts) # t3 = time.time() ovr = int_area * 1.0 / (area_r1 + area_r2 - int_area) if ovr >= threshold: suppressed[j] = 1 # print t1 - tic, t2 - t1, t3 - t2 # print print(time.time() - tic) print("nms done") return keep
def group_rects(self, rects): pairs = [] prev_rect = None prev_stretched = None for rect in rects: stretched = stretch_rect(rect, self.stretch_factor) if prev_rect is None: prev_rect = rect prev_stretched = stretched continue intersect, region = cv2.rotatedRectangleIntersection(prev_stretched, stretched) if region is None: prev_rect = rect prev_stretched = stretched continue base_y = (prev_rect[0][1] + rect[0][1]) / 2.0 if region[0][0][1] < base_y: pairs.append((prev_rect, rect)) prev_rect = None prev_stretched = None else: prev_rect = rect prev_stretched = stretched return pairs
def rectangle_iou(rectA, rectB): """Computes the ratio of the intersection area of the input rectangles to the (sum of rectangle areas - intersection area). Used with the NMS function. :param rectA: a rectangle described by (x,y,w,h) :type rectA: tuple :param rectB: a rectangle describe by (x,y,w,h) :type rectB: tuple :returns: The ratio of overlap between rectA and rectB (intersection area/(rectA area + rectB area - intersection area) """ rectAd = ((rectA[0], rectA[1]),(rectA[2],rectA[3]), 0) rectBd = ((rectB[0], rectB[1]),(rectB[2], rectB[3]), 0) # rotatedRectangleIntersection expects (rrect, rrect) as ((cx, cy), (w, h), deg) retVal, region = cv2.rotatedRectangleIntersection(rectAd, rectBd) # cv2.rotatedRectangleIntersection -- rectangles passed are rotated around their centers. if cv2.INTERSECT_NONE == retVal: return 0 elif cv2.INTERSECT_FULL == retVal: return 1.0 else: # https://docs.opencv.org/master/d3/dc0/group__imgproc__shape.html#ga2c759ed9f497d4a618048a2f56dc97f1 intersection_area = cv2.contourArea(region) rectA_area = rectA[2] * rectA[3] rectB_area = rectB[2] * rectB[3] return intersection_area / (rectA_area + rectB_area - intersection_area)
def iou_rotate_calculate1(boxes1, boxes2, use_gpu=True, gpu_id=0): # start = time.time() if use_gpu: ious = rbbx_overlaps(boxes1, boxes2, gpu_id) else: area1 = boxes1[:, 2] * boxes1[:, 3] area2 = boxes2[:, 2] * boxes2[:, 3] ious = [] for i, box1 in enumerate(boxes1): temp_ious = [] r1 = ((box1[0], box1[1]), (box1[2], box1[3]), box1[4]) for j, box2 in enumerate(boxes2): r2 = ((box2[0], box2[1]), (box2[2], box2[3]), box2[4]) int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area1[i] + area2[j] - int_area) temp_ious.append(inter) else: temp_ious.append(0.0) ious.append(temp_ious) # print('{}s'.format(time.time() - start)) return np.array(ious, dtype=np.float32)
def findSquares(msers, canvas=None): """ Find squares within MSERs using simple square tests """ msers.sort(key=lambda points: len(points), reverse=True) squares = [] for i, points in enumerate(msers): box = ColorBox(i, RotatedRect(*cv2.minAreaRect(points))) if not all(box.rect.size): continue if len(points) / (box.rect.size[0] * box.rect.size[1]) < MIN_SQUARENESS_RATIO: continue if max(box.rect.size) / min(box.rect.size) > MAX_ASPECT_RATIO: continue # check that this box don't intersect with accepted boxes separated = True for accepted in squares: retval, _ = cv2.rotatedRectangleIntersection( accepted.rect, box.rect) if retval != cv2.INTERSECT_NONE: separated = False break if not separated: continue squares.append(box) if canvas is not None: canvas[points[:, 1], points[:, 0]] = 128 return squares
def rbox_nms(dets, threshold): keep = [] scores = dets[:, -1] theta = 180 * dets[:, 4] / np.pi order = scores.argsort()[::-1] ndets = dets.shape[0] suppressed = np.zeros((ndets), dtype=np.int) for _i in range(ndets): i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((dets[i, 0], dets[i, 1]), (dets[i, 2], dets[i, 3]), theta[i]) area_r1 = dets[i, 2] * dets[i, 3] for _j in range(_i + 1, ndets): j = order[_j] if suppressed[j] == 1: continue r2 = ((dets[j, 0], dets[j, 1]), (dets[j, 2], dets[j, 3]), theta[j]) area_r2 = dets[j, 2] * dets[j, 3] ovr = 0.0 int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if None != int_pts: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) ovr = int_area * 1.0 / (area_r1 + area_r2 - int_area) if ovr >= threshold: suppressed[j] = 1 return keep
def iou_rotate_calculate(boxes1, boxes2, rad=True): """Calculate iou for a pair of rot box.(Matrix not support) """ area1 = boxes1[:, 2] * boxes1[:, 3] area2 = boxes2[:, 2] * boxes2[:, 3] ious = np.zeros([boxes1.shape[0], boxes2.shape[0]]) if rad: boxes1[:, 4] *= 180 / math.pi boxes2[:, 4] *= 180 / math.pi for i in range(len(boxes1)): r1 = ((boxes1[i, 0], boxes1[i, 1]), (boxes1[i, 2], boxes1[i, 3]), boxes1[i, 4]) for j in range(len(boxes2)): r2 = ((boxes2[j, 0], boxes2[j, 1]), (boxes2[j, 2], boxes2[j, 3]), boxes2[j, 4]) int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) ious[i][j] = int_area * 1.0 / (area1[i] + area2[j] - int_area) else: ious[i][j] = 0 return ious
def _rotated_rectangle_intersection_area(s_rect,m_rect,debug=False): r1 = cv2.rotatedRectangleIntersection(s_rect, m_rect) if r1[0] == 0: return 0, None elif r1[0] == 2: return s_rect[1][0]*s_rect[1][1], None x = 0 y = 0 p = [] len_p = r1[1].shape[0] for i in range(len_p): p.append(Point(r1[1][i][0][0], r1[1][i][0][1])) x += r1[1][i][0][0] y += r1[1][i][0][1] c = Point(x / len_p, y / len_p) # if debug: # print('source:{}'.format(''.join(map(str,p)))) pp = sorted(p, key=functools.cmp_to_key(lambda x, y: cmp(x, y, c))) # if debug: # print('sorted:{}'.format(''.join(map(str,pp)))) r = np.full((len_p, 2), 0.0, dtype='float32') for i in range(len(pp)): r[i][0] = pp[i].x r[i][1] = pp[i].y r2 = cv2.contourArea(r) return (r2,r)
def is_object_occluded(obj: ClusteredObject, visible_objects): #return False #Does not work / unreliable! #For each occluder in fov and closer: # occlusion_area=0.0 for occluder in visible_objects: if obj == occluder: continue if not obj.mindist_i - occluder.maxdist_i > 2 and occluder.maxdist_i > 0: #OPTION: distance difference continue #get the rotated-rect intersection intersection_type, intersection_points = cv2.rotatedRectangleIntersection( occluder.rect_i, obj.rect_i) # #if intersect -> add area (naive approach) # if intersection_points is not None: # intersection_rect=cv2.minAreaRect(intersection_points) # occlusion_area+=np.prod(intersection_rect[1]) #Check for a single occluder that covers enough area (naive) if intersection_points is not None: intersection_rect = cv2.minAreaRect(intersection_points) if np.prod(intersection_rect[1] ) > 0.8 * obj.get_area(): #Option: occlusion area return True return False
def cv2_rbbx_overlaps(boxes, query_boxes): ''' Parameters ---------------- boxes: (N, 5) --- x_ctr, y_ctr, height, width, angle query: (K, 5) --- x_ctr, y_ctr, height, width, angle ---------------- Returns ---------------- Overlaps (N, K) IoU ''' N = boxes.shape[0] K = query_boxes.shape[0] overlaps = np.zeros((N, K), dtype=np.float32) for k in range(K): query_area = query_boxes[k, 2] * query_boxes[k, 3] for n in range(N): box_area = boxes[n, 2] * boxes[n, 3] #IoU of rotated rectangle #loading data anti to clock-wise rn = ((boxes[n, 0], boxes[n, 1]), (boxes[n, 2], boxes[n, 3]), -boxes[n, 4]) rk = ((query_boxes[k, 0], query_boxes[k, 1]), (query_boxes[k, 2], query_boxes[k, 3]), -query_boxes[k, 4]) int_pts = cv2.rotatedRectangleIntersection(rk, rn)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) overlaps[n, k] = int_area * 1.0 / (query_area + box_area - int_area) return overlaps
def __call__(self, img, anno=None): ih, iw = img.shape[:2] polygons = [] if anno: for bbox in anno['bboxes']: x, y, w, h, a = xy42xywha(bbox) polygons.append(((x, y), (w, h), a)) for count in range(self.max_try): if isinstance(self.size, int): nh = nw = min(ih, iw, self.size) else: if self.max_aspect == 1: nh = nw = random.randint(min(ih, iw, self.size[0]), min(ih, iw, self.size[1])) else: nh = random.randint(min(ih, self.size[0]), min(ih, self.size[1])) nw = random.randint(min(iw, self.size[0]), min(iw, self.size[1])) if max(nh / nw, nw / nh) > self.max_aspect: continue oh = random.randint(0, ih - nh) ow = random.randint(0, iw - nw) a = np.random.uniform(0, 360) src = xywha2xy4([ow + nw / 2, oh + nh / 2, nw, nh, a]) dst = np.array([[0, 0], [nw, 0], [nw, nh]], dtype=np.float32) m = cv.getAffineTransform(src.astype(np.float32)[:3], dst) if anno: bound = (ow + nw / 2, oh + nh / 2), (nw, nh), a iou, intersections = [], [] for polygon in polygons: inter_points = cv.rotatedRectangleIntersection(bound, polygon)[1] if inter_points is None: iou.append(0) intersections.append(None) else: order_points = cv.convexHull(inter_points, returnPoints=True) inter_area = cv.contourArea(order_points) iou.append(inter_area / (polygon[1][0] * polygon[1][1])) intersections.append(cv.boxPoints(cv.minAreaRect(order_points))) iou = np.array(iou) if isinstance(self.iou_thresh, float): mask = iou >= self.iou_thresh else: mask = (iou > self.iou_thresh[0]) & (iou < self.iou_thresh[1]) if np.any(mask): continue mask = iou >= self.iou_thresh[1] if np.any(mask): bboxes = np.array([inter for inter, m in zip(intersections, mask) if m]) bboxes = np.concatenate([bboxes, np.ones_like(bboxes[:, :, [0]])], axis=-1) bboxes = np.matmul(m, bboxes.transpose([0, 2, 1])).transpose([0, 2, 1]) anno['bboxes'] = bboxes anno['labels'] = anno['labels'][mask] else: if self.nonempty: continue else: anno = None img = cv.warpAffine(img, m, (nw, nh)) break return img, anno
def nms_rotate_cpu(boxes, iou_threshold): keep = [] scores = boxes[:, -1] order = scores.argsort()[::-1] num = boxes.shape[0] suppressed = np.zeros((num), dtype=np.int) for _i in range(num): i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((boxes[i, 0], boxes[i, 1]), (boxes[i, 2], boxes[i, 3]), boxes[i, 4]) area_r1 = boxes[i, 2] * boxes[i, 3] for _j in range(_i + 1, num): j = order[_j] if suppressed[i] == 1: continue r2 = ((boxes[j, 0], boxes[j, 1]), (boxes[j, 2], boxes[j, 3]), boxes[j, 4]) area_r2 = boxes[j, 2] * boxes[j, 3] inter = 0.0 inter_iof = 0.0 distance = np.sqrt((r1[0][0] - r2[0][0])**2 + (r1[0][1] - r2[0][1])**2) try: int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + 0.00001) inter_iof = int_area * 1.0 / (area_r2 + 0.00001) except: """ cv2.error: /io/opencv/modules/imgproc/src/intersection.cpp:247: error: (-215) intersection.size() <= 8 in function rotatedRectangleIntersection """ # print(r1) # print(r2) inter = 0.9999 if inter >= iou_threshold or inter_iof >= 0.8 or distance <= min( r1[1][0], r1[1][1]): suppressed[j] = 1 return np.array(keep, np.int64)
def cal_box_share(box, query_box): rn = ((box[0], box[1]), (box[3], box[2]), -box[4]) rk = ((query_box[0], query_box[1]), (query_box[3], query_box[2]), -query_box[4]) int_pts = cv2.rotatedRectangleIntersection(rk, rn)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints = True) int_area = cv2.contourArea(order_pts) return int_area return 0
def ratio(self, rect): # 求两个box重合度 inter_pts = cv2.rotatedRectangleIntersection(self.rect, rect)[1] if inter_pts is None: return 0, None inter_area = cv2.contourArea(inter_pts) inter_ratio = inter_area / (rect[1][0] * rect[1][1]) contex = cv2.convexHull(inter_pts) return (inter_ratio, contex)
def compute_rotate_inter(r1, r2): rr1 = ((r1[0], r1[1]), (r1[2], r1[3]), r1[4]) rr2 = ((r2[0], r2[1]), (r2[2], r2[3]), r2[4]) int_pts = cv2.rotatedRectangleIntersection(rr1, rr2)[1] inter = 0.0 if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) inter = cv2.contourArea(order_pts) return inter
def rnms(self, boxes, iou_threshold): keep = [] order = boxes[:, -1].argsort()[::-1] num = boxes.shape[0] suppressed = np.zeros((num), dtype=np.int) for _i in range(num): # if len(keep) >= max_output_size: # break i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((boxes[i, 0], boxes[i, 1]), (boxes[i, 2], boxes[i, 3]), boxes[i, 4]) area_r1 = boxes[i, 2] * boxes[i, 3] for _j in range(_i + 1, num): j = order[_j] if suppressed[i] == 1: continue r2 = ((boxes[j, 0], boxes[j, 1]), (boxes[j, 2], boxes[j, 3]), boxes[j, 4]) area_r2 = boxes[j, 2] * boxes[j, 3] inter = 0.0 try: int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + 1e-5) except: """ cv2.error: /io/opencv/modules/imgproc/src/intersection.cpp:247: error: (-215) intersection.size() <= 8 in function rotatedRectangleIntersection """ # print(r1) # print(r2) inter = 0.9999 if inter >= iou_threshold: suppressed[j] = 1 return np.array(keep, np.int64)
def _judge_Intersection(self): b = ((self._center_x + self._new_pos_x, self._center_y + self._new_pos_y), (self._w, self._h), self._angle * 180 / np.pi) for i in range(len(self._rotate_box_collection)): res = cv2.rotatedRectangleIntersection(self._rotate_box_collection[i], b) if res[0] != 0: # print('overlap') return 1 return 0
def rotation_nms(rboxes, scores, iou_threshold=0.5): """rotation non-maximum suppression (NMS) on the boxes according to their intersection-over-union (IoU) Arguments: rboxes {np.array} -- [N * 5] (cx, cy, w, h, theta (rad/s)) scores {np.array} -- [N * 1] iou_threshold {float} -- threshold for IoU """ cx = rboxes[:, 0] cy = rboxes[:, 1] w = rboxes[:, 2] h = rboxes[:, 3] theta = rboxes[:, 4] * 180.0 / np.pi order = scores.argsort()[::-1] areas = w * h keep = [] while order.size > 0: best_rbox_idx = order[0] keep.append(best_rbox_idx) best_rbbox = np.array([cx[best_rbox_idx], cy[best_rbox_idx], w[best_rbox_idx], h[best_rbox_idx], theta[best_rbox_idx]]) remain_rbboxes = np.hstack((cx[order[1:]].reshape(1, -1).T, cy[order[1:]].reshape(1,-1).T, w[order[1:]].reshape(1,-1).T, h[order[1:]].reshape(1,-1).T, theta[order[1:]].reshape(1,-1).T)) inters = [] for remain_rbbox in remain_rbboxes: rbbox1 = ((best_rbbox[0], best_rbbox[1]), (best_rbbox[2], best_rbbox[3]), best_rbbox[4]) rbbox2 = ((remain_rbbox[0], remain_rbbox[1]), (remain_rbbox[2], remain_rbbox[3]), remain_rbbox[4]) inter = cv2.rotatedRectangleIntersection(rbbox1, rbbox2)[1] if inter is not None: inter_pts = cv2.convexHull(inter, returnPoints=True) inter = cv2.contourArea(inter_pts) inters.append(inter) else: inters.append(0) inters = np.array(inters) iou = inters / (areas[best_rbox_idx] + areas[order[1:]] - inters) inds = np.where(iou <= iou_threshold)[0] order = order[inds + 1] return keep
def nms_rotate_cpu(det, iou_threshold): """ :param boxes: format [x_c, y_c, w, h, theta,scores] :param threshold: iou threshold (0.7 or 0.5) :param max_output_size: max number of output :return: the remaining index of boxes """ keep = [] x_c = det[:, 0] y_c= det[:, 1] w = det[:, 2] h = det[:, 3] theta=det[:,4] scores = det[:, -1] order = scores.argsort()[::-1] #print("order=",order.shape) num = det.shape[0] suppressed = np.zeros((num), dtype=np.int) for _i in range(num): if len(keep) >= 150: break i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((det[i, 0], det[i, 1]), (det[i, 2], det[i, 3]), det[i, 4]) area_r1 = det[i, 2] * det[i, 3] for _j in range(_i + 1, num): #print("j_=",_j) j = order[_j] if suppressed[i] == 1: continue r2 = ((det[j, 0], det[j, 1]), (det[j, 2], det[j, 3]), det[j, 4]) area_r2 = det[j, 2] * det[j, 3] inter = 0.0 int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + 0.00001) #if inter >= iou_threshold: # suppressed[j] = 1 inds=np.where(inter<=iou_threshold)[0] order=order[inds+1] return keep
def inclined_nms(boxes, scores, iou_threshold, max_output_size=2000): """ :param boxes format =[[x, y, w, h, theta], [x, y, w, h, theta], ...,[x, y, w, h, theta]] :param scores format = [score1, score2, score3, ... , scoren] :param iou_threshold format 0.2 ~ 0.4 :return: keep index of the boxes and scores, format[0, 1, 15, 20, ... ,n] """ if not isinstance(boxes, np.ndarray): np.array(boxes, dtype=np.float32) ## 旋转的nms计算 ##输入box是5tuple keep = [] order = scores.argsort()[::-1] num = boxes.shape[0] suppressed = np.zeros((num), dtype=np.int) for _i in range(num): if len(keep) >= max_output_size: break i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((boxes[i, 0], boxes[i, 1]), (boxes[i, 2], boxes[i, 3]), boxes[i, 4]) area_r1 = boxes[i, 2] * boxes[i, 3] for _j in range(_i + 1, num): j = order[_j] if suppressed[i] == 1: continue r2 = ((boxes[j, 0], boxes[j, 1]), (boxes[j, 2], boxes[j, 3]), boxes[j, 4]) area_r2 = boxes[j, 2] * boxes[j, 3] inter = 0.0 int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + 1e-5) if inter >= iou_threshold: suppressed[j] = 1 return np.array(keep, np.int64)
def _jaccard_overlap(self, pred, gt): r1 = ((pred[0], pred[1]), (pred[2], pred[3]), pred[4]) area_r1 = pred[2] * pred[3] r2 = ((gt[0], gt[1]), (gt[2], gt[3]), gt[4]) area_r2 = gt[2] * gt[3] int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) ovr = int_area * 1.0 / (area_r1 + area_r2 - int_area) return ovr else: return 0
def finish(self, vc): # draw resulting path pts = transpose(array(self.trackedPoints, int32)[:, 1:]) pathIm = cv2.polylines(self.firstFrame, [(pts[:, 0:2]).reshape((-1, 1, 2))], False, (255, 150, 0)) cv2.imshow("Trackingview", pathIm) # determine minimum rectangle around start position minRectHalfSize = self.startPosMinRectSize / 2. self.startPosMinRect = ((pts[0, 0], pts[0, 1]), (self.startPosMinRectSize, self.startPosMinRectSize), 90) self.boundingRectOfPath = cv2.minAreaRect(pts[:, 0:2]) resIntersection, intersectionReg = cv2.rotatedRectangleIntersection(self.startPosMinRect, self.boundingRectOfPath) self.robotLeavesStartRect = resIntersection == 1 # both rectangles contain the starting position => rectangles can only intersect or are fully contained # write rectangles screenshot rectsIm = cv2.drawContours(self.firstFrame, [int0(cv2.boxPoints(self.startPosMinRect))], 0, (0,0,255)) rectsIm = cv2.drawContours(rectsIm, [int0(cv2.boxPoints(self.boundingRectOfPath))], 0, (0,255,0)) cv2.imwrite("distance - Rects.png", rectsIm) # work with float arrays: ptsFlt = transpose(array(self.trackedPoints)[:, 1:]) # perform evaluation on recorded path for i in range(0, ptsFlt.shape[0] - 1): distance = linalg.norm(ptsFlt[i + 1, 0:2] - ptsFlt[i, 0:2]) deltaTime = ptsFlt[i + 1, 2] - ptsFlt[i, 2] if distance == 0. or deltaTime == 0.: self.speedBtwnPoints.append(0.) else: self.speedBtwnPoints.append(distance / deltaTime) self.totalDistance += distance #endFor loop over tracked path self.averageSpeed = self.totalDistance / (ptsFlt[-1, 2] - ptsFlt[0,2]) # calculate inverse reward here self.invTotalDistance_LeavingRect -= self.totalDistance if not self.robotLeavesStartRect: self.invTotalDistance_LeavingRect += 100 * self.trackingTime # punishment vc.release() cv2.destroyWindow("Trackingview")
def nms_rotate_cpu(boxes, scores, iou_threshold, max_output_size): keep = [] order = scores.argsort()[::-1] num = boxes.shape[0] suppressed = np.zeros((num), dtype=np.int) for _i in range(num): if len(keep) >= max_output_size: break i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((boxes[i, 0], boxes[i, 1]), (boxes[i, 2], boxes[i, 3]), boxes[i, 4]) area_r1 = boxes[i, 2] * boxes[i, 3] for _j in range(_i + 1, num): j = order[_j] if suppressed[i] == 1: continue r2 = ((boxes[j, 0], boxes[j, 1]), (boxes[j, 2], boxes[j, 3]), boxes[j, 4]) area_r2 = boxes[j, 2] * boxes[j, 3] inter = 0.0 int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if int_pts is not None: order_pts = cv2.convexHull(int_pts, returnPoints=True) int_area = cv2.contourArea(order_pts) inter = int_area * 1.0 / (area_r1 + area_r2 - int_area + cfgs.EPSILON) if inter >= iou_threshold: suppressed[j] = 1 return np.array(keep, np.int64)
def rbbx_overlaps(boxes, query_boxes): ''' Parameters ---------------- boxes: (N, 5) --- x_ctr, y_ctr, height, width, angle query: (K, 5) --- x_ctr, y_ctr, height, width, angle ---------------- Returns ---------------- Overlaps (N, K) IoU ''' N = boxes.shape[0] K = query_boxes.shape[0] overlaps = np.zeros((N, K), dtype = np.float32) for k in range(K): query_area = query_boxes[k, 2] * query_boxes[k, 3] for n in range(N): box_area = boxes[n, 2] * boxes[n, 3] #IoU of rotated rectangle #loading data anti to clock-wise rn = ((boxes[n, 0], boxes[n, 1]), (boxes[n, 3], boxes[n, 2]), -boxes[n, 4]) rk = ((query_boxes[k, 0], query_boxes[k, 1]), (query_boxes[k, 3], query_boxes[k, 2]), -query_boxes[k, 4]) int_pts = cv2.rotatedRectangleIntersection(rk, rn)[1] #print type(int_pts) if None != int_pts: order_pts = cv2.convexHull(int_pts, returnPoints = True) int_area = cv2.contourArea(order_pts) overlaps[n, k] = int_area * 1.0 / (query_area + box_area - int_area) return overlaps
def rotate_cpu_nms(dets, threshold): ''' Parameters ---------------- dets: (N, 6) --- x_ctr, y_ctr, height, width, angle, score threshold: 0.7 or 0.5 IoU ---------------- Returns ---------------- keep: keep the remaining index of dets ''' keep = [] scores = dets[:, -1] tic = time.time() order = scores.argsort()[::-1] ndets = dets.shape[0] print "nms start" print ndets suppressed = np.zeros((ndets), dtype = np.int) for _i in range(ndets): i = order[_i] if suppressed[i] == 1: continue keep.append(i) r1 = ((dets[i,0],dets[i,1]),(dets[i,3],dets[i,2]),dets[i,4]) area_r1 = dets[i,2]*dets[i,3] for _j in range(_i+1,ndets): #tic = time.time() j = order[_j] if suppressed[j] == 1: continue r2 = ((dets[j,0],dets[j,1]),(dets[j,3],dets[j,2]),dets[j,4]) area_r2 = dets[j,2]*dets[j,3] ovr = 0.0 #+++ #d = math.sqrt((dets[i,0] - dets[j,0])**2 + (dets[i,1] - dets[j,1])**2) #d1 = math.sqrt(dets[i,2]**2 + dets[i,3]**2) #d2 = math.sqrt(dets[j,2]**2 + dets[j,3]**2) #if d<d1+d2: #+++ int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1] if None != int_pts: order_pts = cv2.convexHull(int_pts, returnPoints = True) #t2 = time.time() int_area = cv2.contourArea(order_pts) #t3 = time.time() ovr = int_area*1.0/(area_r1+area_r2-int_area) if ovr>=threshold: suppressed[j]=1 #print t1 - tic, t2 - t1, t3 - t2 #print print time.time() - tic print "nms done" return keep
def nadji_konture_determinante(pogodne_konture, frame_bin, frame, prolaz): potencijalne_granice_determinante = [] rbr = 0 for contour in pogodne_konture: rotated_contour = rotate_contour(contour, frame_bin) rbr = rbr+1 x,y,w,h = cv2.boundingRect(rotated_contour) #koordinate i velicina granicnog pravougaonika #area = cv2.contourArea(contour) rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(frame, [box], 0, (0,0,255), 2) rect = cv2.minAreaRect(rotated_contour) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(frame, [box], 0, (0,255,255), 2) if (h/w > 5) and h>30 and w>5: potencijalne_granice_determinante.append(contour) rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(frame, [box], 0, (0,255,0), 2) print "Broj potencijalnih granica pre mergovanja = ", len(potencijalne_granice_determinante) potencijalne_granice_determinante = merge_regions(potencijalne_granice_determinante) potencijalne_granice_determinante = merge_intersected(potencijalne_granice_determinante) print "Broj potencijalnih granica nakon mergovanja = ", len(potencijalne_granice_determinante) if len(potencijalne_granice_determinante) != 2: return # NALAZENJE PRAVOUGAONIKA KOJI PREDSTALJVA REGION DETERMINANTE granica_determinante = np.ndarray((len(potencijalne_granice_determinante[0])+len(potencijalne_granice_determinante[1]), 1,2), dtype=np.int32) granica_determinante = np.concatenate((potencijalne_granice_determinante[0], potencijalne_granice_determinante[1])) rectDet = cv2.minAreaRect(granica_determinante) boxDet = cv2.boxPoints(rectDet) boxDet = np.int0(boxDet) cv2.drawContours(frame, [boxDet], 0, (255,0,0), 2) konture_determinante = [] konture_unutar_determinante = [] rotirane_konture_mapa_po_x = {} for contour in pogodne_konture: #rotated_contour = rotate_contour(contour, frame_bin) #x,y,w,h = cv2.boundingRect(rotated_contour) #koordinate i velicina granicnog pravougaonika #area = cv2.contourArea(contour) rectCont = cv2.minAreaRect(contour) centerCont, sizeCont, angleCont = rectCont x,y = centerCont indicator, vertices = cv2.rotatedRectangleIntersection(rectDet, rectCont) print "Indikator presecnosti: ", indicator if indicator>0: konture_determinante.append(contour) if indicator == 2: konture_unutar_determinante.append(contour) rotirane_konture_mapa_po_x[x] = contour sortirane_rotirane_mapa_po_x = collections.OrderedDict(sorted(rotirane_konture_mapa_po_x.items())) sortirane_rotirane_konture = np.array(sortirane_rotirane_mapa_po_x.values()) iscrtajIzKontureSliku(sortirane_rotirane_konture, frame_bin, prolaz) print "Prepoznate konture determinante: ", len(konture_determinante) print "Prepoznate sortirane konture unutar determinante: ", len(sortirane_rotirane_konture) # Rotiranje kontura unutar determinante oko centra determinante centerDet, sizeDet, angleDet = cv2.minAreaRect(granica_determinante) print "Centar determinante je u : ", centerDet rotirane_konture = [] distances_x = [] distances_y = [] index = 0 for contour in sortirane_rotirane_konture: xt,yt,h,w = cv2.boundingRect(contour) rectCont = cv2.minAreaRect(contour) centerCont, sizeCont, angleCont = rectCont ccx, ccy = centerCont print "Centar konture broj", index, " : X=", ccx," Y=", ccy region_points = [] for i in range (xt,xt+h): for j in range(yt,yt+w): dist = cv2.pointPolygonTest(contour,(i,j),False) if dist>=0 and frame_bin[j,i]==255: # da li se tacka nalazi unutar konture? region_points.append([i,j]) cdx,cdy = centerDet # height, width = sizeCont # if width<height: # angle+=90 # Rotiranje svake tačke regiona oko centra rotacije alpha = np.pi/2 - abs(np.radians(angleDet)) region_points_rotated = np.ndarray((len(region_points), 1,2), dtype=np.int32) for i, point in enumerate(region_points): x = point[0] y = point[1] #TODO 1 - izračunati koordinate tačke nakon rotacije rx = np.sin(alpha)*(x-cdx) - np.cos(alpha)*(y-cdy) + cdx ry = np.cos(alpha)*(x-cdx) + np.sin(alpha)*(y-cdy) + cdy region_points_rotated[i] = [rx,ry] rectRotatedCont = cv2.minAreaRect(region_points_rotated) centerRotatedCont, sizeRotatedCont, angleRotatedCont = rectRotatedCont crcx, crcy = centerRotatedCont print "Rotirana oko centra determinante : X=", crcx," Y=", crcy distances_x.append(ccx-cdx) distances_y.append(ccy-cdy) boxRotatedCont = cv2.boxPoints(rectRotatedCont) boxRotatedCont = np.int0(boxRotatedCont) cv2.drawContours(frame, [boxRotatedCont], 0, (255,255,255), 2) rotirane_konture.append(region_points_rotated) rotirane_konture_mapa_po_x[int(ccx)] = region_points_rotated index += 1 #sortirane_rotirane_mapa_po_x = collections.OrderedDict(sorted(rotirane_konture_mapa_po_x.items())) #sortirane_rotirane_konture = np.array(sortirane_rotirane_mapa_po_x.values()) print "Broj kontura u determinanti = ", len(konture_unutar_determinante), " sortiranih treba isto? = ", len(sortirane_rotirane_konture) # KMeans razdvajanje brojeva po pripadnosti vrsti i koloni n_clusters = int(round(math.sqrt(len(konture_unutar_determinante)), 0)) print "Dimenzija determinante ", n_clusters, "x", n_clusters # KMeans za razdvajanje vrsta print "Distance po x od centra determinante : ", distances_x k_means_x = KMeans(n_clusters=n_clusters, max_iter=1000) distances_x = np.array(distances_x).reshape(len(distances_x), 1) k_means_x.fit(distances_x) # KMeans za razdvajanje kolona print "Distance po y od centra determinante : ", distances_y k_means_y = KMeans(n_clusters=n_clusters, max_iter=1000) distances_y = np.array(distances_y).reshape(len(distances_y), 1) k_means_y.fit(distances_y) regioni = smestiElementeUMatricu(sortirane_rotirane_konture, k_means_x, k_means_y, frame_bin, prolaz) return np.array(regioni, np.float32)
def Evaluate(self): "Function to evaluate the tracking log" # check if enough points tracked for valid evaluation: self.logger.info("Tracking:Evaluate: Evaluation of tracking cycle started") validEvaluation = True if len(self.trackedPoints[0]) > self.trackingTime * SettingsPC.MinimumTrackingRate else False if not validEvaluation: self.logger.warning("Tracking:Evaluate: Not enough tracked points {0} w.r.t. minimum {1}, with trackingTime {2}s and tracking rate {3}".format( len(self.trackedPoints[0]), self.trackingTime * SettingsPC.MinimumTrackingRate, self.trackingTime, SettingsPC.MinimumTrackingRate)) # tracking needs to have captured at least 1 point before being able to calculate something if len(self.trackedPoints[0]) < 2 or len(self.trackedPoints[1]) < 2 or len(self.trackedPoints[2]) < 2: self.logger.warning("Tracking:Evaluate: Not a single position captured!") self.trackingPropertiesLock.acquire() TrackingProperties.TrackingFailed = True self.trackingPropertiesLock.release() return False # draw resulting path pts = transpose(array(self.trackedPoints, int32)[:, 1:]) pathIm = cv2.polylines(self.firstFrame, [(pts[:, 0:2]).reshape((-1, 1, 2))], False, (255, 150, 0)) cv2.imshow("Trackingview", pathIm) # save screenshot self.screenshotOutputFilename = "{0}-TrackPath-Ref_{1}--Rec_{2}{3}{4}.png".format(self.screenshotOutputFilename, datetime.datetime.fromtimestamp(self.referenceTimestamp).strftime("%Y-%m-%d %H-%M-%S"), time.strftime("%Y-%m-%d %H-%M-%S"), "-TESTMODE" if self.testMode else "", "-Eval_FAILED" if not validEvaluation else "") cv2.imwrite(self.screenshotOutputFilename, pathIm) # determine minimum rectangle around start position minRectHalfSize = self.startPosMinRectSize / 2. self.startPosMinRect = ((pts[0, 0], pts[0, 1]), (self.startPosMinRectSize, self.startPosMinRectSize), 90) #startPosMinRectBox = array([[pts[0, 0] - minRectHalfSize, pts[1, 0] - minRectHalfSize], [pts[0, 0] - minRectHalfSize, pts[1, 0] + minRectHalfSize], [pts[0, 0] + minRectHalfSize, pts[1, 0] + minRectHalfSize], #[pts[0, 0] + minRectHalfSize, pts[1, 0] - minRectHalfSize]], int32) self.boundingRectOfPath = cv2.minAreaRect(pts[:, 0:2]) #resIntersection, intersectionReg = cv2.rotatedRectangleIntersection(self.startPosMinRect, self.boundingRectOfPath) resIntersection, intersectionReg = cv2.rotatedRectangleIntersection(self.startPosMinRect, self.boundingRectOfPath) #print "resIntersection = {0}".format(resIntersection) self.robotLeavesStartRect = resIntersection == 1 # both rectangles contain the starting position => rectangles can only intersect or are fully contained # write rectangles screenshot rectsIm = cv2.drawContours(self.firstFrame, [int0(cv2.boxPoints(self.startPosMinRect))], 0, (0,0,255)) rectsIm = cv2.drawContours(rectsIm, [int0(cv2.boxPoints(self.boundingRectOfPath))], 0, (0,255,0)) cv2.imwrite(self.screenshotOutputFilename[:-4] + "--Rects.png", rectsIm) # work with float arrays: ptsFlt = transpose(array(self.trackedPoints)[:, 1:]) # perform evaluation on recorded path for i in range(0, ptsFlt.shape[0] - 1): distance = linalg.norm(ptsFlt[i + 1, 0:2] - ptsFlt[i, 0:2]) deltaTime = ptsFlt[i + 1, 2] - ptsFlt[i, 2] if distance == 0. or deltaTime == 0.: self.speedBtwnPoints.append(0.) else: self.speedBtwnPoints.append(distance / deltaTime) self.totalDistance += distance #endFor loop over tracked path self.averageSpeed = self.totalDistance / (ptsFlt[-1, 2] - ptsFlt[0,2]) # calculate inverse reward here self.invTotalDistance_LeavingRect -= self.totalDistance if not self.robotLeavesStartRect: self.invTotalDistance_LeavingRect += 100 * self.trackingTime # punishment # return also evaluation results in trackingProperties self.resultTracking["TotalDistance"] = self.totalDistance self.resultTracking["AverageSpeed"] = self.averageSpeed self.resultTracking["RobotLeavesStartRect"] = self.robotLeavesStartRect self.resultTracking["invTotalDistance_LeavingRect"] = self.invTotalDistance_LeavingRect self.trackingPropertiesLock.acquire() TrackingProperties.ResultTracking = self.resultTracking TrackingProperties.ResultReferenceTimeStamp = self.referenceTimestamp TrackingProperties.TrackingFailed = not validEvaluation self.trackingPropertiesLock.release() return validEvaluation
def check_intersection_from_rect(self, rect1, rect2): #print(rect1, rect2) ret, _ = cv2.rotatedRectangleIntersection(rect1, rect2) return ret != cv2.INTERSECT_NONE