示例#1
0
def main_6_video():
    video, calibration_image = get_capture_and_calibration_image_video6()
    calibrator = Calibrator.calibrate(calibration_image, 2)
    visualize_calibration(calibrator, calibration_image)
    if not calibrator.calibrated:
        print('System was not calibrated.')
        return

    detector = Detector(calibrator)
    wnd = CvNamedWindow('detection', cv2.WINDOW_NORMAL)

    video.set_pos(961)
    ellipses_count = 0
    for frame in video.frames():
        pos = video.frame_pos()
        print(pos)
        t, ellipses = detect_and_draw(detector, frame)
        ellipses_count = max(ellipses_count, len(ellipses))
        utils.put_frame_pos(frame, pos)
        put_video_duration(frame, video.frame_pos_msec())
        put_ellipses_count(frame, ellipses_count)
        wnd.imshow(frame)
        cv2.waitKey()
    cv2.waitKey()

    video.release()
    cv2.destroyAllWindows()
示例#2
0
def main():
    from skimage.feature import peak_local_max
    from skimage.morphology import watershed
    import scipy.ndimage as ndi

    img = realImage()
    # img = testImage()
    img = fillHoles(img)

    thresh = img.copy()

    with utils.timeit_context():
        dst = ndi.distance_transform_edt(img)
        localMax = peak_local_max(dst, indices=False, min_distance=1, labels=thresh)
        markers = ndi.label(localMax)[0]
        labels = watershed(-dst, markers, mask=thresh)

    segmImg = (labels * (255 / labels.max())).astype(np.uint8)

    wnd = CvNamedWindow(flags=cv2.WINDOW_NORMAL)
    segmWnd = CvNamedWindow('segm', flags=cv2.WINDOW_NORMAL)

    wnd.imshow(img)
    segmWnd.imshow(segmImg)

    cvWaitKeys()
示例#3
0
def main():
    # img = testImage()
    img = realImage()
    n = 1
    sz = n * 2 + 1  # 3, 5 ...
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sz, sz))
    iterations = 1
    mode = None  # erode dilate
    wnd = CvNamedWindow()

    def images():
        while True:
            if mode == 'dilate':
                cv2.dilate(img, kernel, dst=img, iterations=iterations)
            elif mode == 'erode':
                cv2.erode(img, kernel, dst=img, iterations=iterations)

            yield img

    for im in images():
        wnd.imshow(im)
        key = cvWaitKeys(27, '1', '2')
        if key == 27:
            break
        elif key == ord('1'):
            mode = 'erode'
        elif key == ord('2'):
            mode = 'dilate'
示例#4
0
def main():
    wnd = CvNamedWindow('ORB', flags=cv2.WINDOW_NORMAL)
    vc = VideoController(delay=-1)
    detector = cv2.ORB_create(nfeatures=1000)
    # frames = rect_frames_sequence((200, 200), (40, 50), (5, 5), 50, step=2)
    frames = ellipse_frames_sequence((200, 200), (65, 75), (40, 50),
                                     23,
                                     steps=30,
                                     step=2)
    frame0 = next(frames)
    keypoints, descrs = detector.detectAndCompute(frame0, None)

    pts0 = np.reshape([key_pt.pt for key_pt in keypoints],
                      (-1, 1, 2)).astype(np.float32)
    wnd.imshow(draw_points(frame0.copy(), pts0))
    vc.wait_key()

    lk_params = dict(winSize=(15, 15),
                     maxLevel=2,
                     criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                               10, 0.03))
    for frame in frames:
        pts1, st, err = cv2.calcOpticalFlowPyrLK(frame0, frame, pts0, None,
                                                 **lk_params)
        pts1 = pts1[st == 1]

        wnd.imshow(draw_points(frame.copy(), pts1))
        pts0 = pts1.reshape(-1, 1, 2)
        frame0 = frame
        if vc.wait_key() == 27:
            break
示例#5
0
def visualize(contours, image, winname):
    draw_contours(contours, image, (0, 255, 0), thickness=2, draw_measurements=False)

    wnd = CvNamedWindow(winname, cv2.WINDOW_NORMAL)
    wnd.imshow(image)
    cv2.waitKey()
    wnd.destroy()
示例#6
0
    def VIS_assemble_ellipses_from_parts(bgr, part, next_part, ellipses):
        if bgr.shape[0] > 300 or bgr.shape[1] > 300:
            return
        green = (0, 255, 0)
        red = (0, 0, 255)
        blue = (255, 0, 0)

        im = bgr.copy()

        cv2.polylines(im, [part.points], False, green, 2)
        x, y = part.first_pt
        im[y - 3:y + 3, x - 3:x + 3] = [0, 255, 0]
        x, y = part.last_pt
        im[y - 3:y + 3, x - 3:x + 3] = [0, 127, 0]

        if next_part:
            cv2.polylines(im, [next_part.points], False, red, 2)
            x, y = next_part.first_pt
            im[y - 3:y + 3, x - 3:x + 3] = [0, 0, 255]
            x, y = next_part.last_pt
            im[y - 3:y + 3, x - 3:x + 3] = [0, 0, 127]

        for poly in ellipses:
            cv2.polylines(im, [poly.points], False, blue, 2)
        wnd = CvNamedWindow('DEBUG')
        wnd.imshow(im)
        cv2.waitKey()
        wnd.destroy()
示例#7
0
    def walk_points_sequence(points, is_closed=False, point_action=None):
        point_action = point_action or (lambda i, pt: print(f'{i}, {pt}'))

        max_x = points[..., 0].max() + 20
        max_y = points[..., 1].max() + 20

        img = np.zeros((max_y, max_x), dtype=np.uint8)
        cv2.polylines(img, [points], is_closed, 255)

        # show_goodFeaturesToTrack(img)

        cv2.circle(img, tuple(points[0, 0]), 3, 255, 1)
        cv2.circle(img, tuple(points[-1, 0]), 3, 127, -1)

        wnd = CvNamedWindow(flags=cv2.WINDOW_NORMAL)
        wnd.imshow(img)
        if cv2.waitKey() == 27:
            return

        for i, pt in enumerate(points):
            point_action(i, pt)
            im = img.copy()
            cv2.circle(im, tuple(pt[0]), 2, 255, 1)
            wnd.imshow(im)
            if cv2.waitKey() == 27:
                return
示例#8
0
 def VIS_EDGES(edges):
     if DEBUG.should_not_vis(edges):
         return
     wnd = CvNamedWindow('DEBUG EDGES')
     wnd.imshow(edges)
     cv2.waitKey()
     wnd.destroy()
示例#9
0
    def VIS_POLYGONS(img, polygons):
        if img.shape[0] > 300 or img.shape[1] > 300:
            return
        print('VIS_POLYGONS: polygons count:', len(polygons),
              [p.len for p in polygons])
        if len(polygons) == 0:
            return

        tails_colors = ((0, 127, 0), (0, 255, 0))
        points_color = (0, 0, 0)
        separator = DEBUG.separator(img)
        images = []

        im = DEBUG.__draw_polygons(img.copy(), polygons, (0, 255, 0), 1,
                                   tails_colors, points_color)
        images.extend([im, separator])

        if len(polygons) > 1:
            for p in polygons:
                im = DEBUG.__draw_polygons(img.copy(), [p], (0, 255, 0), 1,
                                           tails_colors, points_color)
                images.extend([im, separator])

        wnd = CvNamedWindow('polygons', cv2.WINDOW_NORMAL)
        wnd.imshow(np.hstack(images))
        cv2.waitKey()
        wnd.destroy()
示例#10
0
    def VIS_ALL_CONTOURS(img, contours):
        if img.shape[0] > 300 or img.shape[1] > 300:
            return

        print('contours LEN:', len(contours))
        if len(contours) == 0:
            return
        separator = DEBUG.separator(img)
        images = []
        tails_colors = ((0, 127, 0), (0, 255, 0))
        points_color = (0, 0, 0)

        im = DEBUG.__draw_contours(img.copy(), contours, (0, 255, 0), 1,
                                   tails_colors, points_color)
        images.extend([im, separator])

        if len(contours) > 1:
            for c in contours:
                im = DEBUG.__draw_contours(img.copy(), [c], (0, 255, 0), 1,
                                           tails_colors, points_color)
                images.extend([im, separator])
                print(f'VIS_ALL_CONTOURS: contour len {c.len()}')

        wnd = CvNamedWindow('contours', cv2.WINDOW_NORMAL)
        wnd.imshow(np.hstack(images))
        cv2.waitKey()
        wnd.destroy()
示例#11
0
def main():
    wnd = CvNamedWindow('frames', flags=cv2.WINDOW_NORMAL)
    vc = VideoController(delay=-1)

    # frames = rect_frames_sequence((200, 200), (40, 50), (5, 5), 50, step=2)
    frames = ellipse_frames_sequence((200, 200), (45, 55), (40, 50),
                                     23,
                                     steps=30,
                                     step=2)
    frame0 = next(frames)

    feature_params = dict(maxCorners=100,
                          qualityLevel=0.3,
                          minDistance=7,
                          blockSize=7)
    pts0 = cv2.goodFeaturesToTrack(frame0, mask=None, **feature_params)
    # TODO: add point at center of rect
    # additional_pts = (
    #     [[5 + 40 / 2, 5 + 50 / 2]],
    #     [[4, 4]],
    #     [[3, 3]],
    #     [[5, 5]],
    #     [[2, 2]],
    #     [[0, 0]],
    #     [[-1, -1]],
    #     #-------------
    #     [[47, 57]],
    #     [[49, 59]],
    #     #-------------
    #     [[4, 6]],
    #     [[4, 8]],
    #     [[4, 9]],
    #     [[4, 11]],
    #     [[4, 12]],
    #     [[4, 13]],
    # )
    # pts0 = np.append(pts0, additional_pts, axis=0).astype(np.float32)

    if pts0 is None:
        print('No good features to track')
        return

    wnd.imshow(draw_points(frame0.copy(), pts0))
    vc.wait_key()

    lk_params = dict(winSize=(15, 15),
                     maxLevel=2,
                     criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                               10, 0.03))
    for frame in frames:
        pts1, st, err = cv2.calcOpticalFlowPyrLK(frame0, frame, pts0, None,
                                                 **lk_params)
        pts1 = pts1[st == 1]

        wnd.imshow(draw_points(frame.copy(), pts1))
        pts0 = pts1.reshape(-1, 1, 2)
        frame0 = frame
        if vc.wait_key() == 27:
            break
示例#12
0
def main():
    video = VideoCapture(video_sources.video_2)
    workArea = WorkAreaView(video_sources.video_2_work_area_markers)

    vc = VideoController(10, 'pause')
    (video_wnd, bin_diff_wnd, gray_diff_wnd, colorDiffWnd,
     learned_BG_wnd) = Wnd.create('video', 'binary diff', 'gray diff',
                                  'color diff', 'Learned BG')
    colorAbsDiffWnd = Wnd('color_abs_diff')
    segmentedWnd = Wnd('segmented')

    segmenter = Segmenter()

    frames_iter = workArea.skip_non_area(video.frames())

    motionDetector = MotionDetector(next(frames_iter)[0], 3)
    backgroundModel = BackgroundModel(15)
    prevBackground = None
    for frame, _ in frames_iter:
        motionDetector.detect(frame)

        if motionDetector.motionEnded():
            # calc fgMask
            mask, gray_diff, color_diff, colorAbsDiff = calcForegroundMask(
                prevBackground, frame)
            # bin_diff_wnd.imshow(resize(mask, 0.5))
            bin_diff_wnd.imshow(cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR))

            # gray_diff_wnd.imshow(resize(gray_diff, .5))
            # colorDiffWnd.imshow(resize(color_diff, .5))
            # colorAbsDiffWnd.imshow(resize(colorAbsDiff, .5))
            markers, objectsCount = segmenter.segment(mask)
            segmentedWnd.imshow(
                resize(Segmenter.markersToDisplayImage(markers, objectsCount),
                       .5))

            backgroundModel = BackgroundModel(15)

        if motionDetector.isSilence():
            backgroundModel.learn(frame, foregroundMask=None)
            learned_BG_wnd.imshow(resize(backgroundModel.learned, .5))

        if motionDetector.motionStarted():
            prevBackground = backgroundModel.learned
            backgroundModel = None
            learned_BG_wnd.imshow(resize(prevBackground, .5))

        # VIS

        vis_img = motionDetector.indicateCurrentState(frame.copy())
        vis_img = utils.put_frame_pos(vis_img, video.frame_pos(), xy=(2, 55))
        video_wnd.imshow(vis_img)
        # bin_diff_wnd.imshow(resize(motionDetector.bin_diff, .5))
        # gray_diff_wnd.imshow(resize(motionDetector.gray_diff, .5))
        # VIS END

        if vc.wait_key() == 27: break

    video.release()
示例#13
0
def main():
    img = np.zeros((300, 300, 3), np.uint8)
    cv2.circle(img, (150, 150), 75, (0, 180, 0), -1)
    wnd = CvNamedWindow('select ROI')
    while True:
        wnd.imshow(img)
        key = cvWaitKeys(27, ord('r'), ord('s'))
        if key in (27, -1):
            break
示例#14
0
def visualize_contours(contours, image, winname):
    contours = sorted(contours, key=lambda c: c.len(), reverse=True)
    wnd = CvNamedWindow(winname, cv2.WINDOW_NORMAL)
    for part in contours:
        cv2.drawContours(image, [part.points()], -1, utils.random_color(), 2)
        print('part', part.len())
        wnd.imshow(image)
        cv2.waitKey()
    wnd.destroy(winname)
示例#15
0
def main():
    def roiSelectedEvent(roi, imageRoi, wnd, image):
        if roi is None:
            return
        predictions = predict_on_image(model, None, imageRoi, 0.95)
        cv2.imshow('roi', predictions)

    model = getModel()
    video = VideoCapture(video_sources.video_2)
    vc = VideoController(10, 'pause')
    wnd = Wnd('video', roiSelectedEvent=roiSelectedEvent)
    for frame in video.frames():
        cv2.destroyWindow('roi')
        wnd.imshow(frame)
        if vc.wait_key() == 27: break
示例#16
0
    def VIS_CONTOURS(edges, contours):
        print(f'DEBUG.VIS_CONTOURS: contours count {len(contours)}')
        if DEBUG.should_not_vis(edges):
            return

        im = np.zeros_like(edges)
        cv2.drawContours(im, contours, -1, 255, 1)

        images = [im]
        for c in contours:
            im = np.zeros_like(edges)
            cv2.drawContours(im, [c], -1, 255, 1)
            images.append(im)

        wnd = CvNamedWindow('DEBUG CONTOURS')
        wnd.imshow(np.hstack(images))
        cv2.waitKey()
        wnd.destroy()
示例#17
0
def main():
    feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7)
    lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

    video = VideoCapture(video_sources.video_2)
    wnd = CvNamedWindow('video')
    vc = VideoController(delay=50)

    prev_gray = None
    po = None
    tracking = False

    for frame in video.frames():
        if tracking:
            frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # calculate optical flow
            p1, st, err = cv2.calcOpticalFlowPyrLK(prev_gray, frame_gray, p0, None, **lk_params)
            p1 = p1[st == 1]
            # draw the tracks
            for pt in p1:
                a, b = pt.ravel()
                frame = cv2.circle(frame, (a, b), 5, (0, 255, 0), -1)
            prev_gray = frame_gray
            p0 = p1.reshape(-1, 1, 2)

        wnd.imshow(frame)

        key = vc.wait_key()
        if key == 27:
            break
        elif not tracking and key == ord('r'):  # init tracking
            roi = cv2.selectROI('roi', frame)
            cv2.destroyWindow('roi')

            if roi is None or sum(roi) == 0:
                continue

            prev_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            p0 = cv2.goodFeaturesToTrack(prev_gray, mask=roi_mask(prev_gray, roi), **feature_params)
            if p0 is not None and len(p0) > 0:
                tracking = True

    video.release()
示例#18
0
    def show_contour_parts(contour, parts):
        points = contour.points()
        max_x = points[..., 0].max() + 20
        max_y = points[..., 1].max() + 20
        separator = np.full((max_y, 2), 127, dtype=np.uint8)

        images = []
        for poly in parts:
            img = np.zeros((max_y, max_x), dtype=np.uint8)
            poly_points = poly.points
            if len(poly_points.shape) == 3:
                poly_points = poly_points[:, 0]
            cv2.polylines(img, [poly_points], False, 255, 1)
            for (x, y) in poly_points:
                r = 2
                img[y - r:y + r, x - r:x + r] = 200
            images.extend([img, separator])

        wnd = CvNamedWindow('parts')
        if len(images) == 0:
            return
        wnd.imshow(np.hstack(images))
示例#19
0
def main():
    # img = testImage()
    img = realImage()

    segmenter = Segmenter()

    # iters = 1000
    # images = [img.copy() for _ in range(iters)]
    # with utils.timeit_context():
    #     for i in range(1000):
    #         segmenter.segment2(images[i])
    #
    # images = [img.copy() for _ in range(iters)]
    # with utils.timeit_context():
    #     for i in range(1000):
    #         segmenter.segment(images[i])

    markers = segmenter.segment(img.copy())

    wnd = Wnd('labels')
    wnd.imshow(segmenter.markersToDisplayImage(markers))
    waitKeys()
示例#20
0
def main():
    video = VideoCapture(video_sources.video_6)

    diff_wnd = CvNamedWindow('diff')
    mask_wnd = CvNamedWindow('mask')
    input_wnd = CvNamedWindow('input')

    # prev_frame = denoise(video.read())
    prev_frame = cv2.cvtColor(denoise(video.read()), cv2.COLOR_BGR2GRAY)

    vm = VideoController(10)
    diff = None
    for frame in video.frames():
        # with utils.timeit_context('frame processing'):
        #     frame = denoise(frame)
        #     diff = cv2.absdiff(prev_frame, frame, dst=diff)
        #     ret, mask = cv2.threshold(diff, 45, 255, cv2.THRESH_BINARY)
        #     binary_mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
        #     # cn = cv2.countNonZero(binary_mask)
        #     nonzero = cv2.findNonZero(binary_mask)

        with utils.timeit_context('frame processing'):
            frame = denoise(frame)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            diff = cv2.absdiff(prev_frame, gray, dst=diff)
            ret, mask = cv2.threshold(diff, 45, 255, cv2.THRESH_BINARY)
            # cn = cv2.countNonZero(binary_mask)
            nonzero = cv2.findNonZero(mask)

        diff_wnd.imshow(diff)
        mask_wnd.imshow(mask)
        input_wnd.imshow(utils.put_frame_pos(frame.copy(), video.frame_pos()))

        prev_frame = gray

        if not vm.wait_key():
            break

    video.release()
    cv2.destroyAllWindows()
示例#21
0
class DetectionDebugger:
    def __init__(self, detector, image, region_selection=None):
        self.detector = detector
        self.base_image = image
        self.buffer_image = np.empty_like(self.base_image)
        self.region_selection = None
        self.set_region_selection(region_selection)
        self.ellipses = self.polygons = self.selected_ellipses = self.selected_polygons = []
        self.wnd = CvNamedWindow('test', mouse_callback=self.__mc)

    def set_region_selection(self, region_selection):
        if region_selection is None:
            return
        start, end = region_selection
        self.region_selection = RegionSelection(start=start).set_end(end)

    def __mc(self, evt, x, y, flags, param):
        if evt != cv2.EVENT_LBUTTONDOWN:
            return
        self.__handle_mouse_click((x, y),
                                  ctrl_key=flags & cv2.EVENT_FLAG_CTRLKEY)

    def __handle_mouse_click(self, pt, ctrl_key):
        if ctrl_key:
            self.make_region_selection(pt)
        elif self.region_selection and self.region_selection.point_test(
                pt) < 0:
            self.__clear_region_selection()
        else:
            pt = self.translate_image_pt_to_region_pt(pt)
            self.select_nearest(pt)

        self.__redraw()

    def __handle_key(self, key):
        if key == 27:
            return False
        should_redraw = False

        if key == ord('a'):
            self.__clear_region_selection()
            should_redraw = True
        elif key == ord('r'):
            self.__repeat_detect()
            should_redraw = True

        if should_redraw:
            self.__redraw()
        return True

    def translate_image_pt_to_region_pt(self, pt):
        if self.region_selection:
            return self.region_selection.translate_image_pt(pt)
        return pt

    def select_nearest(self, pt):
        nearest_polygons = self.find_nearest(pt, self.polygons)
        self.selected_polygons = nearest_polygons
        for p in nearest_polygons:
            print(f'POLY: {p.len}')
        nearest_ellipses = self.find_nearest(pt, self.ellipses)
        self.selected_ellipses = nearest_ellipses

    @staticmethod
    def find_nearest(pt, polygons):
        if len(polygons) == 0:
            return []
        polygon_distances = [(p, p.distance_from_point(pt)) for p in polygons]
        min_dist_polygon = min(polygon_distances, key=lambda item: item[1])[0]
        return [min_dist_polygon]

    def make_region_selection(self, pt):
        self.ellipses = self.polygons = self.selected_ellipses = self.selected_polygons = []
        if self.region_selection is None:
            self.region_selection = RegionSelection(start=pt)
        else:
            self.region_selection.set_end(pt)
            self.__detect()

    def __clear_region_selection(self):
        self.region_selection = None
        self.ellipses = self.polygons = self.selected_ellipses = self.selected_polygons = []
        self.__detect()

    def __repeat_detect(self):
        # self.ellipses = self.polygons = self.selected_ellipses = self.selected_polygons = []
        self.__detect()

    def __redraw(self):
        np.copyto(dst=self.buffer_image, src=self.base_image)
        if self.region_selection and self.region_selection.is_complete():
            region_buffer = self.region_selection.get_image_region(
                self.buffer_image)
        else:
            region_buffer = self.buffer_image

        # draw selection to region_buffer
        self.__draw_poly_for_presentation(self.selected_ellipses,
                                          region_buffer, (0, 255, 0), 2)
        self.__draw_poly_asis(self.selected_polygons, region_buffer,
                              (0, 0, 255), 2)

        if self.region_selection:
            self.region_selection.draw(self.buffer_image)
        self.wnd.imshow(self.buffer_image)

    @staticmethod
    def __draw_poly_asis(polygons, image, color=(0, 255, 0), thickness=2):
        pts = [p.points for p in polygons]
        cv2.polylines(image, pts, False, color, thickness)

    @staticmethod
    def __draw_poly_for_presentation(polygons,
                                     image,
                                     color=(0, 255, 0),
                                     thickness=2):
        for p in polygons:
            p.draw_for_presentation(image, color, thickness)

    def __detect(self):
        if self.region_selection:
            image = self.region_selection.get_image_region(
                self.base_image).copy()
        else:
            image = self.base_image.copy()
        self.ellipses, self.polygons = self.detector.detect(image)
        print(
            f'__detect. ellipses: {len(self.ellipses)}. polygons: {len(self.polygons)}'
        )
        self.selected_ellipses = list(self.ellipses)

    def start(self):
        self.__detect()
        self.__redraw()

        while self.__handle_key(cv2.waitKey()):
            pass