def delete_one_edge(self, contour, inliner_idx, d, p): ''' 从contour中删除和第p个未被分配的点在同一条直线上的点,点到直线的最大距离为d @param contour: 轮廓 @param inliner_idx: 当前点已经完成的分配 @param d: 最大偏差 @param p: 中心点的位置 ''' assert isinstance(contour, np.ndarray) counter = 0 center = 0 # when the contour is shorter than p, center will be 0 for k, v in enumerate(inliner_idx): counter = counter + 1 if v == -1 else counter if counter == p: center = k break # TODO: determine Region of Support (ROS) points = get_elements_in_window(contour, center, 2) # get line in format (vx, vy, x0, y0) line = cv2.fitLine(np.asarray(points, dtype=np.float32), cv2.cv.CV_DIST_L2, 0, 0.01, 0.01) vx = line[0] vy = line[1] n = np.asarray([vx, vy]) n = np.squeeze(n) a = np.asarray([line[2], line[3]]) a = np.squeeze(a) self.set_adjacent_inliner(contour, inliner_idx, center, n, a, d) return inliner_idx
def filter_indexes(self, indexes, w=1): ''' 使用类似中值滤波的技术,对轮廓进行去噪 @param indexes: 轮廓 @param w: 窗口的大小 ''' assert isinstance(indexes, list) indexes_ret = [] n = len(indexes) for i in xrange(n): indexes_ret.append(np.median(get_elements_in_window(indexes, i, w))) return indexes_ret
def filter_indexes(self, indexes, w=1): ''' 使用类似中值滤波的技术,对轮廓进行去噪 @param indexes: 轮廓 @param w: 窗口的大小 ''' assert isinstance(indexes, list) indexes_ret = [] n = len(indexes) for i in xrange(n): indexes_ret.append( np.median(get_elements_in_window(indexes, i, w)) ) return indexes_ret
def test_indexes_in_window_case4(self): indexes = [2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 1] ret_indexes = get_elements_in_window(indexes, 18, 1) expected_indexes = np.array([2, 2, 1], dtype=np.float32) self.assertItemsEqual(ret_indexes, expected_indexes)
def test_indexes_in_window_case3(self): indexes = [2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 1] ret_indexes = get_elements_in_window(indexes, 2, 2) expected_indexes = [2, 2, 2, 0, 0] self.assertItemsEqual(ret_indexes, expected_indexes)