示例#1
0
    def mouse_call(event, x, y, flags, param):
        x = min(window_size[0] - 1, max(x, 0))
        y = min(window_size[1] - 1, max(y, 0))
        if event == cv.EVENT_MOUSEMOVE:
            if blobid[0] != idmap[y, x]:
                blobid[0] = idmap[y, x]
                # override color of the blob
                img[:] = background.copy()
                blob = blob_cls.get_instance(blobid[0])
                if blob is None:
                    print("\r", end="\t\t\t\t\t\t\t")
                    sys.stdout.flush()
                    return
                over_draw(img,
                          None,
                          blob.box,
                          mask=blob.mask__,
                          fill_color=WHITE)
                # ... and its adjacents
                for adj_blob, pose in zip(*blob.adj_blobs):
                    over_draw(img,
                              None,
                              adj_blob.box,
                              mask=adj_blob.mask__,
                              fill_color=POSE2COLOR[pose])

                rD = blob.Dert.Dy / blob.Dert.Dx if blob.Dert.Dx else 2 * blob.Dert.Dy
                dir_val = abs(blob.Dert.G * rD)

                # ... print blobs properties.
                print("\rblob:",
                      "id =",
                      blob.id,
                      "sign =",
                      "'+'" if blob.sign else "'-'",
                      "I =",
                      blob.Dert.I,
                      "G =",
                      blob.Dert.G,
                      "Dy =",
                      blob.Dert.Dy,
                      "Dx =",
                      blob.Dert.Dx,
                      "S =",
                      blob.A,
                      "M = ",
                      blob.Dert.M,
                      "dir_val = ",
                      dir_val,
                      end="\t\t\t")
                sys.stdout.flush()
示例#2
0
    def update(self, y, P_=()):
        """Call this method each update."""
        # draw Ps in new row
        for P in P_:
            self.img[y, P.x0:P.x0 + P.L] = self.sign_map_unterminated[P.sign]

        # add new blobs' ids, if any
        id_end = self.blob_cls.instance_cnt
        if self.first_id < id_end:
            new_blobs_ids = range(self.first_id, id_end)
            self.incomplete_blob_ids.update(new_blobs_ids)
            self.first_id = id_end

        # iterate through incomplete blobs
        for blob_id in set(self.incomplete_blob_ids):
            blob = self.blob_cls.get_instance(blob_id)
            if blob is None:  # blob has been merged, remove its ref
                self.incomplete_blob_ids.remove(blob_id)
                continue
            elif blob.open_stacks == 0:
                # has no open_stack, has been terminated,
                # re-draw with normal colors, remove ref
                self.incomplete_blob_ids.remove(blob_id)
                blob_img = draw_blob(blob,
                                     blob_box=blob.box,
                                     sign_map=BlobStreamer.sign_map)
                over_draw(self.img, blob_img, blob.box)

                # add to id_map
                over_draw(self._id_map,
                          None,
                          blob.box,
                          mask=blob.mask,
                          fill_color=blob.id)

        # resize window to display
        self.frame = cv.resize(self.img,
                               self.window_size,
                               interpolation=cv.INTER_NEAREST)
        super().update()
示例#3
0
def visualize_blobs(idmap, blob_, window_size=None, winname="Blobs"):
    """
    Visualize blobs after clustering.
    Highlight the blob the mouse is hovering on and its
    adjacents.
    """
    print("Preparing for visualization...", end="")
    blob_cls = blob_[0].__class__
    height, width = idmap.shape
    if window_size is None:
        window_size = (
            max(width, MIN_WINDOW_WIDTH),
            max(height, MIN_WINDOW_HEIGHT),
        )
    background = blank_image((height, width))
    for blob in blob_:
        over_draw(background,
                  None,
                  blob.box,
                  mask=blob.mask__,
                  fill_color=[blob.sign * 32] * 3)

    idmap = cv.resize(idmap.astype('uint64'),
                      window_size,
                      interpolation=cv.INTER_NEAREST)
    img = background.copy()
    blobid = [-1]

    def mouse_call(event, x, y, flags, param):
        x = min(window_size[0] - 1, max(x, 0))
        y = min(window_size[1] - 1, max(y, 0))
        if event == cv.EVENT_MOUSEMOVE:
            if blobid[0] != idmap[y, x]:
                blobid[0] = idmap[y, x]
                # override color of the blob
                img[:] = background.copy()
                blob = blob_cls.get_instance(blobid[0])
                if blob is None:
                    print("\r", end="\t\t\t\t\t\t\t")
                    sys.stdout.flush()
                    return
                over_draw(img,
                          None,
                          blob.box,
                          mask=blob.mask__,
                          fill_color=WHITE)
                # ... and its adjacents
                for adj_blob, pose in zip(*blob.adj_blobs):
                    over_draw(img,
                              None,
                              adj_blob.box,
                              mask=adj_blob.mask__,
                              fill_color=POSE2COLOR[pose])

                rD = blob.Dert.Dy / blob.Dert.Dx if blob.Dert.Dx else 2 * blob.Dert.Dy
                dir_val = abs(blob.Dert.G * rD)

                # ... print blobs properties.
                print("\rblob:",
                      "id =",
                      blob.id,
                      "sign =",
                      "'+'" if blob.sign else "'-'",
                      "I =",
                      blob.Dert.I,
                      "G =",
                      blob.Dert.G,
                      "Dy =",
                      blob.Dert.Dy,
                      "Dx =",
                      blob.Dert.Dx,
                      "S =",
                      blob.A,
                      "M = ",
                      blob.Dert.M,
                      "dir_val = ",
                      dir_val,
                      end="\t\t\t")
                sys.stdout.flush()

    cv.namedWindow(winname)
    cv.setMouseCallback(winname, mouse_call)
    print()
    while True:
        cv.imshow(winname,
                  cv.resize(img, window_size, interpolation=cv.INTER_NEAREST))
        if cv.waitKey(1) == ord('q'):
            break
    cv.destroyAllWindows()
    print()
示例#4
0
        def mouse_call(event, x, y, flags, param):
            x = min(self.window_size[0] - 1, max(x, 0))
            y = min(self.window_size[1] - 1, max(y, 0))
            self.x = x
            self.y = y

            if event == cv.EVENT_LBUTTONDOWN and not self.is_zooming:
                self.x1 = x
                self.y1 = y
                self.render = self._render_draw_rectangle
            elif event == cv.EVENT_LBUTTONUP:
                if not self.is_zooming:
                    if x != self.x1 and y != self.y1:
                        self.x2 = x
                        self.y2 = y
                        if x < self.x1:
                            self.x1, self.x2 = x, self.x1
                        if y < self.y1:
                            self.y1, self.y2 = y, self.y1
                        self.render = self._zoomed_render
                        self.is_zooming = True
                        self.id_map = cv.resize(self._id_map[self.y1:self.y2,
                                                             self.x1:self.x2],
                                                self.window_size,
                                                interpolation=cv.INTER_NEAREST)

                else:
                    self.is_zooming = False
                    self.render = self._render
                    self.id_map = self._id_map

            elif event == cv.EVENT_MOUSEMOVE:
                if self.pointing_blob_id != self.id_map[y, x]:
                    self.pointing_blob_id = self.id_map[y, x]
                    # override color of the blob
                    self.img = np.copy(self.background)
                    blob = self.blob_cls.get_instance(self.pointing_blob_id)
                    if blob is None:
                        print("\r", end="\t\t\t\t\t\t\t")
                        sys.stdout.flush()
                        return
                    over_draw(self.img,
                              None,
                              blob.box,
                              mask=blob.mask,
                              fill_color=(255, 255, 255))  # gray
                    # ... and its adjacents
                    for adj_blob, pose in blob.adj_blobs[0]:
                        if pose == 0:  # internal
                            color = (0, 0, 255)  # red
                        elif pose == 1:  # external
                            color = (0, 255, 0)  # green
                        elif pose == 2:  # open
                            color = (255, 0, 0)  # blue
                        else:
                            raise ValueError(
                                "adj pose id incorrect. Something is wrong")
                        over_draw(self.img,
                                  None,
                                  adj_blob.box,
                                  mask=adj_blob.mask,
                                  fill_color=color)
                    # ... print blobs properties.
                    print("\rblob:",
                          "id =",
                          blob.id,
                          "sign =",
                          "'+'" if blob.sign else "'-'",
                          "I =",
                          blob.Dert['I'],
                          "G =",
                          blob.Dert['G'],
                          "Dy =",
                          blob.Dert['Dy'],
                          "Dx =",
                          blob.Dert['Dx'],
                          "S =",
                          blob.Dert['S'],
                          "Ly =",
                          blob.Dert['Ly'],
                          end="\t\t\t")
                    sys.stdout.flush()